ChibiOS/RT
2.5.1
I/O Queues
Collaboration diagram for I/O Queues:

Detailed Description

ChibiOS/RT queues are mostly used in serial-like device drivers. The device drivers are usually designed to have a lower side (lower driver, it is usually an interrupt service routine) and an upper side (upper driver, accessed by the application threads).
There are several kind of queues:

Precondition:
In order to use the I/O queues the CH_USE_QUEUES option must be enabled in chconf.h.

Data Structures

struct  GenericQueue
 Generic I/O queue structure. More...

Functions

void chIQInit (InputQueue *iqp, uint8_t *bp, size_t size, qnotify_t infy, void *link)
 Initializes an input queue.
void chIQResetI (InputQueue *iqp)
 Resets an input queue.
msg_t chIQPutI (InputQueue *iqp, uint8_t b)
 Input queue write.
msg_t chIQGetTimeout (InputQueue *iqp, systime_t time)
 Input queue read with timeout.
size_t chIQReadTimeout (InputQueue *iqp, uint8_t *bp, size_t n, systime_t time)
 Input queue read with timeout.
void chOQInit (OutputQueue *oqp, uint8_t *bp, size_t size, qnotify_t onfy, void *link)
 Initializes an output queue.
void chOQResetI (OutputQueue *oqp)
 Resets an output queue.
msg_t chOQPutTimeout (OutputQueue *oqp, uint8_t b, systime_t time)
 Output queue write with timeout.
msg_t chOQGetI (OutputQueue *oqp)
 Output queue read.
size_t chOQWriteTimeout (OutputQueue *oqp, const uint8_t *bp, size_t n, systime_t time)
 Output queue write with timeout.

Queue functions returned status value

#define Q_OK   RDY_OK
 Operation successful.
#define Q_TIMEOUT   RDY_TIMEOUT
 Timeout condition.
#define Q_RESET   RDY_RESET
 Queue has been reset.
#define Q_EMPTY   -3
 Queue empty.
#define Q_FULL   -4
 Queue full,.

Macro Functions

#define chQSizeI(qp)   ((size_t)((qp)->q_top - (qp)->q_buffer))
 Returns the queue's buffer size.
#define chQSpaceI(qp)   ((qp)->q_counter)
 Queue space.
#define chQGetLink(qp)   ((qp)->q_link)
 Returns the queue application-defined link.
#define chIQGetFullI(iqp)   chQSpaceI(iqp)
 Returns the filled space into an input queue.
#define chIQGetEmptyI(iqp)   (chQSizeI(iqp) - chQSpaceI(iqp))
 Returns the empty space into an input queue.
#define chIQIsEmptyI(iqp)   ((bool_t)(chQSpaceI(iqp) <= 0))
 Evaluates to TRUE if the specified input queue is empty.
#define chIQIsFullI(iqp)
 Evaluates to TRUE if the specified input queue is full.
#define chIQGet(iqp)   chIQGetTimeout(iqp, TIME_INFINITE)
 Input queue read.
#define chOQGetFullI(oqp)   (chQSizeI(oqp) - chQSpaceI(oqp))
 Returns the filled space into an output queue.
#define chOQGetEmptyI(oqp)   chQSpaceI(oqp)
 Returns the empty space into an output queue.
#define chOQIsEmptyI(oqp)
 Evaluates to TRUE if the specified output queue is empty.
#define chOQIsFullI(oqp)   ((bool_t)(chQSpaceI(oqp) <= 0))
 Evaluates to TRUE if the specified output queue is full.
#define chOQPut(oqp, b)   chOQPutTimeout(oqp, b, TIME_INFINITE)
 Output queue write.

Defines

#define _INPUTQUEUE_DATA(name, buffer, size, inotify, link)
 Data part of a static input queue initializer.
#define INPUTQUEUE_DECL(name, buffer, size, inotify, link)   InputQueue name = _INPUTQUEUE_DATA(name, buffer, size, inotify, link)
 Static input queue initializer.
#define _OUTPUTQUEUE_DATA(name, buffer, size, onotify, link)
 Data part of a static output queue initializer.
#define OUTPUTQUEUE_DECL(name, buffer, size, onotify, link)   OutputQueue name = _OUTPUTQUEUE_DATA(name, buffer, size, onotify, link)
 Static output queue initializer.

Typedefs

typedef struct GenericQueue GenericQueue
 Type of a generic I/O queue structure.
typedef void(* qnotify_t )(GenericQueue *qp)
 Queue notification callback type.
