root/webserver/example/freeRTOSexample/Demo/Common/Minimal/BlockQ.c

Revision 14, 12.8 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  * Creates six tasks that operate on three queues as follows:
50  *
51  * The first two tasks send and receive an incrementing number to/from a queue.
52  * One task acts as a producer and the other as the consumer.  The consumer is a
53  * higher priority than the producer and is set to block on queue reads.  The queue
54  * only has space for one item - as soon as the producer posts a message on the
55  * queue the consumer will unblock, pre-empt the producer, and remove the item.
56  *
57  * The second two tasks work the other way around.  Again the queue used only has
58  * enough space for one item.  This time the consumer has a lower priority than the
59  * producer.  The producer will try to post on the queue blocking when the queue is
60  * full.  When the consumer wakes it will remove the item from the queue, causing
61  * the producer to unblock, pre-empt the consumer, and immediately re-fill the
62  * queue.
63  *
64  * The last two tasks use the same queue producer and consumer functions.  This time the queue has
65  * enough space for lots of items and the tasks operate at the same priority.  The
66  * producer will execute, placing items into the queue.  The consumer will start
67  * executing when either the queue becomes full (causing the producer to block) or
68  * a context switch occurs (tasks of the same priority will time slice).
69  *
70  */
71
72 /*
73
74 Changes from V4.1.1
75
76         + The second set of tasks were created the wrong way around.  This has been
77           corrected.
78 */
79
80
81 #include <stdlib.h>
82
83 /* Scheduler include files. */
84 #include "FreeRTOS.h"
85 #include "task.h"
86 #include "queue.h"
87
88 /* Demo program include files. */
89 #include "BlockQ.h"
90
91 #define blckqSTACK_SIZE         configMINIMAL_STACK_SIZE
92 #define blckqNUM_TASK_SETS      ( 3 )
93
94 /* Structure used to pass parameters to the blocking queue tasks. */
95 typedef struct BLOCKING_QUEUE_PARAMETERS
96 {
97         xQueueHandle xQueue;                                    /*< The queue to be used by the task. */
98         portTickType xBlockTime;                                /*< The block time to use on queue reads/writes. */
99         volatile portSHORT *psCheckVariable;    /*< Incremented on each successful cycle to check the task is still running. */
100 } xBlockingQueueParameters;
101
102 /* Task function that creates an incrementing number and posts it on a queue. */
103 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );
104
105 /* Task function that removes the incrementing number from a queue and checks that
106 it is the expected number. */
107 static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );
108
109 /* Variables which are incremented each time an item is removed from a queue, and
110 found to be the expected value.
111 These are used to check that the tasks are still running. */
112 static volatile portSHORT sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
113
114 /* Variable which are incremented each time an item is posted on a queue.   These
115 are used to check that the tasks are still running. */
116 static volatile portSHORT sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
117
118 /*-----------------------------------------------------------*/
119
120 void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )
121 {
122 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;
123 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;
124 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;
125 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;
126 const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;
127 const portTickType xDontBlock = ( portTickType ) 0;
128
129         /* Create the first two tasks as described at the top of the file. */
130        
131         /* First create the structure used to pass parameters to the consumer tasks. */
132         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
133
134         /* Create the queue used by the first two tasks to pass the incrementing number.
135         Pass a pointer to the queue in the parameter structure. */
136         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
137
138         /* The consumer is created first so gets a block time as described above. */
139         pxQueueParameters1->xBlockTime = xBlockTime;
140
141         /* Pass in the variable that this task is going to increment so we can check it
142         is still running. */
143         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );
144                
145         /* Create the structure used to pass parameters to the producer task. */
146         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
147
148         /* Pass the queue to this task also, using the parameter structure. */
149         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
150
151         /* The producer is not going to block - as soon as it posts the consumer will
152         wake and remove the item so the producer should always have room to post. */
153         pxQueueParameters2->xBlockTime = xDontBlock;
154
155         /* Pass in the variable that this task is going to increment so we can check
156         it is still running. */
157         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );
158
159
160         /* Note the producer has a lower priority than the consumer when the tasks are
161         spawned. */
162         xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
163         xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
164
165        
166
167         /* Create the second two tasks as described at the top of the file.   This uses
168         the same mechanism but reverses the task priorities. */
169
170         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
171         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
172         pxQueueParameters3->xBlockTime = xDontBlock;
173         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );
174
175         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
176         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
177         pxQueueParameters4->xBlockTime = xBlockTime;
178         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
179
180         xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
181         xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
182
183
184
185         /* Create the last two tasks as described above.  The mechanism is again just
186         the same.  This time both parameter structures are given a block time. */
187         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
188         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
189         pxQueueParameters5->xBlockTime = xBlockTime;
190         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );
191
192         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
193         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
194         pxQueueParameters6->xBlockTime = xBlockTime;
195         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
196
197         xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );
198         xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );
199 }
200 /*-----------------------------------------------------------*/
201
202 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )
203 {
204 unsigned portSHORT usValue = 0;
205 xBlockingQueueParameters *pxQueueParameters;
206 portSHORT sErrorEverOccurred = pdFALSE;
207
208         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
209
210         for( ;; )
211         {               
212                 if( xQueueSend( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
213                 {
214                         sErrorEverOccurred = pdTRUE;
215                 }
216                 else
217                 {
218                         /* We have successfully posted a message, so increment the variable
219                         used to check we are still running. */
220                         if( sErrorEverOccurred == pdFALSE )
221                         {
222                                 ( *pxQueueParameters->psCheckVariable )++;
223                         }
224
225                         /* Increment the variable we are going to post next time round.  The
226                         consumer will expect the numbers to     follow in numerical order. */
227                         ++usValue;
228                 }
229         }
230 }
231 /*-----------------------------------------------------------*/
232
233 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )
234 {
235 unsigned portSHORT usData, usExpectedValue = 0;
236 xBlockingQueueParameters *pxQueueParameters;
237 portSHORT sErrorEverOccurred = pdFALSE;
238
239         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
240
241         for( ;; )
242         {       
243                 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
244                 {
245                         if( usData != usExpectedValue )
246                         {
247                                 /* Catch-up. */
248                                 usExpectedValue = usData;
249
250                                 sErrorEverOccurred = pdTRUE;
251                         }
252                         else
253                         {
254                                 /* We have successfully received a message, so increment the
255                                 variable used to check we are still running. */
256                                 if( sErrorEverOccurred == pdFALSE )
257                                 {
258                                         ( *pxQueueParameters->psCheckVariable )++;
259                                 }
260                                                        
261                                 /* Increment the value we expect to remove from the queue next time
262                                 round. */
263                                 ++usExpectedValue;
264                         }                       
265                 }               
266         }
267 }
268 /*-----------------------------------------------------------*/
269
270 /* This is called to check that all the created tasks are still running. */
271 portBASE_TYPE xAreBlockingQueuesStillRunning( void )
272 {
273 static portSHORT sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
274 static portSHORT sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };
275 portBASE_TYPE xReturn = pdPASS, xTasks;
276
277         /* Not too worried about mutual exclusion on these variables as they are 16
278         bits and we are only reading them. We also only care to see if they have
279         changed or not.
280        
281         Loop through each check variable to and return pdFALSE if any are found not
282         to have changed since the last call. */
283
284         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
285         {
286                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )
287                 {
288                         xReturn = pdFALSE;
289                 }
290                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
291
292
293                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )
294                 {
295                         xReturn = pdFALSE;
296                 }
297                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
298         }
299
300         return xReturn;
301 }
302
Note: See TracBrowser for help on using the browser.