Character Queues





The file COMMON\CQUEUES.FTH provides circular character (byte) queues. If the equate TASKING? is non-zero, the blocking routines will use PAUSE. Interrupts are disabled for the queue empty/full checks.

struct /cqueue  \ -- size ; character queue structure in idata, buffer in udata
Circular queue data structure.

  int >qhead            \ Offset of head
  int >qtail            \ Offset of tail
  int >qchars           \ Number of characters in the queue
  int >qmask            \ Mask to apply to pointers
  ptr >qbuffer          \ Base address of character buffer
end-struct

: cqueue:       \ size -- ; -- cqueue ; size CQUEUE: <name>
An interpreter definition to build a character queue of the specified size. The queue data structure is built in the current IDATA space, and the buffer itself is in the current UDATA space. Executing <name> returns the address of the queue data structure. N.B. The size of a queue must be a power of two, e.g. 32, 64 ...

: init-cqueue   \ cqueue -- ; initialise queue
Initialise the specified queue created by CQUEUE:.

: init-hcqueue  \ size cqueue -- ; SFP002
Initialise the specified queue created by ALLOCATE. When a queue is allocated from the heap by a phrase of the form "/CQUEUE <size> + ALLOCATE", this word must be used.

: (>cqueue)     \ char cqueue -- ; put character on cqueue
Put char into the queue with no checks.

: (cqueue>)     \ cqueue -- char ; get next character from queue
Get the next character from the queue with no checks.

: (cqfull?)     \ queue -- flag ; TRUE if queue full
Return true if the queue is full. No interrupt protection is provided.

: cqfull?       \ queue -- flag ; TRUE if queue full
Return true if the queue is full. Interrupt protection is provided.

: cqchars       \ queue -- n
Return the number of characters in the queue.

: cqempty?      \ queue -- flag ; TRUE if queue empty
Return true if the queue is empty.

: cqnotempty?   \ queue -- flag ; TRUE if queue not empty
Return true if the queue is not empty, i.e. if it contains any characters.

: >cqueue       \ char cqueue -- ; spins if full
Put a character into the queue. If the queue is full, the system waits (blocks) until there is enough space.

: cqueue>       \ queue -- char ; spins while queue empty
Remove the next character, waiting if the queue is empty.