Queues
Memory and Low-level Functions 5-17
5.3.3 QUE Example
Example 5-18 uses a QUE queue to send five messages from a writer to a
reader task. The functions MEM_alloc and MEM_free are used to allocate
and free the MsgObj structures.
The program in Example 5-18 yields the results shown in Figure 5-3. The
writer task uses QUE_put to enqueue each of its five messages and then the
reader task uses QUE_get to dequeue each message.
Example 5-18. Using QUE to Send Messages
/*
* ======== quetest.c ========
* Use a QUE queue to send messages from a writer to a read
* reader.
*
* The queue is created by the Configuration Tool.
* For simplicity, we use MEM_alloc and MEM_free to manage
* the MsgObj structures. It would be way more efficient to
* preallocate a pool of MsgObj's and keep them on a 'free'
* queue. Using the Config Tool, create 'freeQueue'. Then in
* main, allocate the MsgObj's with MEM_alloc and add them to
* 'freeQueue' with QUE_put.
* You can then replace MEM_alloc calls with QUE_get(freeQueue)
* and MEM_free with QUE_put(freeQueue, msg).
*
* A queue can hold an arbitrary number of messages or elements.
* Each message must, however, be a structure with a QUE_Elem as
* its first field.
*/
#include <std.h>
#include <log.h>
#include <mem.h>
#include <que.h>
#include <sys.h>
#define NUMMSGS 5 /* number of messages */
typedef struct MsgObj {
QUE_Elem elem; /* first field for QUE */
Char val; /* message value */
} MsgObj, *Msg;
extern QUE_Obj queue;
/* Trace Log created statically */
extern LOG_Obj trace;
Void reader();
Void writer();