typedef GenericQueue InputQueue
 Type of an input queue structure.
typedef GenericQueue OutputQueue
 Type of an output queue structure.

Function Documentation

void chIQInit ( InputQueue iqp,
uint8_t *  bp,
size_t  size,
qnotify_t  infy,
void *  link 
)

Initializes an input queue.

A Semaphore is internally initialized and works as a counter of the bytes contained in the queue.

Note:
The callback is invoked from within the S-Locked system state, see System States.
Parameters:
[out]iqppointer to an InputQueue structure
[in]bppointer to a memory area allocated as queue buffer
[in]sizesize of the queue buffer
[in]infypointer to a callback function that is invoked when data is read from the queue. The value can be NULL.
[in]linkapplication defined pointer
Function Class:
Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 88 of file chqueues.c.

References GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_link, GenericQueue::q_notify, GenericQueue::q_rdptr, GenericQueue::q_top, GenericQueue::q_waiting, GenericQueue::q_wrptr, and queue_init.

void chIQResetI ( InputQueue iqp)

Resets an input queue.

All the data in the input queue is erased and lost, any waiting thread is resumed with status Q_RESET.

Note:
A reset operation can be used by a low level driver in order to obtain immediate attention from the high level layers.
Parameters:
[in]iqppointer to an InputQueue structure
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 110 of file chqueues.c.

References chDbgCheckClassI(), chSchReadyI(), fifo_remove(), notempty, GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_rdptr, Q_RESET, GenericQueue::q_waiting, GenericQueue::q_wrptr, and Thread::rdymsg.

Here is the call graph for this function:

msg_t chIQPutI ( InputQueue iqp,
uint8_t  b 
)

Input queue write.

A byte value is written into the low end of an input queue.

Parameters:
[in]iqppointer to an InputQueue structure
[in]bthe byte value to be written in the queue
Returns:
The operation status.
Return values:
Q_OKif the operation has been completed with success.
Q_FULLif the queue is full and the operation cannot be completed.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 133 of file chqueues.c.

References chDbgCheckClassI(), chIQIsFullI, chSchReadyI(), fifo_remove(), notempty, GenericQueue::q_buffer, GenericQueue::q_counter, Q_FULL, Q_OK, GenericQueue::q_top, GenericQueue::q_waiting, GenericQueue::q_wrptr, and Thread::rdymsg.

Here is the call graph for this function:

msg_t chIQGetTimeout ( InputQueue iqp,
systime_t  time 
)

Input queue read with timeout.

This function reads a byte value from an input queue. If the queue is empty then the calling thread is suspended until a byte arrives in the queue or a timeout occurs.

Note:
The callback is invoked before reading the character from the buffer or before entering the state THD_STATE_WTQUEUE.
Parameters:
[in]iqppointer to an InputQueue structure
[in]timethe number of ticks before the operation timeouts, the following special values are allowed:
  • TIME_IMMEDIATE immediate timeout.
  • TIME_INFINITE no timeout.
Returns:
A byte value from the queue.
Return values:
Q_TIMEOUTif the specified time expired.
Q_RESETif the queue has been reset.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 171 of file chqueues.c.

References chIQIsEmptyI, chSysLock, chSysUnlock, GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_notify, Q_OK, GenericQueue::q_rdptr, and GenericQueue::q_top.

size_t chIQReadTimeout ( InputQueue iqp,
uint8_t *  bp,
size_t  n,
systime_t  time 
)

Input queue read with timeout.

The function reads data from an input queue into a buffer. The operation completes when the specified amount of data has been transferred or after the specified timeout or if the queue has been reset.

Note:
The function is not atomic, if you need atomicity it is suggested to use a semaphore or a mutex for mutual exclusion.
The callback is invoked before reading each character from the buffer or before entering the state THD_STATE_WTQUEUE.
Parameters:
[in]iqppointer to an InputQueue structure
[out]bppointer to the data buffer
[in]nthe maximum amount of data to be transferred, the value 0 is reserved
[in]timethe number of ticks before the operation timeouts, the following special values are allowed:
  • TIME_IMMEDIATE immediate timeout.
  • TIME_INFINITE no timeout.
Returns:
The number of bytes effectively transferred.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 219 of file chqueues.c.

References chDbgCheck, chIQIsEmptyI, chSysLock, chSysUnlock, GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_notify, Q_OK, GenericQueue::q_rdptr, GenericQueue::q_top, and TRUE.

void chOQInit ( OutputQueue oqp,
uint8_t *  bp,
size_t  size,
qnotify_t  onfy,
void *  link 
)

