]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/event_groups.c
Continue work on new event groups functionality - fixups required by test results.
[freertos] / FreeRTOS / Source / event_groups.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdlib.h>\r
68 \r
69 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
70 all the API functions to use the MPU wrappers.  That should only be done when\r
71 task.h is included from an application file. */\r
72 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
73 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "timers.h"\r
78 #include "event_groups.h"\r
79 \r
80 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
81 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
82 header files above, but not in this file, in order to generate the correct\r
83 privileged Vs unprivileged linkage and placement. */\r
84 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */\r
85 \r
86 #if ( INCLUDE_xEventGroupSetBitFromISR == 1 ) && ( configUSE_TIMERS == 0 )\r
87         #error configUSE_TIMERS must be set to 1 to make the xEventGroupSetBitFromISR() function available.\r
88 #endif\r
89 \r
90 #if ( INCLUDE_xEventGroupSetBitFromISR == 1 ) && ( INCLUDE_xTimerPendCallbackFromISR == 0 )\r
91         #error INCLUDE_xTimerPendCallbackFromISR must also be set to one to make the xEventGroupSetBitFromISR() function available.\r
92 #endif\r
93 \r
94 \r
95 #if configUSE_16_BIT_TICKS == 1\r
96         #define taskCLEAR_EVENTS_ON_EXIT_BIT    0x0100U\r
97         #define taskUNBLOCKED_DUE_TO_BIT_SET    0x0200U\r
98         #define taskWAIT_FOR_ALL_BITS                   0x0400U\r
99         #define taskEVENT_BITS_CONTROL_BYTES    0xff00U\r
100 #else\r
101         #define taskCLEAR_EVENTS_ON_EXIT_BIT    0x01000000UL\r
102         #define taskUNBLOCKED_DUE_TO_BIT_SET    0x02000000UL\r
103         #define taskWAIT_FOR_ALL_BITS                   0x04000000UL\r
104         #define taskEVENT_BITS_CONTROL_BYTES    0xff000000UL\r
105 #endif\r
106 \r
107 typedef struct EventBitsDefinition\r
108 {\r
109         xEventBitsType uxEventBits;\r
110         xList xTasksWaitingForBits;             /*< List of tasks waiting for a bit to be set. */\r
111 } xEVENT_BITS;\r
112 \r
113 /* Used internally only. */\r
114 typedef struct EVENT_GROUP_CALLBACK_PARAMTERS\r
115 {\r
116         xEventGroupHandle xTargetEventGroup;\r
117         xEventBitsType xBitsToSet;\r
118 } xEventGroupCallbackParameters;\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 xEventGroupHandle xEventGroupCreate( void )\r
123 {\r
124 xEVENT_BITS *pxEventBits;\r
125 \r
126         pxEventBits = pvPortMalloc( sizeof( xEVENT_BITS ) );\r
127         if( pxEventBits != NULL )\r
128         {\r
129                 pxEventBits->uxEventBits = 0;\r
130                 vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );\r
131         }\r
132 \r
133         return ( xEventGroupHandle ) pxEventBits;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet, xEventBitsType uxBitsToWaitFor, portTickType xTicksToWait )\r
138 {\r
139 xEventBitsType uxOriginalBitValue, uxReturn;\r
140 xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
141 portBASE_TYPE xYieldedAlready;\r
142 \r
143         #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
144         {\r
145                 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
146         }\r
147         #endif\r
148 \r
149         vTaskSuspendAll();\r
150         {\r
151                 uxOriginalBitValue = pxEventBits->uxEventBits;\r
152 \r
153                 ( void ) xEventGroupSetBits( xEventGroup, uxBitsToSet );\r
154 \r
155                 if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
156                 {\r
157                         /* All the rendezvous bits will have been set once this task set\r
158                         its bits - no need to block. */\r
159                         uxReturn = ( uxOriginalBitValue | uxBitsToSet );\r
160 \r
161                         /* Rendezvous always clear the bits.  They will have been cleared\r
162                         already unless this is the only task in the rendezvous. */\r
163                         pxEventBits->uxEventBits &= uxBitsToWaitFor;\r
164 \r
165                         xTicksToWait = 0;\r
166                 }\r
167                 else\r
168                 {\r
169                         if( xTicksToWait != ( portTickType ) 0 )\r
170                         {\r
171                                 /* Store the bits that the calling task is waiting for in the\r
172                                 task's event list item so the kernel knows when a match is\r
173                                 found.  Then enter the blocked state. */\r
174                                 vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | taskCLEAR_EVENTS_ON_EXIT_BIT | taskWAIT_FOR_ALL_BITS ), xTicksToWait );\r
175                         }\r
176                         else\r
177                         {\r
178                                 /* The rendezvous bits were not set, but no block time was\r
179                                 specified - just return the current event bit value. */\r
180                                 uxReturn = pxEventBits->uxEventBits;\r
181                         }\r
182                 }\r
183         }\r
184         xYieldedAlready = xTaskResumeAll();\r
185 \r
186         if( xTicksToWait != ( portTickType ) 0 )\r
187         {\r
188                 if( xYieldedAlready == pdFALSE )\r
189                 {\r
190                         portYIELD_WITHIN_API();\r
191                 }\r
192 \r
193                 /* The task blocked to wait for its required bits to be set - at this\r
194                 point either the required bits were set or the block time expired.  If\r
195                 the required bits were set they will have been stored in the task's\r
196                 event list item, and they should now be retrieved then cleared. */\r
197                 uxReturn = uxTaskResetEventItemValue();\r
198 \r
199                 if( ( uxReturn & taskUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 )\r
200                 {\r
201                         /* The task timed out, just return the current event bit value. */\r
202                         uxReturn = pxEventBits->uxEventBits;\r
203                 }\r
204                 else\r
205                 {\r
206                         /* The task unblocked because the bits were set.  Clear the control\r
207                         bits before returning the value. */\r
208                         uxReturn &= ~taskEVENT_BITS_CONTROL_BYTES;\r
209                 }\r
210         }\r
211 \r
212         return uxReturn;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToWaitFor, portBASE_TYPE xClearOnExit, portBASE_TYPE xWaitForAllBits, portTickType xTicksToWait )\r
217 {\r
218 xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
219 xEventBitsType uxReturn, uxControlBits = 0;\r
220 \r
221         /* Check the user is not attempting to wait on the bits used by the kernel\r
222         itself, and that at least one bit is being requested. */\r
223         configASSERT( ( uxBitsToWaitFor & taskEVENT_BITS_CONTROL_BYTES ) == 0 );\r
224         configASSERT( uxBitsToWaitFor != 0 );\r
225         #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
226         {\r
227                 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
228         }\r
229         #endif\r
230 \r
231         taskENTER_CRITICAL();\r
232         {\r
233                 const xEventBitsType uxCurrentEventBits = pxEventBits->uxEventBits;\r
234 \r
235                 if( xWaitForAllBits == pdFALSE )\r
236                 {\r
237                         /* Task only has to wait for one bit within uxBitsToWaitFor to be set.  Is\r
238                         one already set? */\r
239                         if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( xEventBitsType ) 0 )\r
240                         {\r
241                                 /* At least one of the bits was set.  No need to block. */\r
242                                 xTicksToWait = 0;\r
243                         }\r
244                 }\r
245                 else\r
246                 {\r
247                         /* Task has to wait for all the bits in uxBitsToWaitFor to be set.  Are they\r
248                         set already? */\r
249                         if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
250                         {\r
251                                 /* All the bits were set, no need to block. */\r
252                                 xTicksToWait = 0;\r
253                         }\r
254                 }\r
255 \r
256                 /* The task can return now if either its wait condition is already met\r
257                 or the requested block time is 0. */\r
258                 if( xTicksToWait == ( portTickType ) 0 )\r
259                 {\r
260                         /* No need to block, just set the return value. */\r
261                         uxReturn = uxCurrentEventBits;\r
262 \r
263                         if( xClearOnExit != pdFALSE )\r
264                         {\r
265                                 /* The user requested the bits be cleared again prior to exiting\r
266                                 this function. */\r
267                                 pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
268                         }\r
269                 }\r
270                 else\r
271                 {\r
272                         /* The task is going to block to wait for its required bits to be\r
273                         set.  uxControlBits are used to remember the specified behaviour of\r
274                         this call to xEventGroupWaitBits() - for use when the event bits\r
275                         unblock the task. */\r
276                         if( xClearOnExit != pdFALSE )\r
277                         {\r
278                                 uxControlBits |= taskCLEAR_EVENTS_ON_EXIT_BIT;\r
279                         }\r
280 \r
281                         if( xWaitForAllBits != pdFALSE )\r
282                         {\r
283                                 uxControlBits |= taskWAIT_FOR_ALL_BITS;\r
284                         }\r
285 \r
286                         /* Store the bits that the calling task is waiting for in the\r
287                         task's event list item so the kernel knows when a match is\r
288                         found.  Then enter the blocked state. */\r
289                         vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );\r
290                         portYIELD_WITHIN_API();\r
291                 }\r
292         }\r
293         taskEXIT_CRITICAL();\r
294 \r
295         if( xTicksToWait != ( portTickType ) 0 )\r
296         {\r
297                 /* The task blocked to wait for its required bits to be set - at this\r
298                 point either the required bits were set or the block time expired.  If\r
299                 the required bits were set they will have been stored in the task's\r
300                 event list item, and they should now be retrieved then cleared. */\r
301                 uxReturn = uxTaskResetEventItemValue();\r
302 \r
303                 if( ( uxReturn & taskUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 )\r
304                 {\r
305                         /* The task timed out, just return the current event bit value. */\r
306                         uxReturn = pxEventBits->uxEventBits;\r
307                 }\r
308                 else\r
309                 {\r
310                         /* The task unblocked because the bits were set.  Clear the control\r
311                         bits before returning the value. */\r
312                         uxReturn &= ~taskEVENT_BITS_CONTROL_BYTES;\r
313                 }\r
314         }\r
315 \r
316         return uxReturn;\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToClear )\r
321 {\r
322 xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
323 xEventBitsType uxReturn;\r
324 \r
325         /* Check the user is not attempting to clear the bits used by the kernel\r
326         itself. */\r
327         configASSERT( ( uxBitsToClear & taskEVENT_BITS_CONTROL_BYTES ) == 0 );\r
328 \r
329         uxBitsToClear = ~uxBitsToClear;\r
330         taskENTER_CRITICAL();\r
331         {\r
332                 /* The value returned is the event group value prior to the bits being\r
333                 cleared. */\r
334                 uxReturn = pxEventBits->uxEventBits;\r
335 \r
336                 /* Clear the bits. */\r
337                 pxEventBits->uxEventBits &= uxBitsToClear;\r
338         }\r
339         taskEXIT_CRITICAL();\r
340 \r
341         return uxReturn;\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet )\r
346 {\r
347 xListItem *pxListItem, *pxNext;\r
348 xListItem const *pxListEnd;\r
349 xList *pxList;\r
350 xEventBitsType uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;\r
351 xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
352 portBASE_TYPE xMatchFound = pdFALSE;\r
353 \r
354         /* Check the user is not attempting to set the bits used by the kernel\r
355         itself. */\r
356         configASSERT( ( uxBitsToSet & taskEVENT_BITS_CONTROL_BYTES ) == 0 );\r
357 \r
358         pxList = &( pxEventBits->xTasksWaitingForBits );\r
359         pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
360         vTaskSuspendAll();\r
361         {\r
362                 pxListItem = listGET_HEAD_ENTRY( pxList );\r
363 \r
364                 /* Set the bits. */\r
365                 pxEventBits->uxEventBits |= uxBitsToSet;\r
366 \r
367                 /* See if the new bit value should unblock any tasks. */\r
368                 while( pxListItem != pxListEnd )\r
369                 {\r
370                         pxNext = listGET_NEXT( pxListItem );\r
371                         uxBitsWaitedFor = listGET_LIST_ITEM_VALUE( pxListItem );\r
372                         xMatchFound = pdFALSE;\r
373 \r
374                         /* Split the bits waited for from the control bits. */\r
375                         uxControlBits = uxBitsWaitedFor & taskEVENT_BITS_CONTROL_BYTES;\r
376                         uxBitsWaitedFor &= ~taskEVENT_BITS_CONTROL_BYTES;\r
377 \r
378                         if( ( uxControlBits & taskWAIT_FOR_ALL_BITS ) == ( xEventBitsType ) 0 )\r
379                         {\r
380                                 /* Just looking for single bit being set. */\r
381                                 if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( xEventBitsType ) 0 )\r
382                                 {\r
383                                         xMatchFound = pdTRUE;\r
384                                 }\r
385                         }\r
386                         else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )\r
387                         {\r
388                                 /* All bits are set. */\r
389                                 xMatchFound = pdTRUE;\r
390                         }\r
391                         else\r
392                         {\r
393                                 /* Need all bits to be set, but not all the bits were set. */\r
394                         }\r
395 \r
396                         if( xMatchFound != pdFALSE )\r
397                         {\r
398                                 /* The bits match.  Should the bits be cleared on exit? */\r
399                                 if( ( uxControlBits & taskCLEAR_EVENTS_ON_EXIT_BIT ) != ( xEventBitsType ) 0 )\r
400                                 {\r
401                                         uxBitsToClear |= uxBitsWaitedFor;\r
402                                 }\r
403 \r
404                                 /* Store the actual event flag value in the task's event list\r
405                                 item before removing the task from the event list.  The\r
406                                 taskUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows\r
407                                 that is was unblocked due to its required bits matching, rather\r
408                                 than because it timed out. */\r
409                                 ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | taskUNBLOCKED_DUE_TO_BIT_SET );\r
410                         }\r
411 \r
412                         /* Move onto the next list item.  Note pxListItem->pxNext is not\r
413                         used here as the list item may have been removed from the event list\r
414                         and inserted into the ready/pending reading list. */\r
415                         pxListItem = pxNext;\r
416                 }\r
417 \r
418                 /* Clear any bits that matched when the taskCLEAR_EVENTS_ON_EXIT_BIT\r
419                 bit was set in the control word. */\r
420                 pxEventBits->uxEventBits &= ~uxBitsToClear;\r
421         }\r
422         ( void ) xTaskResumeAll();\r
423 \r
424         return pxEventBits->uxEventBits;\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 void vEventGroupDelete( xEventGroupHandle xEventGroup )\r
429 {\r
430 xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
431 const xList *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );\r
432 \r
433         vTaskSuspendAll();\r
434         {\r
435                 while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( unsigned portBASE_TYPE ) 0 )\r
436                 {\r
437                         /* Unblock the task, returning 0 as the event list is being deleted\r
438                         and     cannot therefore have any bits set. */\r
439                         configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( xListItem * ) &( pxTasksWaitingForBits->xListEnd ) );\r
440                         ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, ( portTickType ) taskUNBLOCKED_DUE_TO_BIT_SET );\r
441                 }\r
442 \r
443                 vPortFree( pxEventBits );\r
444         }\r
445         ( void ) xTaskResumeAll();\r
446 }\r
447 /*-----------------------------------------------------------*/\r
448 \r
449 /* For internal use only - execute a 'set bits' command that was pended from\r
450 an interrupt. */\r
451 void vEventGroupSetBitsCallback( void *pvEventGroup, unsigned long ulBitsToSet )\r
452 {\r
453         ( void ) xEventGroupSetBits( pvEventGroup, ( xEventBitsType ) ulBitsToSet );\r
454 }\r
455 \r
456 \r