root/webserver/example/freeRTOS/Demo/Common/Minimal/AltBlckQ.c

Revision 14, 13.3 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

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