]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/BlockQ.c
b3ea3a0571cf3dfe891d0f1f883aa2a31a652547
[freertos] / FreeRTOS / Demo / Common / Full / BlockQ.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /**\r
29  * Creates six tasks that operate on three queues as follows:\r
30  *\r
31  * The first two tasks send and receive an incrementing number to/from a queue.  \r
32  * One task acts as a producer and the other as the consumer.  The consumer is a \r
33  * higher priority than the producer and is set to block on queue reads.  The queue \r
34  * only has space for one item - as soon as the producer posts a message on the \r
35  * queue the consumer will unblock, pre-empt the producer, and remove the item.\r
36  * \r
37  * The second two tasks work the other way around.  Again the queue used only has\r
38  * enough space for one item.  This time the consumer has a lower priority than the \r
39  * producer.  The producer will try to post on the queue blocking when the queue is \r
40  * full.  When the consumer wakes it will remove the item from the queue, causing \r
41  * the producer to unblock, pre-empt the consumer, and immediately re-fill the \r
42  * queue.\r
43  * \r
44  * The last two tasks use the same queue producer and consumer functions.  This time the queue has\r
45  * enough space for lots of items and the tasks operate at the same priority.  The \r
46  * producer will execute, placing items into the queue.  The consumer will start \r
47  * executing when either the queue becomes full (causing the producer to block) or \r
48  * a context switch occurs (tasks of the same priority will time slice).\r
49  *\r
50  * \page BlockQC blockQ.c\r
51  * \ingroup DemoFiles\r
52  * <HR>\r
53  */\r
54 \r
55 /*\r
56 Changes from V1.00:\r
57         \r
58         + Reversed the priority and block times of the second two demo tasks so\r
59           they operate as per the description above.\r
60 \r
61 Changes from V2.0.0\r
62 \r
63         + Delay periods are now specified using variables and constants of\r
64           TickType_t rather than unsigned long.\r
65 \r
66 Changes from V4.0.2\r
67 \r
68         + The second set of tasks were created the wrong way around.  This has been\r
69           corrected.\r
70 */\r
71 \r
72 \r
73 #include <stdlib.h>\r
74 \r
75 /* Scheduler include files. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 #include "queue.h"\r
79 \r
80 /* Demo program include files. */\r
81 #include "BlockQ.h"\r
82 #include "print.h"\r
83 \r
84 #define blckqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
85 #define blckqNUM_TASK_SETS      ( 3 )\r
86 \r
87 /* Structure used to pass parameters to the blocking queue tasks. */\r
88 typedef struct BLOCKING_QUEUE_PARAMETERS\r
89 {\r
90         QueueHandle_t xQueue;                                   /*< The queue to be used by the task. */\r
91         TickType_t xBlockTime;                  /*< The block time to use on queue reads/writes. */\r
92         volatile short *psCheckVariable;        /*< Incremented on each successful cycle to check the task is still running. */\r
93 } xBlockingQueueParameters;\r
94 \r
95 /* Task function that creates an incrementing number and posts it on a queue. */\r
96 static void vBlockingQueueProducer( void *pvParameters );\r
97 \r
98 /* Task function that removes the incrementing number from a queue and checks that \r
99 it is the expected number. */\r
100 static void vBlockingQueueConsumer( void *pvParameters );\r
101 \r
102 /* Variables which are incremented each time an item is removed from a queue, and \r
103 found to be the expected value. \r
104 These are used to check that the tasks are still running. */\r
105 static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
106 \r
107 /* Variable which are incremented each time an item is posted on a queue.   These \r
108 are used to check that the tasks are still running. */\r
109 static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )\r
114 {\r
115 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;\r
116 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;\r
117 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;\r
118 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;\r
119 const TickType_t xBlockTime = ( TickType_t ) 1000 / portTICK_PERIOD_MS;\r
120 const TickType_t xDontBlock = ( TickType_t ) 0;\r
121 \r
122         /* Create the first two tasks as described at the top of the file. */ \r
123         \r
124         /* First create the structure used to pass parameters to the consumer tasks. */\r
125         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
126 \r
127         /* Create the queue used by the first two tasks to pass the incrementing number.  \r
128         Pass a pointer to the queue in the parameter structure. */\r
129         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
130 \r
131         /* The consumer is created first so gets a block time as described above. */\r
132         pxQueueParameters1->xBlockTime = xBlockTime;\r
133 \r
134         /* Pass in the variable that this task is going to increment so we can check it \r
135         is still running. */\r
136         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );\r
137                 \r
138         /* Create the structure used to pass parameters to the producer task. */\r
139         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
140 \r
141         /* Pass the queue to this task also, using the parameter structure. */\r
142         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;\r
143 \r
144         /* The producer is not going to block - as soon as it posts the consumer will \r
145         wake and remove the item so the producer should always have room to post. */\r
146         pxQueueParameters2->xBlockTime = xDontBlock;\r
147 \r
148         /* Pass in the variable that this task is going to increment so we can check \r
149         it is still running. */\r
150         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );\r
151 \r
152 \r
153         /* Note the producer has a lower priority than the consumer when the tasks are \r
154         spawned. */\r
155         xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );\r
156         xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );\r
157 \r
158         \r
159 \r
160         /* Create the second two tasks as described at the top of the file.   This uses \r
161         the same mechanism but reverses the task priorities. */\r
162 \r
163         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
164         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
165         pxQueueParameters3->xBlockTime = xDontBlock;\r
166         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );\r
167 \r
168         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
169         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;\r
170         pxQueueParameters4->xBlockTime = xBlockTime;\r
171         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );\r
172 \r
173         xTaskCreate( vBlockingQueueProducer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );\r
174         xTaskCreate( vBlockingQueueConsumer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );\r
175 \r
176 \r
177 \r
178         /* Create the last two tasks as described above.  The mechanism is again just \r
179         the same.  This time both parameter structures are given a block time. */\r
180         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
181         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
182         pxQueueParameters5->xBlockTime = xBlockTime;\r
183         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );\r
184 \r
185         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
186         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;\r
187         pxQueueParameters6->xBlockTime = xBlockTime;\r
188         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] ); \r
189 \r
190         xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );\r
191         xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void vBlockingQueueProducer( void *pvParameters )\r
196 {\r
197 unsigned short usValue = 0;\r
198 xBlockingQueueParameters *pxQueueParameters;\r
199 const char * const pcTaskStartMsg = "Blocking queue producer started.\r\n";\r
200 const char * const pcTaskErrorMsg = "Could not post on blocking queue\r\n";\r
201 short sErrorEverOccurred = pdFALSE;\r
202 \r
203         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
204 \r
205         /* Queue a message for printing to say the task has started. */\r
206         vPrintDisplayMessage( &pcTaskStartMsg );\r
207 \r
208         for( ;; )\r
209         {               \r
210                 if( xQueueSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )\r
211                 {\r
212                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
213                         sErrorEverOccurred = pdTRUE;\r
214                 }\r
215                 else\r
216                 {\r
217                         /* We have successfully posted a message, so increment the variable \r
218                         used to check we are still running. */\r
219                         if( sErrorEverOccurred == pdFALSE )\r
220                         {\r
221                                 ( *pxQueueParameters->psCheckVariable )++;\r
222                         }\r
223 \r
224                         /* Increment the variable we are going to post next time round.  The \r
225                         consumer will expect the numbers to     follow in numerical order. */\r
226                         ++usValue;\r
227                 }\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void vBlockingQueueConsumer( void *pvParameters )\r
233 {\r
234 unsigned short usData, usExpectedValue = 0;\r
235 xBlockingQueueParameters *pxQueueParameters;\r
236 const char * const pcTaskStartMsg = "Blocking queue consumer started.\r\n";\r
237 const char * const pcTaskErrorMsg = "Incorrect value received on blocking queue.\r\n";\r
238 short sErrorEverOccurred = pdFALSE;\r
239 \r
240         /* Queue a message for printing to say the task has started. */\r
241         vPrintDisplayMessage( &pcTaskStartMsg );\r
242 \r
243         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
244 \r
245         for( ;; )\r
246         {       \r
247                 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )\r
248                 {\r
249                         if( usData != usExpectedValue )\r
250                         {\r
251                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
252 \r
253                                 /* Catch-up. */\r
254                                 usExpectedValue = usData;\r
255 \r
256                                 sErrorEverOccurred = pdTRUE;\r
257                         }\r
258                         else\r
259                         {\r
260                                 /* We have successfully received a message, so increment the \r
261                                 variable used to check we are still running. */ \r
262                                 if( sErrorEverOccurred == pdFALSE )\r
263                                 {\r
264                                         ( *pxQueueParameters->psCheckVariable )++;\r
265                                 }\r
266                                                         \r
267                                 /* Increment the value we expect to remove from the queue next time \r
268                                 round. */\r
269                                 ++usExpectedValue;\r
270                         }                       \r
271                 }               \r
272         }\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 /* This is called to check that all the created tasks are still running. */\r
277 portBASE_TYPE xAreBlockingQueuesStillRunning( void )\r
278 {\r
279 static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
280 static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
281 portBASE_TYPE xReturn = pdPASS, xTasks;\r
282 \r
283         /* Not too worried about mutual exclusion on these variables as they are 16 \r
284         bits and we are only reading them. We also only care to see if they have \r
285         changed or not.\r
286         \r
287         Loop through each check variable and return pdFALSE if any are found not \r
288         to have changed since the last call. */\r
289 \r
290         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )\r
291         {\r
292                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )\r
293                 {\r
294                         xReturn = pdFALSE;\r
295                 }\r
296                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];\r
297 \r
298 \r
299                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )\r
300                 {\r
301                         xReturn = pdFALSE;\r
302                 }\r
303                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];\r
304         }\r
305 \r
306         return xReturn;\r
307 }\r
308 \r