]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/PollQ.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / Common / Minimal / PollQ.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * This version of PollQ. c is for use on systems that have limited stack\r
67  * space and no display facilities.  The complete version can be found in\r
68  * the Demo/Common/Full directory.\r
69  *\r
70  * Creates two tasks that communicate over a single queue.  One task acts as a\r
71  * producer, the other a consumer.\r
72  *\r
73  * The producer loops for three iteration, posting an incrementing number onto the\r
74  * queue each cycle.  It then delays for a fixed period before doing exactly the\r
75  * same again.\r
76  *\r
77  * The consumer loops emptying the queue.  Each item removed from the queue is\r
78  * checked to ensure it contains the expected value.  When the queue is empty it\r
79  * blocks for a fixed period, then does the same again.\r
80  *\r
81  * All queue access is performed without blocking.  The consumer completely empties\r
82  * the queue each time it runs so the producer should never find the queue full.\r
83  *\r
84  * An error is flagged if the consumer obtains an unexpected value or the producer\r
85  * find the queue is full.\r
86  */\r
87 \r
88 /*\r
89 Changes from V2.0.0\r
90 \r
91         + Delay periods are now specified using variables and constants of\r
92           portTickType rather than unsigned long.\r
93 */\r
94 \r
95 #include <stdlib.h>\r
96 \r
97 /* Scheduler include files. */\r
98 #include "FreeRTOS.h"\r
99 #include "task.h"\r
100 #include "queue.h"\r
101 \r
102 /* Demo program include files. */\r
103 #include "PollQ.h"\r
104 \r
105 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
106 #define pollqQUEUE_SIZE                 ( 10 )\r
107 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )\r
108 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )\r
109 #define pollqNO_DELAY                   ( ( portTickType ) 0 )\r
110 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )\r
111 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )\r
112 \r
113 /* The task that posts the incrementing number onto the queue. */\r
114 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
115 \r
116 /* The task that empties the queue. */\r
117 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
118 \r
119 /* Variables that are used to check that the tasks are still running with no\r
120 errors. */\r
121 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
126 {\r
127 static xQueueHandle xPolledQueue;\r
128 \r
129         /* Create the queue used by the producer and consumer. */\r
130         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
131 \r
132         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
133         in use.  The queue registry is provided as a means for kernel aware \r
134         debuggers to locate queues and has no purpose if a kernel aware debugger\r
135         is not being used.  The call to vQueueAddToRegistry() will be removed\r
136         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
137         defined to be less than 1. */\r
138         vQueueAddToRegistry( xPolledQueue, ( signed char * ) "Poll_Test_Queue" );\r
139 \r
140         /* Spawn the producer and consumer. */\r
141         xTaskCreate( vPolledQueueConsumer, ( signed char * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
142         xTaskCreate( vPolledQueueProducer, ( signed char * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
147 {\r
148 unsigned short usValue = ( unsigned short ) 0;\r
149 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
150 \r
151         for( ;; )\r
152         {               \r
153                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
154                 {\r
155                         /* Send an incrementing number on the queue without blocking. */\r
156                         if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
157                         {\r
158                                 /* We should never find the queue full so if we get here there\r
159                                 has been an error. */\r
160                                 xError = pdTRUE;\r
161                         }\r
162                         else\r
163                         {\r
164                                 if( xError == pdFALSE )\r
165                                 {\r
166                                         /* If an error has ever been recorded we stop incrementing the\r
167                                         check variable. */\r
168                                         portENTER_CRITICAL();\r
169                                                 xPollingProducerCount++;\r
170                                         portEXIT_CRITICAL();\r
171                                 }\r
172 \r
173                                 /* Update the value we are going to post next time around. */\r
174                                 usValue++;\r
175                         }\r
176                 }\r
177 \r
178                 /* Wait before we start posting again to ensure the consumer runs and\r
179                 empties the queue. */\r
180                 vTaskDelay( pollqPRODUCER_DELAY );\r
181         }\r
182 }  /*lint !e818 Function prototype must conform to API. */\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
186 {\r
187 unsigned short usData, usExpectedValue = ( unsigned short ) 0;\r
188 signed portBASE_TYPE xError = pdFALSE;\r
189 \r
190         for( ;; )\r
191         {               \r
192                 /* Loop until the queue is empty. */\r
193                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
194                 {\r
195                         if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
196                         {\r
197                                 if( usData != usExpectedValue )\r
198                                 {\r
199                                         /* This is not what we expected to receive so an error has\r
200                                         occurred. */\r
201                                         xError = pdTRUE;\r
202 \r
203                                         /* Catch-up to the value we received so our next expected\r
204                                         value should again be correct. */\r
205                                         usExpectedValue = usData;\r
206                                 }\r
207                                 else\r
208                                 {\r
209                                         if( xError == pdFALSE )\r
210                                         {\r
211                                                 /* Only increment the check variable if no errors have\r
212                                                 occurred. */\r
213                                                 portENTER_CRITICAL();\r
214                                                         xPollingConsumerCount++;\r
215                                                 portEXIT_CRITICAL();\r
216                                         }\r
217                                 }\r
218 \r
219                                 /* Next time round we would expect the number to be one higher. */\r
220                                 usExpectedValue++;\r
221                         }\r
222                 }\r
223 \r
224                 /* Now the queue is empty we block, allowing the producer to place more\r
225                 items in the queue. */\r
226                 vTaskDelay( pollqCONSUMER_DELAY );\r
227         }\r
228 } /*lint !e818 Function prototype must conform to API. */\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 /* This is called to check that all the created tasks are still running with no errors. */\r
232 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
233 {\r
234 portBASE_TYPE xReturn;\r
235 \r
236         /* Check both the consumer and producer poll count to check they have both\r
237         been changed since out last trip round.  We do not need a critical section\r
238         around the check variables as this is called from a higher priority than\r
239         the other tasks that access the same variables. */\r
240         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
241                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
242           )\r
243         {\r
244                 xReturn = pdFALSE;\r
245         }\r
246         else\r
247         {\r
248                 xReturn = pdTRUE;\r
249         }\r
250 \r
251         /* Set the check variables back down so we know if they have been\r
252         incremented the next time around. */\r
253         xPollingConsumerCount = pollqINITIAL_VALUE;\r
254         xPollingProducerCount = pollqINITIAL_VALUE;\r
255 \r
256         return xReturn;\r
257 }\r