Real-Time Operating Systems: Queue Management
Real-Time Operating Systems: Queue Management
Queue Management
Dr Usman Zabit
By Richard Barry
Copyright © 2016
2
by Real-time Engineers Ltd
CHAPTER 4
QUEUE MANAGEMENT
3
4.1 CHAPTER INTRODUCTION AND
SCOPE
‘Queues’ provide a task-to-task, task-to-interrupt, and
interrupt-to-task communication mechanism.
Scope
This chapter aims to give readers a good understanding of:
4
4.1 CHAPTER INTRODUCTION AND
SCOPE
…
How to block on multiple queues.
The maximum number of items a queue can hold is called its ‘length’.
Both the length and the size of each data item are set
when the queue is created.
Queues are normally used as First In First Out (FIFO) buffers, where
data is written to the end (tail) of the queue and removed from the front
(head) of the queue.
Figure 31 demonstrates data being written to and read from a queue
that is being used as a FIFO.
It is also possible to write to the front of a queue, and to overwrite data
that is already at the front of a queue.
6
AN EXAMPLE SEQUENCE OF WRITES
TO, AND READS FROM A QUEUE: 1
7
AN EXAMPLE SEQUENCE OF WRITES
TO, AND READS FROM A QUEUE: 2
8
AN EXAMPLE SEQUENCE OF WRITES
TO, AND READS FROM A QUEUE: 3
9
AN EXAMPLE SEQUENCE OF WRITES
TO, AND READS FROM A QUEUE: 4
10
AN EXAMPLE SEQUENCE OF WRITES
TO, AND READS FROM A QUEUE: 5
11
FIGURE 7 FROM “CH 2: HEAP MEMORY MANAGEMENT”
12
HOW DATA IS MOVED TO A QUEUE
There are two ways in which queue behavior could have been
implemented:
1. Queue by copy
Queuing by copy means the data sent to the queue is copied
byte for byte into the queue.
2. Queue by reference
Queuing by reference means the queue only holds pointers to
the data sent to the queue, not the data itself.
13
QUEUE BY COPY METHOD
FreeRTOS uses the queue by copy method.
Queuing by copy is considered to be simultaneously more
powerful and simpler to use than queueing by reference because:
Stack variable can be sent directly to a queue, even though the
variable will not exist after the function in which it is declared
has exited.
Data can be sent to a queue without first allocating a buffer to
hold the data, and then copying the data into the allocated buffer.
The sending task can immediately re-use the variable or buffer
that was sent to the queue.
The sending task and the receiving task are completely de-
coupled—the application designer does not need to concern
themselves with which task ‘owns’ the data, or
14
which task is responsible for releasing the data.
QUEUE BY COPY METHOD
…
Queuing by copy does not prevent the queue from also being used to
queue by reference.
For example, when the size of the data being queued makes it
impractical to copy the data into the queue, then a pointer to the data
can be copied into the queue instead.
The RTOS takes complete responsibility for allocating the memory
used to store data.
In a memory protected system, the RAM that a task can access will
be restricted. In that case queueing by reference could only be used if
the sending and receiving task
could both access the RAM in which the data was stored.
Queuing by copy does not impose that restriction; the kernel always
runs with full privileges, allowing a queue to
be used to pass data across memory protection boundaries. 15
ACCESS BY MULTIPLE TASKS
16
BLOCKING ON QUEUE READS
When a task attempts to read from a queue, it can optionally
specify a ‘block’ time.
This is the time the task will be kept in the Blocked state to
wait for data to be available from the queue,
should the queue already be empty.
A task that is in the Blocked state, waiting for data to
become available from a queue, is automatically moved to
the Ready state when another task or interrupt places data
into the queue.
The task will also be moved automatically from the
Blocked state to the Ready state if the specified block time
expires before data becomes available.
17
BLOCKING ON QUEUE READS
Queues can have multiple readers, so it is possible for a
single queue to have more than one task blocked on it
waiting for data.
When this is the case, only one task will be unblocked when
data becomes available.
The task that is unblocked will always be the highest priority
task that is waiting for data.
If the blocked tasks have equal priority, then the task that
has been waiting for data the longest will be unblocked.
18
BLOCKING ON QUEUE WRITES
Just as when reading from a queue, a task can optionally
specify a block time when writing to
a queue.
In this case, the block time is the maximum time the task
should be held in the Blocked state to wait for space to
become available on the queue, should the queue already be
full.
19
BLOCKING ON QUEUE WRITES
Queues can have multiple writers, so it is possible for a full
queue to have more than one task blocked on it waiting to
complete a send operation.
When this is the case, only one task will be unblocked when
space on the queue becomes available.
The task that is unblocked will always be the highest priority
task that is waiting for space.
If the blocked tasks have equal priority, then the task that has
been waiting for space the longest will be unblocked.
20
BLOCKING ON MULTIPLE QUEUE
Queues can be grouped into sets, allowing a task to enter the
Blocked state to wait for data to become available on any of
the queues in the set.
Queue sets are demonstrated in section 4.6, Receiving From
Multiple Queues.
21
4.3 USING A QUEUE
The xQueueCreate() API Function
A queue must be explicitly created before it can be used.
22
4.3 USING A QUEUE
The xQueueCreate() API Function
23
TABLE 18: XQUEUECREATE() API FUNCTION PARAMETERS AND RETURN VALUE
24
DIFFERENT APIS FOR QUEUE MANAGEMENT
26
XQUEUESENDTO… APIS
27
XQUEUESENDTO… APIS
28
XQUEUESENDTO… APIS
29
XQUEUESENDTO… APIS
30
THE XQUEUERECEIVE() API FUNCTION
xQueueReceive() is used to receive (read) an item from a
queue.
The item that is received is removed from the queue.
31
THE XQUEUERECEIVE() API FUNCTION
32
THE XQUEUERECEIVE() API FUNCTION
33
THE XQUEUERECEIVE() API FUNCTION
34
THE XQUEUERECEIVE() API FUNCTION
35
THE UXQUEUEMESSAGESWAITING() API FUNCTION
36
THE UXQUEUEMESSAGESWAITING() API FUNCTION
37
SIMPLE USE OF QUEUE B/W 2 TASKS ON ESP32
}
SIMPLE USE OF QUEUE B/W 2 TASKS
int valueFromQueue = 0;
40
SIMPLE USE OF QUEUE B/W 2 TASKS
/* Declaring a global variable of type QueueHandle_t */
QueueHandle_t integerQueue;
void setup() {
Serial.begin(112500); // Initialize the serial comm
delay(1000);
See complete code in
the “Notes” section of
integerQueue = xQueueCreate (3, // Queue length
this slide
sizeof(int) // Queue item size
);
if (integerQueue != NULL) {
43
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
44
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
The priority of the tasks that send to the queue are lower
than the priority of the task that receives from the queue.
This means the queue should never contain more than one
item because,
???
45
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
46
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
47
See next slide for remainder of code
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
48
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
50
See next slide for remainder of code
EX 10. BLOCKING WHEN RECEIVING FROM A QUEUE
51
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
52
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
53
See next slide for remainder of code
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
54
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
55
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
56
EXAMPLE 10. BLOCKING WHEN RECEIVING FROM A QUEUE
57
4.4 RECEIVING DATA FROM MULTIPLE SOURCES
58
4.4 RECEIVING DATA FROM MULTIPLE SOURCES
59
4.4 RECEIVING DATA FROM MULTIPLE SOURCES
60
4.4 RECEIVING DATA FROM MULTIPLE SOURCES
63
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
64
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
65
See next slide for remainder of code
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
66
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
69
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
70
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
71
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
Output ???
72
EX 11. WHEN RUN ON ESP32
Output ???
73
EX 11. WHEN RUN ON ESP32
Why?
Output ???
74
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
75
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
76
EX 11. BLOCKING WHEN SENDING TO A QUEUE, AND SENDING
STRUCTURES ON A QUEUE
77
STATIC KEYWORD IN C LANGUAGE
(1) A static variable inside a function keeps its value between
invocations of this function.
(2) A static function is "seen" only in the file it's declared in.
78
https://stackoverflow.com/questions/572547/what-does-static-mean-in-c
STATIC KEYWORD BEFORE A VARIABLE
Example Code:
79
https://stackoverflow.com/questions/572547/what-does-static-mean-in-c
STATIC KEYWORD BEFORE A VARIABLE
Output of example code:
80
https://stackoverflow.com/questions/572547/what-does-static-mean-in-c
STATIC KEYWORD BEFORE A VARIABLE
Usage:
This is useful for cases where a function needs to keep some
state between invocations, and you don't want to use global
variables.
Beware, however, this feature should be used very sparingly -
it makes your code not thread-safe and harder to understand.
81
https://stackoverflow.com/questions/572547/what-does-static-mean-in-c
STATIC KEYWORD BEFORE A FUNCTION
82
https://stackoverflow.com/questions/572547/what-does-static-mean-in-c
TYPEDEF ENUM EXPLANATION IN C
Question:
83
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Question (contd.):
84
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Answer:
85
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Answer (contd.):
86
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Answer (contd.):
87
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Answer 2:
88
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Answer 2 (contd.):
89
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c
TYPEDEF ENUM EXPLANATION IN C
Answer 2 (contd.):
90
https://stackoverflow.com/questions/34132439/typedef-enum-explanation-in-c