]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/BlockQ.c
Change version numbers to V4.1.2
[freertos] / Demo / Common / Full / BlockQ.c
1 /*\r
2         FreeRTOS.org V4.1.2 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /**\r
34  * Creates six tasks that operate on three queues as follows:\r
35  *\r
36  * The first two tasks send and receive an incrementing number to/from a queue.  \r
37  * One task acts as a producer and the other as the consumer.  The consumer is a \r
38  * higher priority than the producer and is set to block on queue reads.  The queue \r
39  * only has space for one item - as soon as the producer posts a message on the \r
40  * queue the consumer will unblock, pre-empt the producer, and remove the item.\r
41  * \r
42  * The second two tasks work the other way around.  Again the queue used only has\r
43  * enough space for one item.  This time the consumer has a lower priority than the \r
44  * producer.  The producer will try to post on the queue blocking when the queue is \r
45  * full.  When the consumer wakes it will remove the item from the queue, causing \r
46  * the producer to unblock, pre-empt the consumer, and immediately re-fill the \r
47  * queue.\r
48  * \r
49  * The last two tasks use the same queue producer and consumer functions.  This time the queue has\r
50  * enough space for lots of items and the tasks operate at the same priority.  The \r
51  * producer will execute, placing items into the queue.  The consumer will start \r
52  * executing when either the queue becomes full (causing the producer to block) or \r
53  * a context switch occurs (tasks of the same priority will time slice).\r
54  *\r
55  * \page BlockQC blockQ.c\r
56  * \ingroup DemoFiles\r
57  * <HR>\r
58  */\r
59 \r
60 /*\r
61 Changes from V1.00:\r
62         \r
63         + Reversed the priority and block times of the second two demo tasks so\r
64           they operate as per the description above.\r
65 \r
66 Changes from V2.0.0\r
67 \r
68         + Delay periods are now specified using variables and constants of\r
69           portTickType rather than unsigned portLONG.\r
70 \r
71 Changes from V4.0.2\r
72 \r
73         + The second set of tasks were created the wrong way around.  This has been\r
74           corrected.\r
75 */\r
76 \r
77 \r
78 #include <stdlib.h>\r
79 \r
80 /* Scheduler include files. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "queue.h"\r
84 \r
85 /* Demo program include files. */\r
86 #include "BlockQ.h"\r
87 #include "print.h"\r
88 \r
89 #define blckqSTACK_SIZE         ( ( unsigned portSHORT ) 128 )\r
90 #define blckqNUM_TASK_SETS      ( 3 )\r
91 \r
92 /* Structure used to pass parameters to the blocking queue tasks. */\r
93 typedef struct BLOCKING_QUEUE_PARAMETERS\r
94 {\r
95         xQueueHandle xQueue;                                    /*< The queue to be used by the task. */\r
96         portTickType xBlockTime;                        /*< The block time to use on queue reads/writes. */\r
97         volatile portSHORT *psCheckVariable;    /*< Incremented on each successful cycle to check the task is still running. */\r
98 } xBlockingQueueParameters;\r
99 \r
100 /* Task function that creates an incrementing number and posts it on a queue. */\r
101 static void vBlockingQueueProducer( void *pvParameters );\r
102 \r
103 /* Task function that removes the incrementing number from a queue and checks that \r
104 it is the expected number. */\r
105 static void vBlockingQueueConsumer( void *pvParameters );\r
106 \r
107 /* Variables which are incremented each time an item is removed from a queue, and \r
108 found to be the expected value. \r
109 These are used to check that the tasks are still running. */\r
110 static volatile portSHORT sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 };\r
111 \r
112 /* Variable which are incremented each time an item is posted on a queue.   These \r
113 are used to check that the tasks are still running. */\r
114 static volatile portSHORT sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 };\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )\r
119 {\r
120 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;\r
121 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;\r
122 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;\r
123 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;\r
124 const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;\r
125 const portTickType xDontBlock = ( portTickType ) 0;\r
126 \r
127         /* Create the first two tasks as described at the top of the file. */ \r
128         \r
129         /* First create the structure used to pass parameters to the consumer tasks. */\r
130         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
131 \r
132         /* Create the queue used by the first two tasks to pass the incrementing number.  \r
133         Pass a pointer to the queue in the parameter structure. */\r
134         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
135 \r
136         /* The consumer is created first so gets a block time as described above. */\r
137         pxQueueParameters1->xBlockTime = xBlockTime;\r
138 \r
139         /* Pass in the variable that this task is going to increment so we can check it \r
140         is still running. */\r
141         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );\r
142                 \r
143         /* Create the structure used to pass parameters to the producer task. */\r
144         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
145 \r
146         /* Pass the queue to this task also, using the parameter structure. */\r
147         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;\r
148 \r
149         /* The producer is not going to block - as soon as it posts the consumer will \r
150         wake and remove the item so the producer should always have room to post. */\r
151         pxQueueParameters2->xBlockTime = xDontBlock;\r
152 \r
153         /* Pass in the variable that this task is going to increment so we can check \r
154         it is still running. */\r
155         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );\r
156 \r
157 \r
158         /* Note the producer has a lower priority than the consumer when the tasks are \r
159         spawned. */\r
160         xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );\r
161         xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );\r
162 \r
163         \r
164 \r
165         /* Create the second two tasks as described at the top of the file.   This uses \r
166         the same mechanism but reverses the task priorities. */\r
167 \r
168         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
169         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
170         pxQueueParameters3->xBlockTime = xDontBlock;\r
171         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );\r
172 \r
173         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
174         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;\r
175         pxQueueParameters4->xBlockTime = xBlockTime;\r
176         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );\r
177 \r
178         xTaskCreate( vBlockingQueueProducer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );\r
179         xTaskCreate( vBlockingQueueConsumer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );\r
180 \r
181 \r
182 \r
183         /* Create the last two tasks as described above.  The mechanism is again just \r
184         the same.  This time both parameter structures are given a block time. */\r
185         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
186         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
187         pxQueueParameters5->xBlockTime = xBlockTime;\r
188         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );\r
189 \r
190         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
191         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;\r
192         pxQueueParameters6->xBlockTime = xBlockTime;\r
193         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] ); \r
194 \r
195         xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );\r
196         xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static void vBlockingQueueProducer( void *pvParameters )\r
201 {\r
202 unsigned portSHORT usValue = 0;\r
203 xBlockingQueueParameters *pxQueueParameters;\r
204 const portCHAR * const pcTaskStartMsg = "Blocking queue producer started.\r\n";\r
205 const portCHAR * const pcTaskErrorMsg = "Could not post on blocking queue\r\n";\r
206 portSHORT sErrorEverOccurred = pdFALSE;\r
207 \r
208         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
209 \r
210         /* Queue a message for printing to say the task has started. */\r
211         vPrintDisplayMessage( &pcTaskStartMsg );\r
212 \r
213         for( ;; )\r
214         {               \r
215                 if( xQueueSend( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )\r
216                 {\r
217                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
218                         sErrorEverOccurred = pdTRUE;\r
219                 }\r
220                 else\r
221                 {\r
222                         /* We have successfully posted a message, so increment the variable \r
223                         used to check we are still running. */\r
224                         if( sErrorEverOccurred == pdFALSE )\r
225                         {\r
226                                 ( *pxQueueParameters->psCheckVariable )++;\r
227                         }\r
228 \r
229                         /* Increment the variable we are going to post next time round.  The \r
230                         consumer will expect the numbers to     follow in numerical order. */\r
231                         ++usValue;\r
232                 }\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void vBlockingQueueConsumer( void *pvParameters )\r
238 {\r
239 unsigned portSHORT usData, usExpectedValue = 0;\r
240 xBlockingQueueParameters *pxQueueParameters;\r
241 const portCHAR * const pcTaskStartMsg = "Blocking queue consumer started.\r\n";\r
242 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on blocking queue.\r\n";\r
243 portSHORT sErrorEverOccurred = pdFALSE;\r
244 \r
245         /* Queue a message for printing to say the task has started. */\r
246         vPrintDisplayMessage( &pcTaskStartMsg );\r
247 \r
248         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
249 \r
250         for( ;; )\r
251         {       \r
252                 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )\r
253                 {\r
254                         if( usData != usExpectedValue )\r
255                         {\r
256                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
257 \r
258                                 /* Catch-up. */\r
259                                 usExpectedValue = usData;\r
260 \r
261                                 sErrorEverOccurred = pdTRUE;\r
262                         }\r
263                         else\r
264                         {\r
265                                 /* We have successfully received a message, so increment the \r
266                                 variable used to check we are still running. */ \r
267                                 if( sErrorEverOccurred == pdFALSE )\r
268                                 {\r
269                                         ( *pxQueueParameters->psCheckVariable )++;\r
270                                 }\r
271                                                         \r
272                                 /* Increment the value we expect to remove from the queue next time \r
273                                 round. */\r
274                                 ++usExpectedValue;\r
275                         }                       \r
276                 }               \r
277         }\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 /* This is called to check that all the created tasks are still running. */\r
282 portBASE_TYPE xAreBlockingQueuesStillRunning( void )\r
283 {\r
284 static portSHORT sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 };\r
285 static portSHORT sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 };\r
286 portBASE_TYPE xReturn = pdPASS, xTasks;\r
287 \r
288         /* Not too worried about mutual exclusion on these variables as they are 16 \r
289         bits and we are only reading them. We also only care to see if they have \r
290         changed or not.\r
291         \r
292         Loop through each check variable and return pdFALSE if any are found not \r
293         to have changed since the last call. */\r
294 \r
295         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )\r
296         {\r
297                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )\r
298                 {\r
299                         xReturn = pdFALSE;\r
300                 }\r
301                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];\r
302 \r
303 \r
304                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )\r
305                 {\r
306                         xReturn = pdFALSE;\r
307                 }\r
308                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];\r
309         }\r
310 \r
311         return xReturn;\r
312 }\r
313 \r