Initializes an output queue.

A Semaphore is internally initialized and works as a counter of the free bytes in the queue.

Note:
The callback is invoked from within the S-Locked system state, see System States.
Parameters:
[out]oqppointer to an OutputQueue structure
[in]bppointer to a memory area allocated as queue buffer
[in]sizesize of the queue buffer
[in]onfypointer to a callback function that is invoked when data is written to the queue. The value can be NULL.
[in]linkapplication defined pointer
Function Class:
Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 268 of file chqueues.c.

References GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_link, GenericQueue::q_notify, GenericQueue::q_rdptr, GenericQueue::q_top, GenericQueue::q_waiting, GenericQueue::q_wrptr, and queue_init.

void chOQResetI ( OutputQueue oqp)

Resets an output queue.

All the data in the output queue is erased and lost, any waiting thread is resumed with status Q_RESET.

Note:
A reset operation can be used by a low level driver in order to obtain immediate attention from the high level layers.
Parameters:
[in]oqppointer to an OutputQueue structure
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 290 of file chqueues.c.

References chDbgCheckClassI(), chQSizeI, chSchReadyI(), fifo_remove(), notempty, GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_rdptr, Q_RESET, GenericQueue::q_waiting, GenericQueue::q_wrptr, and Thread::rdymsg.

Here is the call graph for this function:

msg_t chOQPutTimeout ( OutputQueue oqp,
uint8_t  b,
systime_t  time 
)

Output queue write with timeout.

This function writes a byte value to an output queue. If the queue is full then the calling thread is suspended until there is space in the queue or a timeout occurs.

Note:
The callback is invoked after writing the character into the buffer.
Parameters:
[in]oqppointer to an OutputQueue structure
[in]bthe byte value to be written in the queue
[in]timethe number of ticks before the operation timeouts, the following special values are allowed:
  • TIME_IMMEDIATE immediate timeout.
  • TIME_INFINITE no timeout.
Returns:
The operation status.
Return values:
Q_OKif the operation succeeded.
Q_TIMEOUTif the specified time expired.
Q_RESETif the queue has been reset.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 322 of file chqueues.c.

References chOQIsFullI, chSysLock, chSysUnlock, GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_notify, Q_OK, GenericQueue::q_top, and GenericQueue::q_wrptr.

msg_t chOQGetI ( OutputQueue oqp)

Output queue read.

A byte value is read from the low end of an output queue.

Parameters:
[in]oqppointer to an OutputQueue structure
Returns:
The byte value from the queue.
Return values:
Q_EMPTYif the queue is empty.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 356 of file chqueues.c.

References chDbgCheckClassI(), chOQIsEmptyI, chSchReadyI(), fifo_remove(), notempty, GenericQueue::q_buffer, GenericQueue::q_counter, Q_EMPTY, Q_OK, GenericQueue::q_rdptr, GenericQueue::q_top, GenericQueue::q_waiting, and Thread::rdymsg.

Here is the call graph for this function:

size_t chOQWriteTimeout ( OutputQueue oqp,
const uint8_t *  bp,
size_t  n,
systime_t  time 
)

Output queue write with timeout.

The function writes data from a buffer to an output queue. The operation completes when the specified amount of data has been transferred or after the specified timeout or if the queue has been reset.

Note:
The function is not atomic, if you need atomicity it is suggested to use a semaphore or a mutex for mutual exclusion.
The callback is invoked after writing each character into the buffer.
Parameters:
[in]oqppointer to an OutputQueue structure
[out]bppointer to the data buffer
[in]nthe maximum amount of data to be transferred, the value 0 is reserved
[in]timethe number of ticks before the operation timeouts, the following special values are allowed:
  • TIME_IMMEDIATE immediate timeout.
  • TIME_INFINITE no timeout.
Returns:
The number of bytes effectively transferred.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 399 of file chqueues.c.

References chDbgCheck, chOQIsFullI, chSysLock, chSysUnlock, GenericQueue::q_buffer, GenericQueue::q_counter, GenericQueue::q_notify, Q_OK, GenericQueue::q_top, GenericQueue::q_wrptr, and TRUE.


Define Documentation

#define Q_OK   RDY_OK

Operation successful.

Definition at line 38 of file chqueues.h.

Referenced by chIQGetTimeout(), chIQPutI(), chIQReadTimeout(), chOQGetI(), chOQPutTimeout(), and chOQWriteTimeout().

#define Q_TIMEOUT   RDY_TIMEOUT

Timeout condition.

Definition at line 39 of file chqueues.h.

