Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sauermann J.Realtime operating systems.Concepts and implementation of microkernels for embedded systems.1997.pdf
Скачиваний:
27
Добавлен:
23.08.2013
Размер:
1.32 Mб
Скачать

30

2.5 Queues

 

 

driven serial port. For each direction, a buffer is used between the task and the serial port, as shown in Figure 2.14. Assume further that the task shall echo all characters received to the serial port, possibly running at a lower speed. At a first glance, you may expect to have the (upper) receive buffer used with a get semaphore, and the (lower) transmit buffer with a put semaphore. The task will be blocked most of the time on the get semaphore, which is a normal condition. What would happen, however, if the task would block on the put semaphore, i.e. if the transmit buffer is full? This will eventually happen if the transmit data rate is lower than the receive data rate. In this case, one would normally signal the sender at the far end to stop transmission for a while, for example by hardware or software handshake. A blocked task, however, would not be able to do this. This scenario is quite common, and one would use a get semaphore for the upper buffer, but a plain ring buffer for the lower one.

Serial Port

Put

Get

Rx

Task

Tx

Get

Put

FIGURE 2.14 Serial Communication between a Task and a Serial Port

2.5.4 Ring Buffer with Get and Put Semaphores

The final option is to use both a get and a put semaphore. The buffer and the semaphores are initialized as described in the previous sections.

For each Put(), a P() call is made to the put semaphore before the item is inserted, and a V() call is made to the get semaphore after the item is inserted:

Call P() for PutSemaphore

(block until there is space)

Buffer[PutIndex] = Item

PutIndex = (PutIndex + 1) modulo BufferSize

Call V() for GetSemaphore

(indicate a new item)

For each Get(), a removed, and a P() from the buffer.

V() call is made on the get semaphore before an item is call is made on the put semaphore after removing an item

2. Concepts

31

 

 

Call P() for GetSemaphore

(block until there is an item)

Item = Buffer[GettIndex]

GetIndex = (GetIndex + 1) modulo BufferSize

Call V() for PutSemaphore

(indicate space available)

Return Item

This ring buffer with get and put semaphore is optimal in the sense that no time is wasted, and no error condition is returned on either full or empty queues. However, it cannot be used in any ISR, since both sides, Put() and Get(), use the P() call which is forbidden for ISRs. Thus the only application for this scheme would be the communication between tasks. Moreover, the disadvantages of put semaphores apply here as well.

32