#define Q_RESET   RDY_RESET

Queue has been reset.

Definition at line 40 of file chqueues.h.

Referenced by chIQResetI(), and chOQResetI().

#define Q_EMPTY   -3

Queue empty.

Definition at line 41 of file chqueues.h.

Referenced by chOQGetI().

#define Q_FULL   -4

Queue full,.

Definition at line 42 of file chqueues.h.

Referenced by chIQPutI().

#define chQSizeI (   qp)    ((size_t)((qp)->q_top - (qp)->q_buffer))

Returns the queue's buffer size.

Parameters:
[in]qppointer to a GenericQueue structure.
Returns:
The buffer size.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 86 of file chqueues.h.

Referenced by chOQResetI().

#define chQSpaceI (   qp)    ((qp)->q_counter)

Queue space.

Returns the used space if used on an input queue or the empty space if used on an output queue.

Parameters:
[in]qppointer to a GenericQueue structure.
Returns:
The buffer space.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 98 of file chqueues.h.

#define chQGetLink (   qp)    ((qp)->q_link)

Returns the queue application-defined link.

Note:
This function can be called in any context.
Parameters:
[in]qppointer to a GenericQueue structure.
Returns:
The application-defined link.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 109 of file chqueues.h.

#define chIQGetFullI (   iqp)    chQSpaceI(iqp)

Returns the filled space into an input queue.

Parameters:
[in]iqppointer to an InputQueue structure
Returns:
The number of full bytes in the queue.
Return values:
0if the queue is empty.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 138 of file chqueues.h.

#define chIQGetEmptyI (   iqp)    (chQSizeI(iqp) - chQSpaceI(iqp))

Returns the empty space into an input queue.

Parameters:
[in]iqppointer to an InputQueue structure
Returns:
The number of empty bytes in the queue.
Return values:
0if the queue is full.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 149 of file chqueues.h.

#define chIQIsEmptyI (   iqp)    ((bool_t)(chQSpaceI(iqp) <= 0))

Evaluates to TRUE if the specified input queue is empty.

Parameters:
[in]iqppointer to an InputQueue structure.
Returns:
The queue status.
Return values:
FALSEThe queue is not empty.
TRUEThe queue is empty.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 161 of file chqueues.h.

Referenced by chIQGetTimeout(), and chIQReadTimeout().

#define chIQIsFullI (   iqp)
Value:
((bool_t)(((iqp)->q_wrptr == (iqp)->q_rdptr) &&   \
                                   ((iqp)->q_counter != 0)))

Evaluates to TRUE if the specified input queue is full.

Parameters:
[in]iqppointer to an InputQueue structure.
Returns:
The queue status.
Return values:
FALSEThe queue is not full.
TRUEThe queue is full.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 173 of file chqueues.h.

Referenced by chIQPutI().

#define chIQGet (   iqp)    chIQGetTimeout(iqp, TIME_INFINITE)

Input queue read.

This function reads a byte value from an input queue. If the queue is empty then the calling thread is suspended until a byte arrives in the queue.

Parameters:
[in]iqppointer to an InputQueue structure
Returns:
A byte value from the queue.
Return values:
Q_RESETif the queue has been reset.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 188 of file chqueues.h.

#define _INPUTQUEUE_DATA (   name,
  buffer,
  size,
  inotify,
  link 
)
Value:
{               \
  _THREADSQUEUE_DATA(name),                                                 \
  0,                                                                        \
  (uint8_t *)(buffer),                                                      \
  (uint8_t *)(buffer) + (size),                                             \
  (uint8_t *)(buffer),                                                      \
  (uint8_t *)(buffer),                                                      \
  (inotify),                                                                \
  (link)                                                                    \
}

Data part of a static input queue initializer.

This macro should be used when statically initializing an input queue that is part of a bigger structure.

Parameters:
[in]namethe name of the input queue variable
[in]bufferpointer to the queue buffer area
[in]sizesize of the queue buffer area
[in]inotifyinput notification callback pointer
[in]linkapplication defined pointer

Definition at line 202 of file chqueues.h.

#define INPUTQUEUE_DECL (   name,
  buffer,
  size,
  inotify,
  link 
)    InputQueue name = _INPUTQUEUE_DATA(name, buffer, size, inotify, link)

Static input queue initializer.

Statically initialized input queues require no explicit initialization using chIQInit().

Parameters:
[in]namethe name of the input queue variable
[in]bufferpointer to the queue buffer area
[in]sizesize of the queue buffer area
[in]inotifyinput notification callback pointer
[in]linkapplication defined pointer

Definition at line 224 of file chqueues.h.

#define chOQGetFullI (   oqp)    (chQSizeI(oqp) - chQSpaceI(oqp))

Returns the filled space into an output queue.

Parameters:
[in]oqppointer to an OutputQueue structure
Returns:
The number of full bytes in the queue.
Return values:
0if the queue is empty.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 253 of file chqueues.h.

#define chOQGetEmptyI (   oqp)    chQSpaceI(oqp)

Returns the empty space into an output queue.

Parameters:
[in]oqppointer to an OutputQueue structure
Returns:
The number of empty bytes in the queue.
Return values:
0if the queue is full.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 264 of file chqueues.h.

#define chOQIsEmptyI (   oqp)
Value:
((bool_t)(((oqp)->q_wrptr == (oqp)->q_rdptr) &&   \
                                    ((oqp)->q_counter != 0)))

Evaluates to TRUE if the specified output queue is empty.

Parameters:
[in]oqppointer to an OutputQueue structure.
Returns:
The queue status.
Return values:
FALSEThe queue is not empty.
TRUEThe queue is empty.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 276 of file chqueues.h.

Referenced by chOQGetI().

#define chOQIsFullI (   oqp)    ((bool_t)(chQSpaceI(oqp) <= 0))

Evaluates to TRUE if the specified output queue is full.

Parameters:
[in]oqppointer to an OutputQueue structure.
Returns:
The queue status.
Return values:
FALSEThe queue is not full.
TRUEThe queue is full.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 289 of file chqueues.h.

Referenced by chOQPutTimeout(), and chOQWriteTimeout().

#define chOQPut (   oqp,
 
)    chOQPutTimeout(oqp, b, TIME_INFINITE)

Output queue write.

This function writes a byte value to an output queue. If the queue is full then the calling thread is suspended until there is space in the queue.

Parameters:
[in]oqppointer to an OutputQueue structure
[in]bthe byte value to be written in the queue
Returns:
The operation status.
Return values:
Q_OKif the operation succeeded.
Q_RESETif the queue has been reset.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 305 of file chqueues.h.

#define _OUTPUTQUEUE_DATA (   name,
  buffer,
  size,
  onotify,
  link 
)
Value:
{              \
  _THREADSQUEUE_DATA(name),                                                 \
  (size),                                                                   \
  (uint8_t *)(buffer),                                                      \
  (uint8_t *)(buffer) + (size),                                             \
  (uint8_t *)(buffer),                                                      \
  (uint8_t *)(buffer),                                                      \
  (onotify),                                                                \
  (link)                                                                    \
}

Data part of a static output queue initializer.

This macro should be used when statically initializing an output queue that is part of a bigger structure.

Parameters:
[in]namethe name of the output queue variable
[in]bufferpointer to the queue buffer area
[in]sizesize of the queue buffer area
[in]onotifyoutput notification callback pointer
[in]linkapplication defined pointer

Definition at line 319 of file chqueues.h.

#define OUTPUTQUEUE_DECL (   name,
  buffer,
  size,
  onotify,
  link 
)    OutputQueue name = _OUTPUTQUEUE_DATA(name, buffer, size, onotify, link)

Static output queue initializer.

Statically initialized output queues require no explicit initialization using chOQInit().

Parameters:
[in]namethe name of the output queue variable
[in]bufferpointer to the queue buffer area
[in]sizesize of the queue buffer area
[in]onotifyoutput notification callback pointer
[in]linkapplication defined pointer

Definition at line 341 of file chqueues.h.


Typedef Documentation

typedef struct GenericQueue GenericQueue

Type of a generic I/O queue structure.

Definition at line 48 of file chqueues.h.

typedef void(* qnotify_t)(GenericQueue *qp)

Queue notification callback type.

Definition at line 51 of file chqueues.h.

Type of an input queue structure.

This structure represents a generic asymmetrical input queue. Writing to the queue is non-blocking and can be performed from interrupt handlers or from within a kernel lock zone (see I-Locked and S-Locked states in System States). Reading the queue can be a blocking operation and is supposed to be performed by a system thread.

Definition at line 123 of file chqueues.h.

Type of an output queue structure.

This structure represents a generic asymmetrical output queue. Reading from the queue is non-blocking and can be performed from interrupt handlers or from within a kernel lock zone (see I-Locked and S-Locked states in System States). Writing the queue can be a blocking operation and is supposed to be performed by a system thread.

Definition at line 238 of file chqueues.h.