]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/queue.c
Improve QueueSet.c test coverage by reading the queue set from an ISR to force paths...
[freertos] / FreeRTOS / Source / queue.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45 \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55 \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license\r
57     and contact details.\r
58 \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell\r
63     the code with commercial support, indemnification, and middleware, under\r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under\r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 #include <stdlib.h>\r
70 #include <string.h>\r
71 \r
72 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
73 all the API functions to use the MPU wrappers.  That should only be done when\r
74 task.h is included from an application file. */\r
75 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
76 \r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 \r
80 #if ( configUSE_CO_ROUTINES == 1 )\r
81         #include "croutine.h"\r
82 #endif\r
83 \r
84 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
85 \r
86 /* Constants used with the cRxLock and xTxLock structure members. */\r
87 #define queueUNLOCKED                                   ( ( signed portBASE_TYPE ) -1 )\r
88 #define queueLOCKED_UNMODIFIED                  ( ( signed portBASE_TYPE ) 0 )\r
89 \r
90 #define queueERRONEOUS_UNBLOCK                  ( -1 )\r
91 \r
92 /* For internal use only. */\r
93 #define queueSEND_TO_BACK                               ( 0 )\r
94 #define queueSEND_TO_FRONT                              ( 1 )\r
95 \r
96 /* Effectively make a union out of the xQUEUE structure. */\r
97 #define pxMutexHolder                                   pcTail\r
98 #define uxQueueType                                             pcHead\r
99 #define uxRecursiveCallCount                    pcReadFrom\r
100 #define queueQUEUE_IS_MUTEX                             NULL\r
101 \r
102 /* Semaphores do not actually store or copy data, so have an items size of\r
103 zero. */\r
104 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )\r
105 #define queueDONT_BLOCK                                  ( ( portTickType ) 0U )\r
106 #define queueMUTEX_GIVE_BLOCK_TIME               ( ( portTickType ) 0U )\r
107 \r
108 /* These definitions *must* match those in queue.h. */\r
109 #define queueQUEUE_TYPE_BASE                            ( 0U )\r
110 #define queueQUEUE_TYPE_MUTEX                           ( 1U )\r
111 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE      ( 2U )\r
112 #define queueQUEUE_TYPE_BINARY_SEMAPHORE        ( 3U )\r
113 #define queueQUEUE_TYPE_RECURSIVE_MUTEX         ( 4U )\r
114 #define queueQUEUE_TYPE_SET                                     ( 0U )\r
115 \r
116 /*\r
117  * Definition of the queue used by the scheduler.\r
118  * Items are queued by copy, not reference.\r
119  */\r
120 typedef struct QueueDefinition\r
121 {\r
122         signed char *pcHead;                                    /*< Points to the beginning of the queue storage area. */\r
123         signed char *pcTail;                                    /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */\r
124 \r
125         signed char *pcWriteTo;                                 /*< Points to the free next place in the storage area. */\r
126         signed char *pcReadFrom;                                /*< Points to the last place that a queued item was read from. */\r
127 \r
128         xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
129         xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
130 \r
131         volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */\r
132         unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
133         unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */\r
134 \r
135         volatile signed portBASE_TYPE xRxLock;  /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
136         volatile signed portBASE_TYPE xTxLock;  /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
137 \r
138         #if ( configUSE_TRACE_FACILITY == 1 )\r
139                 unsigned char ucQueueNumber;\r
140                 unsigned char ucQueueType;\r
141         #endif\r
142 \r
143         #if ( configUSE_QUEUE_SETS == 1 )\r
144                 struct QueueDefinition *pxQueueSetContainer;\r
145         #endif\r
146 \r
147 } xQUEUE;\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /*\r
151  * Inside this file xQueueHandle and xQueueSetHandle are both pointers to xQUEUE\r
152  * structures.  To keep the definition private the API header file defines both\r
153  * as pointers to void.\r
154  */\r
155 typedef xQUEUE * xQueueHandle;\r
156 typedef xQUEUE * xQueueSetHandle;\r
157 \r
158 /**\r
159  * Queue sets can contain both queues and semaphores, so the\r
160  * xQueueSetMemberHandle is defined as a type to be used where a parameter or\r
161  * return value can be either an xQueueHandle or an xSemaphoreHandle.\r
162  */\r
163 typedef xQUEUE * xQueueSetMemberHandle;\r
164 \r
165 /*\r
166  * In order to implement strict data hiding, the queue.h header file defines\r
167  * xQueueHandle and xQueueSetHandle as pointers to void.  In this file\r
168  * xQueueHandle and xQueueSetHandle are defined as pointers to xQUEUE objects.\r
169  * Therefore the queue.h header file cannot be included in this source file,\r
170  * and the function prototypes are provided directly.\r
171  */\r
172 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType ) PRIVILEGED_FUNCTION;\r
173 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
174 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
175 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;\r
176 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
177 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;\r
178 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;\r
179 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType ) PRIVILEGED_FUNCTION;\r
180 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;\r
181 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;\r
182 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;\r
183 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
184 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;\r
185 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
186 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
187 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
188 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;\r
189 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
190 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber ) PRIVILEGED_FUNCTION;\r
191 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
192 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue ) PRIVILEGED_FUNCTION;\r
193 xTaskHandle xQueueGetMutexHolder( xQueueHandle xSemaphore ) PRIVILEGED_FUNCTION;\r
194 xQueueSetHandle xQueueCreateSet( unsigned portBASE_TYPE uxEventQueueLength ) PRIVILEGED_FUNCTION;\r
195 xQueueSetMemberHandle xQueueSelectFromSet( xQueueSetHandle xQueueSet, portTickType xBlockTimeTicks ) PRIVILEGED_FUNCTION;\r
196 portBASE_TYPE xQueueAddToSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet ) PRIVILEGED_FUNCTION;\r
197 portBASE_TYPE xQueueRemoveFromSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet ) PRIVILEGED_FUNCTION;\r
198 \r
199 /*\r
200  * Co-routine queue functions differ from task queue functions.  Co-routines are\r
201  * an optional component.\r
202  */\r
203 #if ( configUSE_CO_ROUTINES == 1 )\r
204         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;\r
205         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;\r
206         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;\r
207         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;\r
208 #endif\r
209 \r
210 /*\r
211  * The queue registry is just a means for kernel aware debuggers to locate\r
212  * queue structures.  It has no other purpose so is an optional component.\r
213  */\r
214 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
215 \r
216         /* The type stored within the queue registry array.  This allows a name\r
217         to be assigned to each queue making kernel aware debugging a little\r
218         more user friendly. */\r
219         typedef struct QUEUE_REGISTRY_ITEM\r
220         {\r
221                 signed char *pcQueueName;\r
222                 xQueueHandle xHandle;\r
223         } xQueueRegistryItem;\r
224 \r
225         /* The queue registry is simply an array of xQueueRegistryItem structures.\r
226         The pcQueueName member of a structure being NULL is indicative of the\r
227         array position being vacant. */\r
228         xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];\r
229 \r
230         /* Removes a queue from the registry by simply setting the pcQueueName\r
231         member to NULL. */\r
232         static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;\r
233         void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;\r
234 #endif\r
235 \r
236 /*\r
237  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not\r
238  * prevent an ISR from adding or removing items to the queue, but does prevent\r
239  * an ISR from removing tasks from the queue event lists.  If an ISR finds a\r
240  * queue is locked it will instead increment the appropriate queue lock count\r
241  * to indicate that a task may require unblocking.  When the queue in unlocked\r
242  * these lock counts are inspected, and the appropriate action taken.\r
243  */\r
244 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
245 \r
246 /*\r
247  * Uses a critical section to determine if there is any data in a queue.\r
248  *\r
249  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
250  */\r
251 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
252 \r
253 /*\r
254  * Uses a critical section to determine if there is any space in a queue.\r
255  *\r
256  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
257  */\r
258 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
259 \r
260 /*\r
261  * Copies an item into the queue, either at the front of the queue or the\r
262  * back of the queue.\r
263  */\r
264 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;\r
265 \r
266 /*\r
267  * Copies an item out of a queue.\r
268  */\r
269 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;\r
270 \r
271 #if ( configUSE_QUEUE_SETS == 1 )\r
272         /*\r
273          * Checks to see if a queue is a member of a queue set, and if so, notifies\r
274          * the queue set that the queue contains data.\r
275          */\r
276         static portBASE_TYPE prvNotifyQueueSetContainer( xQUEUE *pxQueue, portBASE_TYPE xCopyPosition );\r
277 #endif\r
278 \r
279 /*-----------------------------------------------------------*/\r
280 \r
281 /*\r
282  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
283  * accessing the queue event lists.\r
284  */\r
285 #define prvLockQueue( pxQueue )                                                         \\r
286         taskENTER_CRITICAL();                                                                   \\r
287         {                                                                                                               \\r
288                 if( ( pxQueue )->xRxLock == queueUNLOCKED )                     \\r
289                 {                                                                                                       \\r
290                         ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED;  \\r
291                 }                                                                                                       \\r
292                 if( ( pxQueue )->xTxLock == queueUNLOCKED )                     \\r
293                 {                                                                                                       \\r
294                         ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED;  \\r
295                 }                                                                                                       \\r
296         }                                                                                                               \\r
297         taskEXIT_CRITICAL()\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue )\r
301 {\r
302         configASSERT( pxQueue );\r
303 \r
304         taskENTER_CRITICAL();\r
305         {\r
306                 pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );\r
307                 pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
308                 pxQueue->pcWriteTo = pxQueue->pcHead;\r
309                 pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );\r
310                 pxQueue->xRxLock = queueUNLOCKED;\r
311                 pxQueue->xTxLock = queueUNLOCKED;\r
312 \r
313                 if( xNewQueue == pdFALSE )\r
314                 {\r
315                         /* If there are tasks blocked waiting to read from the queue, then\r
316                         the tasks will remain blocked as after this function exits the queue\r
317                         will still be empty.  If there are tasks blocked waiting to     write to\r
318                         the queue, then one should be unblocked as after this function exits\r
319                         it will be possible to write to it. */\r
320                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
321                         {\r
322                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
323                                 {\r
324                                         portYIELD_WITHIN_API();\r
325                                 }\r
326                         }\r
327                 }\r
328                 else\r
329                 {\r
330                         /* Ensure the event queues start in the correct state. */\r
331                         vListInitialise( &( pxQueue->xTasksWaitingToSend ) );\r
332                         vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );\r
333                 }\r
334         }\r
335         taskEXIT_CRITICAL();\r
336 \r
337         /* A value is returned for calling semantic consistency with previous\r
338         versions. */\r
339         return pdPASS;\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )\r
344 {\r
345 xQUEUE *pxNewQueue;\r
346 size_t xQueueSizeInBytes;\r
347 xQueueHandle xReturn = NULL;\r
348 \r
349         /* Remove compiler warnings about unused parameters should\r
350         configUSE_TRACE_FACILITY not be set to 1. */\r
351         ( void ) ucQueueType;\r
352 \r
353         /* Allocate the new queue structure. */\r
354         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
355         {\r
356                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
357                 if( pxNewQueue != NULL )\r
358                 {\r
359                         /* Create the list of pointers to queue items.  The queue is one byte\r
360                         longer than asked for to make wrap checking easier/faster. */\r
361                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;\r
362 \r
363                         pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );\r
364                         if( pxNewQueue->pcHead != NULL )\r
365                         {\r
366                                 /* Initialise the queue members as described above where the\r
367                                 queue type is defined. */\r
368                                 pxNewQueue->uxLength = uxQueueLength;\r
369                                 pxNewQueue->uxItemSize = uxItemSize;\r
370                                 xQueueGenericReset( pxNewQueue, pdTRUE );\r
371 \r
372                                 #if ( configUSE_TRACE_FACILITY == 1 )\r
373                                 {\r
374                                         pxNewQueue->ucQueueType = ucQueueType;\r
375                                 }\r
376                                 #endif /* configUSE_TRACE_FACILITY */\r
377 \r
378                                 #if( configUSE_QUEUE_SETS == 1 )\r
379                                 {\r
380                                         pxNewQueue->pxQueueSetContainer = NULL;\r
381                                 }\r
382                                 #endif /* configUSE_QUEUE_SETS */\r
383 \r
384                                 traceQUEUE_CREATE( pxNewQueue );\r
385                                 xReturn = pxNewQueue;\r
386                         }\r
387                         else\r
388                         {\r
389                                 traceQUEUE_CREATE_FAILED( ucQueueType );\r
390                                 vPortFree( pxNewQueue );\r
391                         }\r
392                 }\r
393         }\r
394 \r
395         configASSERT( xReturn );\r
396 \r
397         return xReturn;\r
398 }\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 #if ( configUSE_MUTEXES == 1 )\r
402 \r
403         xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )\r
404         {\r
405         xQUEUE *pxNewQueue;\r
406 \r
407                 /* Prevent compiler warnings about unused parameters if\r
408                 configUSE_TRACE_FACILITY does not equal 1. */\r
409                 ( void ) ucQueueType;\r
410 \r
411                 /* Allocate the new queue structure. */\r
412                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
413                 if( pxNewQueue != NULL )\r
414                 {\r
415                         /* Information required for priority inheritance. */\r
416                         pxNewQueue->pxMutexHolder = NULL;\r
417                         pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
418 \r
419                         /* Queues used as a mutex no data is actually copied into or out\r
420                         of the queue. */\r
421                         pxNewQueue->pcWriteTo = NULL;\r
422                         pxNewQueue->pcReadFrom = NULL;\r
423 \r
424                         /* Each mutex has a length of 1 (like a binary semaphore) and\r
425                         an item size of 0 as nothing is actually copied into or out\r
426                         of the mutex. */\r
427                         pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
428                         pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;\r
429                         pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;\r
430                         pxNewQueue->xRxLock = queueUNLOCKED;\r
431                         pxNewQueue->xTxLock = queueUNLOCKED;\r
432 \r
433                         #if ( configUSE_TRACE_FACILITY == 1 )\r
434                         {\r
435                                 pxNewQueue->ucQueueType = ucQueueType;\r
436                         }\r
437                         #endif\r
438 \r
439                         #if ( configUSE_QUEUE_SETS == 1 )\r
440                         {\r
441                                 pxNewQueue->pxQueueSetContainer = NULL;\r
442                         }\r
443                         #endif\r
444 \r
445                         /* Ensure the event queues start with the correct state. */\r
446                         vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
447                         vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
448 \r
449                         traceCREATE_MUTEX( pxNewQueue );\r
450 \r
451                         /* Start with the semaphore in the expected state. */\r
452                         xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );\r
453                 }\r
454                 else\r
455                 {\r
456                         traceCREATE_MUTEX_FAILED();\r
457                 }\r
458 \r
459                 configASSERT( pxNewQueue );\r
460                 return pxNewQueue;\r
461         }\r
462 \r
463 #endif /* configUSE_MUTEXES */\r
464 /*-----------------------------------------------------------*/\r
465 \r
466 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )\r
467 \r
468         void* xQueueGetMutexHolder( xQueueHandle xSemaphore )\r
469         {\r
470         void *pxReturn;\r
471 \r
472                 /* This function is called by xSemaphoreGetMutexHolder(), and should not\r
473                 be called directly.  Note:  This is is a good way of determining if the\r
474                 calling task is the mutex holder, but not a good way of determining the\r
475                 identity of the mutex holder, as the holder may change between the\r
476                 following critical section exiting and the function returning. */\r
477                 taskENTER_CRITICAL();\r
478                 {\r
479                         if( xSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )\r
480                         {\r
481                                 pxReturn = ( void * ) xSemaphore->pxMutexHolder;\r
482                         }\r
483                         else\r
484                         {\r
485                                 pxReturn = NULL;\r
486                         }\r
487                 }\r
488                 taskEXIT_CRITICAL();\r
489 \r
490                 return pxReturn;\r
491         }\r
492 \r
493 #endif\r
494 /*-----------------------------------------------------------*/\r
495 \r
496 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
497 \r
498         portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )\r
499         {\r
500         portBASE_TYPE xReturn;\r
501 \r
502                 configASSERT( pxMutex );\r
503 \r
504                 /* If this is the task that holds the mutex then pxMutexHolder will not\r
505                 change outside of this task.  If this task does not hold the mutex then\r
506                 pxMutexHolder can never coincidentally equal the tasks handle, and as\r
507                 this is the only condition we are interested in it does not matter if\r
508                 pxMutexHolder is accessed simultaneously by another task.  Therefore no\r
509                 mutual exclusion is required to test the pxMutexHolder variable. */\r
510                 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
511                 {\r
512                         traceGIVE_MUTEX_RECURSIVE( pxMutex );\r
513 \r
514                         /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to\r
515                         the task handle, therefore no underflow check is required.  Also,\r
516                         uxRecursiveCallCount is only modified by the mutex holder, and as\r
517                         there can only be one, no mutual exclusion is required to modify the\r
518                         uxRecursiveCallCount member. */\r
519                         ( pxMutex->uxRecursiveCallCount )--;\r
520 \r
521                         /* Have we unwound the call count? */\r
522                         if( pxMutex->uxRecursiveCallCount == 0 )\r
523                         {\r
524                                 /* Return the mutex.  This will automatically unblock any other\r
525                                 task that might be waiting to access the mutex. */\r
526                                 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );\r
527                         }\r
528 \r
529                         xReturn = pdPASS;\r
530                 }\r
531                 else\r
532                 {\r
533                         /* We cannot give the mutex because we are not the holder. */\r
534                         xReturn = pdFAIL;\r
535 \r
536                         traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
537                 }\r
538 \r
539                 return xReturn;\r
540         }\r
541 \r
542 #endif /* configUSE_RECURSIVE_MUTEXES */\r
543 /*-----------------------------------------------------------*/\r
544 \r
545 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
546 \r
547         portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )\r
548         {\r
549         portBASE_TYPE xReturn;\r
550 \r
551                 configASSERT( pxMutex );\r
552 \r
553                 /* Comments regarding mutual exclusion as per those within\r
554                 xQueueGiveMutexRecursive(). */\r
555 \r
556                 traceTAKE_MUTEX_RECURSIVE( pxMutex );\r
557 \r
558                 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
559                 {\r
560                         ( pxMutex->uxRecursiveCallCount )++;\r
561                         xReturn = pdPASS;\r
562                 }\r
563                 else\r
564                 {\r
565                         xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );\r
566 \r
567                         /* pdPASS will only be returned if we successfully obtained the mutex,\r
568                         we may have blocked to reach here. */\r
569                         if( xReturn == pdPASS )\r
570                         {\r
571                                 ( pxMutex->uxRecursiveCallCount )++;\r
572                         }\r
573                         else\r
574                         {\r
575                                 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
576                         }\r
577                 }\r
578 \r
579                 return xReturn;\r
580         }\r
581 \r
582 #endif /* configUSE_RECURSIVE_MUTEXES */\r
583 /*-----------------------------------------------------------*/\r
584 \r
585 #if ( configUSE_COUNTING_SEMAPHORES == 1 )\r
586 \r
587         xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )\r
588         {\r
589         xQueueHandle pxHandle;\r
590 \r
591                 pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );\r
592 \r
593                 if( pxHandle != NULL )\r
594                 {\r
595                         pxHandle->uxMessagesWaiting = uxInitialCount;\r
596 \r
597                         traceCREATE_COUNTING_SEMAPHORE();\r
598                 }\r
599                 else\r
600                 {\r
601                         traceCREATE_COUNTING_SEMAPHORE_FAILED();\r
602                 }\r
603 \r
604                 configASSERT( pxHandle );\r
605                 return pxHandle;\r
606         }\r
607 \r
608 #endif /* configUSE_COUNTING_SEMAPHORES */\r
609 /*-----------------------------------------------------------*/\r
610 \r
611 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
612 {\r
613 signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
614 xTimeOutType xTimeOut;\r
615 \r
616         configASSERT( pxQueue );\r
617         configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
618 \r
619         /* This function relaxes the coding standard somewhat to allow return\r
620         statements within the function itself.  This is done in the interest\r
621         of execution time efficiency. */\r
622         for( ;; )\r
623         {\r
624                 taskENTER_CRITICAL();\r
625                 {\r
626                         /* Is there room on the queue now?  To be running we must be\r
627                         the highest priority task wanting to access the queue. */\r
628                         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
629                         {\r
630                                 traceQUEUE_SEND( pxQueue );\r
631                                 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
632 \r
633                                 #if ( configUSE_QUEUE_SETS == 1 )\r
634                                 {\r
635                                         if( pxQueue->pxQueueSetContainer != NULL )\r
636                                         {\r
637                                                 if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )\r
638                                                 {\r
639                                                         /* The queue is a member of a queue set, and posting\r
640                                                         to the queue set caused a higher priority task to\r
641                                                         unblock. A context switch is required. */\r
642                                                         portYIELD_WITHIN_API();\r
643                                                 }\r
644                                         }\r
645                                         else\r
646                                         {\r
647                                                 /* If there was a task waiting for data to arrive on the\r
648                                                 queue then unblock it now. */\r
649                                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
650                                                 {\r
651                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
652                                                         {\r
653                                                                 /* The unblocked task has a priority higher than\r
654                                                                 our own so yield immediately.  Yes it is ok to\r
655                                                                 do this from within the critical section - the\r
656                                                                 kernel takes care of that. */\r
657                                                                 portYIELD_WITHIN_API();\r
658                                                         }\r
659                                                 }\r
660                                         }\r
661                                 }\r
662                                 #else /* configUSE_QUEUE_SETS */\r
663                                 {\r
664                                         /* If there was a task waiting for data to arrive on the\r
665                                         queue then unblock it now. */\r
666                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
667                                         {\r
668                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
669                                                 {\r
670                                                         /* The unblocked task has a priority higher than\r
671                                                         our own so yield immediately.  Yes it is ok to do\r
672                                                         this from within the critical section - the kernel\r
673                                                         takes care of that. */\r
674                                                         portYIELD_WITHIN_API();\r
675                                                 }\r
676                                         }\r
677                                 }\r
678                                 #endif /* configUSE_QUEUE_SETS */\r
679 \r
680                                 taskEXIT_CRITICAL();\r
681 \r
682                                 /* Return to the original privilege level before exiting the\r
683                                 function. */\r
684                                 return pdPASS;\r
685                         }\r
686                         else\r
687                         {\r
688                                 if( xTicksToWait == ( portTickType ) 0 )\r
689                                 {\r
690                                         /* The queue was full and no block time is specified (or\r
691                                         the block time has expired) so leave now. */\r
692                                         taskEXIT_CRITICAL();\r
693 \r
694                                         /* Return to the original privilege level before exiting\r
695                                         the function. */\r
696                                         traceQUEUE_SEND_FAILED( pxQueue );\r
697                                         return errQUEUE_FULL;\r
698                                 }\r
699                                 else if( xEntryTimeSet == pdFALSE )\r
700                                 {\r
701                                         /* The queue was full and a block time was specified so\r
702                                         configure the timeout structure. */\r
703                                         vTaskSetTimeOutState( &xTimeOut );\r
704                                         xEntryTimeSet = pdTRUE;\r
705                                 }\r
706                         }\r
707                 }\r
708                 taskEXIT_CRITICAL();\r
709 \r
710                 /* Interrupts and other tasks can send to and receive from the queue\r
711                 now the critical section has been exited. */\r
712 \r
713                 vTaskSuspendAll();\r
714                 prvLockQueue( pxQueue );\r
715 \r
716                 /* Update the timeout state to see if it has expired yet. */\r
717                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
718                 {\r
719                         if( prvIsQueueFull( pxQueue ) != pdFALSE )\r
720                         {\r
721                                 traceBLOCKING_ON_QUEUE_SEND( pxQueue );\r
722                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
723 \r
724                                 /* Unlocking the queue means queue events can effect the\r
725                                 event list.  It is possible     that interrupts occurring now\r
726                                 remove this task from the event list again - but as the\r
727                                 scheduler is suspended the task will go onto the pending\r
728                                 ready last instead of the actual ready list. */\r
729                                 prvUnlockQueue( pxQueue );\r
730 \r
731                                 /* Resuming the scheduler will move tasks from the pending\r
732                                 ready list into the ready list - so it is feasible that this\r
733                                 task is already in a ready list before it yields - in which\r
734                                 case the yield will not cause a context switch unless there\r
735                                 is also a higher priority task in the pending ready list. */\r
736                                 if( xTaskResumeAll() == pdFALSE )\r
737                                 {\r
738                                         portYIELD_WITHIN_API();\r
739                                 }\r
740                         }\r
741                         else\r
742                         {\r
743                                 /* Try again. */\r
744                                 prvUnlockQueue( pxQueue );\r
745                                 ( void ) xTaskResumeAll();\r
746                         }\r
747                 }\r
748                 else\r
749                 {\r
750                         /* The timeout has expired. */\r
751                         prvUnlockQueue( pxQueue );\r
752                         ( void ) xTaskResumeAll();\r
753 \r
754                         /* Return to the original privilege level before exiting the\r
755                         function. */\r
756                         traceQUEUE_SEND_FAILED( pxQueue );\r
757                         return errQUEUE_FULL;\r
758                 }\r
759         }\r
760 }\r
761 /*-----------------------------------------------------------*/\r
762 \r
763 #if ( configUSE_ALTERNATIVE_API == 1 )\r
764 \r
765         signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
766         {\r
767         signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
768         xTimeOutType xTimeOut;\r
769 \r
770                 configASSERT( pxQueue );\r
771                 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
772 \r
773                 for( ;; )\r
774                 {\r
775                         taskENTER_CRITICAL();\r
776                         {\r
777                                 /* Is there room on the queue now?  To be running we must be\r
778                                 the highest priority task wanting to access the queue. */\r
779                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
780                                 {\r
781                                         traceQUEUE_SEND( pxQueue );\r
782                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
783 \r
784                                         /* If there was a task waiting for data to arrive on the\r
785                                         queue then unblock it now. */\r
786                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
787                                         {\r
788                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
789                                                 {\r
790                                                         /* The unblocked task has a priority higher than\r
791                                                         our own so yield immediately. */\r
792                                                         portYIELD_WITHIN_API();\r
793                                                 }\r
794                                         }\r
795 \r
796                                         taskEXIT_CRITICAL();\r
797                                         return pdPASS;\r
798                                 }\r
799                                 else\r
800                                 {\r
801                                         if( xTicksToWait == ( portTickType ) 0 )\r
802                                         {\r
803                                                 taskEXIT_CRITICAL();\r
804                                                 return errQUEUE_FULL;\r
805                                         }\r
806                                         else if( xEntryTimeSet == pdFALSE )\r
807                                         {\r
808                                                 vTaskSetTimeOutState( &xTimeOut );\r
809                                                 xEntryTimeSet = pdTRUE;\r
810                                         }\r
811                                 }\r
812                         }\r
813                         taskEXIT_CRITICAL();\r
814 \r
815                         taskENTER_CRITICAL();\r
816                         {\r
817                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
818                                 {\r
819                                         if( prvIsQueueFull( pxQueue ) != pdFALSE )\r
820                                         {\r
821                                                 traceBLOCKING_ON_QUEUE_SEND( pxQueue );\r
822                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
823                                                 portYIELD_WITHIN_API();\r
824                                         }\r
825                                 }\r
826                                 else\r
827                                 {\r
828                                         taskEXIT_CRITICAL();\r
829                                         traceQUEUE_SEND_FAILED( pxQueue );\r
830                                         return errQUEUE_FULL;\r
831                                 }\r
832                         }\r
833                         taskEXIT_CRITICAL();\r
834                 }\r
835         }\r
836 \r
837 #endif /* configUSE_ALTERNATIVE_API */\r
838 /*-----------------------------------------------------------*/\r
839 \r
840 #if ( configUSE_ALTERNATIVE_API == 1 )\r
841 \r
842         signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
843         {\r
844         signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
845         xTimeOutType xTimeOut;\r
846         signed char *pcOriginalReadPosition;\r
847 \r
848                 configASSERT( pxQueue );\r
849                 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
850 \r
851                 for( ;; )\r
852                 {\r
853                         taskENTER_CRITICAL();\r
854                         {\r
855                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
856                                 {\r
857                                         /* Remember our read position in case we are just peeking. */\r
858                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
859 \r
860                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
861 \r
862                                         if( xJustPeeking == pdFALSE )\r
863                                         {\r
864                                                 traceQUEUE_RECEIVE( pxQueue );\r
865 \r
866                                                 /* We are actually removing data. */\r
867                                                 --( pxQueue->uxMessagesWaiting );\r
868 \r
869                                                 #if ( configUSE_MUTEXES == 1 )\r
870                                                 {\r
871                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
872                                                         {\r
873                                                                 /* Record the information required to implement\r
874                                                                 priority inheritance should it become necessary. */\r
875                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
876                                                         }\r
877                                                 }\r
878                                                 #endif\r
879 \r
880                                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
881                                                 {\r
882                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
883                                                         {\r
884                                                                 portYIELD_WITHIN_API();\r
885                                                         }\r
886                                                 }\r
887                                         }\r
888                                         else\r
889                                         {\r
890                                                 traceQUEUE_PEEK( pxQueue );\r
891 \r
892                                                 /* We are not removing the data, so reset our read\r
893                                                 pointer. */\r
894                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;\r
895 \r
896                                                 /* The data is being left in the queue, so see if there are\r
897                                                 any other tasks waiting for the data. */\r
898                                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
899                                                 {\r
900                                                         /* Tasks that are removed from the event list will get added to\r
901                                                         the pending ready list as the scheduler is still suspended. */\r
902                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
903                                                         {\r
904                                                                 /* The task waiting has a higher priority than this task. */\r
905                                                                 portYIELD_WITHIN_API();\r
906                                                         }\r
907                                                 }\r
908 \r
909                                         }\r
910 \r
911                                         taskEXIT_CRITICAL();\r
912                                         return pdPASS;\r
913                                 }\r
914                                 else\r
915                                 {\r
916                                         if( xTicksToWait == ( portTickType ) 0 )\r
917                                         {\r
918                                                 taskEXIT_CRITICAL();\r
919                                                 traceQUEUE_RECEIVE_FAILED( pxQueue );\r
920                                                 return errQUEUE_EMPTY;\r
921                                         }\r
922                                         else if( xEntryTimeSet == pdFALSE )\r
923                                         {\r
924                                                 vTaskSetTimeOutState( &xTimeOut );\r
925                                                 xEntryTimeSet = pdTRUE;\r
926                                         }\r
927                                 }\r
928                         }\r
929                         taskEXIT_CRITICAL();\r
930 \r
931                         taskENTER_CRITICAL();\r
932                         {\r
933                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
934                                 {\r
935                                         if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
936                                         {\r
937                                                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
938 \r
939                                                 #if ( configUSE_MUTEXES == 1 )\r
940                                                 {\r
941                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
942                                                         {\r
943                                                                 portENTER_CRITICAL();\r
944                                                                 {\r
945                                                                         vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );\r
946                                                                 }\r
947                                                                 portEXIT_CRITICAL();\r
948                                                         }\r
949                                                 }\r
950                                                 #endif\r
951 \r
952                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
953                                                 portYIELD_WITHIN_API();\r
954                                         }\r
955                                 }\r
956                                 else\r
957                                 {\r
958                                         taskEXIT_CRITICAL();\r
959                                         traceQUEUE_RECEIVE_FAILED( pxQueue );\r
960                                         return errQUEUE_EMPTY;\r
961                                 }\r
962                         }\r
963                         taskEXIT_CRITICAL();\r
964                 }\r
965         }\r
966 \r
967 \r
968 #endif /* configUSE_ALTERNATIVE_API */\r
969 /*-----------------------------------------------------------*/\r
970 \r
971 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )\r
972 {\r
973 signed portBASE_TYPE xReturn;\r
974 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
975 \r
976         configASSERT( pxQueue );\r
977         configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
978 \r
979         /* Similar to xQueueGenericSend, except we don't block if there is no room\r
980         in the queue.  Also we don't directly wake a task that was blocked on a\r
981         queue read, instead we return a flag to say whether a context switch is\r
982         required or not (i.e. has a task with a higher priority than us been woken\r
983         by this post). */\r
984         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
985         {\r
986                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
987                 {\r
988                         traceQUEUE_SEND_FROM_ISR( pxQueue );\r
989 \r
990                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
991 \r
992                         /* If the queue is locked we do not alter the event list.  This will\r
993                         be done when the queue is unlocked later. */\r
994                         if( pxQueue->xTxLock == queueUNLOCKED )\r
995                         {\r
996                                 #if ( configUSE_QUEUE_SETS == 1 )\r
997                                 {\r
998                                         if( pxQueue->pxQueueSetContainer != NULL )\r
999                                         {\r
1000                                                 if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )\r
1001                                                 {\r
1002                                                         /* The queue is a member of a queue set, and posting\r
1003                                                         to the queue set caused a higher priority task to\r
1004                                                         unblock.  A context switch is required. */\r
1005                                                         if( pxHigherPriorityTaskWoken != NULL )\r
1006                                                         {\r
1007                                                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
1008                                                         }\r
1009                                                 }\r
1010                                         }\r
1011                                         else\r
1012                                         {\r
1013                                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1014                                                 {\r
1015                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1016                                                         {\r
1017                                                                 /* The task waiting has a higher priority so record that a\r
1018                                                                 context switch is required. */\r
1019                                                                 if( pxHigherPriorityTaskWoken != NULL )\r
1020                                                                 {\r
1021                                                                         *pxHigherPriorityTaskWoken = pdTRUE;\r
1022                                                                 }\r
1023                                                         }\r
1024                                                 }\r
1025                                         }\r
1026                                 }\r
1027                                 #else /* configUSE_QUEUE_SETS */\r
1028                                 {\r
1029                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1030                                         {\r
1031                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1032                                                 {\r
1033                                                         /* The task waiting has a higher priority so record that a\r
1034                                                         context switch is required. */\r
1035                                                         if( pxHigherPriorityTaskWoken != NULL )\r
1036                                                         {\r
1037                                                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
1038                                                         }\r
1039                                                 }\r
1040                                         }\r
1041                                 }\r
1042                                 #endif /* configUSE_QUEUE_SETS */\r
1043                         }\r
1044                         else\r
1045                         {\r
1046                                 /* Increment the lock count so the task that unlocks the queue\r
1047                                 knows that data was posted while it was locked. */\r
1048                                 ++( pxQueue->xTxLock );\r
1049                         }\r
1050 \r
1051                         xReturn = pdPASS;\r
1052                 }\r
1053                 else\r
1054                 {\r
1055                         traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );\r
1056                         xReturn = errQUEUE_FULL;\r
1057                 }\r
1058         }\r
1059         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1060 \r
1061         return xReturn;\r
1062 }\r
1063 /*-----------------------------------------------------------*/\r
1064 \r
1065 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
1066 {\r
1067 signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
1068 xTimeOutType xTimeOut;\r
1069 signed char *pcOriginalReadPosition;\r
1070 \r
1071         configASSERT( pxQueue );\r
1072         configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
1073 \r
1074         /* This function relaxes the coding standard somewhat to allow return\r
1075         statements within the function itself.  This is done in the interest\r
1076         of execution time efficiency. */\r
1077 \r
1078         for( ;; )\r
1079         {\r
1080                 taskENTER_CRITICAL();\r
1081                 {\r
1082                         /* Is there data in the queue now?  To be running we must be\r
1083                         the highest priority task wanting to access the queue. */\r
1084                         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1085                         {\r
1086                                 /* Remember our read position in case we are just peeking. */\r
1087                                 pcOriginalReadPosition = pxQueue->pcReadFrom;\r
1088 \r
1089                                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
1090 \r
1091                                 if( xJustPeeking == pdFALSE )\r
1092                                 {\r
1093                                         traceQUEUE_RECEIVE( pxQueue );\r
1094 \r
1095                                         /* We are actually removing data. */\r
1096                                         --( pxQueue->uxMessagesWaiting );\r
1097 \r
1098                                         #if ( configUSE_MUTEXES == 1 )\r
1099                                         {\r
1100                                                 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
1101                                                 {\r
1102                                                         /* Record the information required to implement\r
1103                                                         priority inheritance should it become necessary. */\r
1104                                                         pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
1105                                                 }\r
1106                                         }\r
1107                                         #endif\r
1108 \r
1109                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1110                                         {\r
1111                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
1112                                                 {\r
1113                                                         portYIELD_WITHIN_API();\r
1114                                                 }\r
1115                                         }\r
1116                                 }\r
1117                                 else\r
1118                                 {\r
1119                                         traceQUEUE_PEEK( pxQueue );\r
1120 \r
1121                                         /* The data is not being removed, so reset the read\r
1122                                         pointer. */\r
1123                                         pxQueue->pcReadFrom = pcOriginalReadPosition;\r
1124 \r
1125                                         /* The data is being left in the queue, so see if there are\r
1126                                         any other tasks waiting for the data. */\r
1127                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1128                                         {\r
1129                                                 /* Tasks that are removed from the event list will get added to\r
1130                                                 the pending ready list as the scheduler is still suspended. */\r
1131                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1132                                                 {\r
1133                                                         /* The task waiting has a higher priority than this task. */\r
1134                                                         portYIELD_WITHIN_API();\r
1135                                                 }\r
1136                                         }\r
1137                                 }\r
1138 \r
1139                                 taskEXIT_CRITICAL();\r
1140                                 return pdPASS;\r
1141                         }\r
1142                         else\r
1143                         {\r
1144                                 if( xTicksToWait == ( portTickType ) 0 )\r
1145                                 {\r
1146                                         /* The queue was empty and no block time is specified (or\r
1147                                         the block time has expired) so leave now. */\r
1148                                         taskEXIT_CRITICAL();\r
1149                                         traceQUEUE_RECEIVE_FAILED( pxQueue );\r
1150                                         return errQUEUE_EMPTY;\r
1151                                 }\r
1152                                 else if( xEntryTimeSet == pdFALSE )\r
1153                                 {\r
1154                                         /* The queue was empty and a block time was specified so\r
1155                                         configure the timeout structure. */\r
1156                                         vTaskSetTimeOutState( &xTimeOut );\r
1157                                         xEntryTimeSet = pdTRUE;\r
1158                                 }\r
1159                         }\r
1160                 }\r
1161                 taskEXIT_CRITICAL();\r
1162 \r
1163                 /* Interrupts and other tasks can send to and receive from the queue\r
1164                 now the critical section has been exited. */\r
1165 \r
1166                 vTaskSuspendAll();\r
1167                 prvLockQueue( pxQueue );\r
1168 \r
1169                 /* Update the timeout state to see if it has expired yet. */\r
1170                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
1171                 {\r
1172                         if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1173                         {\r
1174                                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
1175 \r
1176                                 #if ( configUSE_MUTEXES == 1 )\r
1177                                 {\r
1178                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
1179                                         {\r
1180                                                 portENTER_CRITICAL();\r
1181                                                 {\r
1182                                                         vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );\r
1183                                                 }\r
1184                                                 portEXIT_CRITICAL();\r
1185                                         }\r
1186                                 }\r
1187                                 #endif\r
1188 \r
1189                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
1190                                 prvUnlockQueue( pxQueue );\r
1191                                 if( xTaskResumeAll() == pdFALSE )\r
1192                                 {\r
1193                                         portYIELD_WITHIN_API();\r
1194                                 }\r
1195                         }\r
1196                         else\r
1197                         {\r
1198                                 /* Try again. */\r
1199                                 prvUnlockQueue( pxQueue );\r
1200                                 ( void ) xTaskResumeAll();\r
1201                         }\r
1202                 }\r
1203                 else\r
1204                 {\r
1205                         prvUnlockQueue( pxQueue );\r
1206                         ( void ) xTaskResumeAll();\r
1207                         traceQUEUE_RECEIVE_FAILED( pxQueue );\r
1208                         return errQUEUE_EMPTY;\r
1209                 }\r
1210         }\r
1211 }\r
1212 /*-----------------------------------------------------------*/\r
1213 \r
1214 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )\r
1215 {\r
1216 signed portBASE_TYPE xReturn;\r
1217 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
1218 \r
1219         configASSERT( pxQueue );\r
1220         configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
1221 \r
1222         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1223         {\r
1224                 /* We cannot block from an ISR, so check there is data available. */\r
1225                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1226                 {\r
1227                         traceQUEUE_RECEIVE_FROM_ISR( pxQueue );\r
1228 \r
1229                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
1230                         --( pxQueue->uxMessagesWaiting );\r
1231 \r
1232                         /* If the queue is locked we will not modify the event list.  Instead\r
1233                         we update the lock count so the task that unlocks the queue will know\r
1234                         that an ISR has removed data while the queue was locked. */\r
1235                         if( pxQueue->xRxLock == queueUNLOCKED )\r
1236                         {\r
1237                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1238                                 {\r
1239                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1240                                         {\r
1241                                                 /* The task waiting has a higher priority than us so\r
1242                                                 force a context switch. */\r
1243                                                 if( pxHigherPriorityTaskWoken != NULL )\r
1244                                                 {\r
1245                                                         *pxHigherPriorityTaskWoken = pdTRUE;\r
1246                                                 }\r
1247                                         }\r
1248                                 }\r
1249                         }\r
1250                         else\r
1251                         {\r
1252                                 /* Increment the lock count so the task that unlocks the queue\r
1253                                 knows that data was removed while it was locked. */\r
1254                                 ++( pxQueue->xRxLock );\r
1255                         }\r
1256 \r
1257                         xReturn = pdPASS;\r
1258                 }\r
1259                 else\r
1260                 {\r
1261                         xReturn = pdFAIL;\r
1262                         traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );\r
1263                 }\r
1264         }\r
1265         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1266 \r
1267         return xReturn;\r
1268 }\r
1269 /*-----------------------------------------------------------*/\r
1270 \r
1271 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )\r
1272 {\r
1273 unsigned portBASE_TYPE uxReturn;\r
1274 \r
1275         configASSERT( pxQueue );\r
1276 \r
1277         taskENTER_CRITICAL();\r
1278                 uxReturn = pxQueue->uxMessagesWaiting;\r
1279         taskEXIT_CRITICAL();\r
1280 \r
1281         return uxReturn;\r
1282 }\r
1283 /*-----------------------------------------------------------*/\r
1284 \r
1285 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )\r
1286 {\r
1287 unsigned portBASE_TYPE uxReturn;\r
1288 \r
1289         configASSERT( pxQueue );\r
1290 \r
1291         uxReturn = pxQueue->uxMessagesWaiting;\r
1292 \r
1293         return uxReturn;\r
1294 }\r
1295 /*-----------------------------------------------------------*/\r
1296 \r
1297 void vQueueDelete( xQueueHandle pxQueue )\r
1298 {\r
1299         configASSERT( pxQueue );\r
1300 \r
1301         traceQUEUE_DELETE( pxQueue );\r
1302         vQueueUnregisterQueue( pxQueue );\r
1303         vPortFree( pxQueue->pcHead );\r
1304         vPortFree( pxQueue );\r
1305 }\r
1306 /*-----------------------------------------------------------*/\r
1307 \r
1308 #if ( configUSE_TRACE_FACILITY == 1 )\r
1309 \r
1310         unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )\r
1311         {\r
1312                 return pxQueue->ucQueueNumber;\r
1313         }\r
1314 \r
1315 #endif /* configUSE_TRACE_FACILITY */\r
1316 /*-----------------------------------------------------------*/\r
1317 \r
1318 #if ( configUSE_TRACE_FACILITY == 1 )\r
1319 \r
1320         void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )\r
1321         {\r
1322                 pxQueue->ucQueueNumber = ucQueueNumber;\r
1323         }\r
1324 \r
1325 #endif /* configUSE_TRACE_FACILITY */\r
1326 /*-----------------------------------------------------------*/\r
1327 \r
1328 #if ( configUSE_TRACE_FACILITY == 1 )\r
1329 \r
1330         unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )\r
1331         {\r
1332                 return pxQueue->ucQueueType;\r
1333         }\r
1334 \r
1335 #endif /* configUSE_TRACE_FACILITY */\r
1336 /*-----------------------------------------------------------*/\r
1337 \r
1338 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )\r
1339 {\r
1340         if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )\r
1341         {\r
1342                 #if ( configUSE_MUTEXES == 1 )\r
1343                 {\r
1344                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
1345                         {\r
1346                                 /* The mutex is no longer being held. */\r
1347                                 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );\r
1348                                 pxQueue->pxMutexHolder = NULL;\r
1349                         }\r
1350                 }\r
1351                 #endif\r
1352         }\r
1353         else if( xPosition == queueSEND_TO_BACK )\r
1354         {\r
1355                 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
1356                 pxQueue->pcWriteTo += pxQueue->uxItemSize;\r
1357                 if( pxQueue->pcWriteTo >= pxQueue->pcTail )\r
1358                 {\r
1359                         pxQueue->pcWriteTo = pxQueue->pcHead;\r
1360                 }\r
1361         }\r
1362         else\r
1363         {\r
1364                 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
1365                 pxQueue->pcReadFrom -= pxQueue->uxItemSize;\r
1366                 if( pxQueue->pcReadFrom < pxQueue->pcHead )\r
1367                 {\r
1368                         pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
1369                 }\r
1370         }\r
1371 \r
1372         ++( pxQueue->uxMessagesWaiting );\r
1373 }\r
1374 /*-----------------------------------------------------------*/\r
1375 \r
1376 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )\r
1377 {\r
1378         if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
1379         {\r
1380                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1381                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1382                 {\r
1383                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1384                 }\r
1385                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1386         }\r
1387 }\r
1388 /*-----------------------------------------------------------*/\r
1389 \r
1390 static void prvUnlockQueue( xQueueHandle pxQueue )\r
1391 {\r
1392         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
1393 \r
1394         /* The lock counts contains the number of extra data items placed or\r
1395         removed from the queue while the queue was locked.  When a queue is\r
1396         locked items can be added or removed, but the event lists cannot be\r
1397         updated. */\r
1398         taskENTER_CRITICAL();\r
1399         {\r
1400                 /* See if data was added to the queue while it was locked. */\r
1401                 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )\r
1402                 {\r
1403                         /* Data was posted while the queue was locked.  Are any tasks\r
1404                         blocked waiting for data to become available? */\r
1405                         #if ( configUSE_QUEUE_SETS == 1 )\r
1406                         {\r
1407                                 if( pxQueue->pxQueueSetContainer != NULL )\r
1408                                 {\r
1409                                         if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE )\r
1410                                         {\r
1411                                                 /* The queue is a member of a queue set, and posting to\r
1412                                                 the queue set caused a higher priority task to unblock.\r
1413                                                 A context switch is required. */\r
1414                                                 vTaskMissedYield();\r
1415                                         }\r
1416                                 }\r
1417                                 else\r
1418                                 {\r
1419                                         /* Tasks that are removed from the event list will get added to\r
1420                                         the pending ready list as the scheduler is still suspended. */\r
1421                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1422                                         {\r
1423                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1424                                                 {\r
1425                                                         /* The task waiting has a higher priority so record that a\r
1426                                                         context switch is required. */\r
1427                                                         vTaskMissedYield();\r
1428                                                 }\r
1429                                         }\r
1430                                         else\r
1431                                         {\r
1432                                                 break;\r
1433                                         }\r
1434                                 }\r
1435                         }\r
1436                         #else /* configUSE_QUEUE_SETS */\r
1437                         {\r
1438                                 /* Tasks that are removed from the event list will get added to\r
1439                                 the pending ready list as the scheduler is still suspended. */\r
1440                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1441                                 {\r
1442                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1443                                         {\r
1444                                                 /* The task waiting has a higher priority so record that a\r
1445                                                 context switch is required. */\r
1446                                                 vTaskMissedYield();\r
1447                                         }\r
1448                                 }\r
1449                                 else\r
1450                                 {\r
1451                                         break;\r
1452                                 }\r
1453                         }\r
1454                         #endif /* configUSE_QUEUE_SETS */\r
1455 \r
1456                         --( pxQueue->xTxLock );\r
1457                 }\r
1458 \r
1459                 pxQueue->xTxLock = queueUNLOCKED;\r
1460         }\r
1461         taskEXIT_CRITICAL();\r
1462 \r
1463         /* Do the same for the Rx lock. */\r
1464         taskENTER_CRITICAL();\r
1465         {\r
1466                 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )\r
1467                 {\r
1468                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1469                         {\r
1470                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1471                                 {\r
1472                                         vTaskMissedYield();\r
1473                                 }\r
1474 \r
1475                                 --( pxQueue->xRxLock );\r
1476                         }\r
1477                         else\r
1478                         {\r
1479                                 break;\r
1480                         }\r
1481                 }\r
1482 \r
1483                 pxQueue->xRxLock = queueUNLOCKED;\r
1484         }\r
1485         taskEXIT_CRITICAL();\r
1486 }\r
1487 /*-----------------------------------------------------------*/\r
1488 \r
1489 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )\r
1490 {\r
1491 signed portBASE_TYPE xReturn;\r
1492 \r
1493         taskENTER_CRITICAL();\r
1494                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
1495         taskEXIT_CRITICAL();\r
1496 \r
1497         return xReturn;\r
1498 }\r
1499 /*-----------------------------------------------------------*/\r
1500 \r
1501 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )\r
1502 {\r
1503 signed portBASE_TYPE xReturn;\r
1504 \r
1505         configASSERT( pxQueue );\r
1506         xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
1507 \r
1508         return xReturn;\r
1509 }\r
1510 /*-----------------------------------------------------------*/\r
1511 \r
1512 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )\r
1513 {\r
1514 signed portBASE_TYPE xReturn;\r
1515 \r
1516         taskENTER_CRITICAL();\r
1517                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
1518         taskEXIT_CRITICAL();\r
1519 \r
1520         return xReturn;\r
1521 }\r
1522 /*-----------------------------------------------------------*/\r
1523 \r
1524 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )\r
1525 {\r
1526 signed portBASE_TYPE xReturn;\r
1527 \r
1528         configASSERT( pxQueue );\r
1529         xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
1530 \r
1531         return xReturn;\r
1532 }\r
1533 /*-----------------------------------------------------------*/\r
1534 \r
1535 #if ( configUSE_CO_ROUTINES == 1 )\r
1536 \r
1537         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
1538         {\r
1539         signed portBASE_TYPE xReturn;\r
1540 \r
1541                 /* If the queue is already full we may have to block.  A critical section\r
1542                 is required to prevent an interrupt removing something from the queue\r
1543                 between the check to see if the queue is full and blocking on the queue. */\r
1544                 portDISABLE_INTERRUPTS();\r
1545                 {\r
1546                         if( prvIsQueueFull( pxQueue ) != pdFALSE )\r
1547                         {\r
1548                                 /* The queue is full - do we want to block or just leave without\r
1549                                 posting? */\r
1550                                 if( xTicksToWait > ( portTickType ) 0 )\r
1551                                 {\r
1552                                         /* As this is called from a coroutine we cannot block directly, but\r
1553                                         return indicating that we need to block. */\r
1554                                         vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );\r
1555                                         portENABLE_INTERRUPTS();\r
1556                                         return errQUEUE_BLOCKED;\r
1557                                 }\r
1558                                 else\r
1559                                 {\r
1560                                         portENABLE_INTERRUPTS();\r
1561                                         return errQUEUE_FULL;\r
1562                                 }\r
1563                         }\r
1564                 }\r
1565                 portENABLE_INTERRUPTS();\r
1566 \r
1567                 portNOP();\r
1568 \r
1569                 portDISABLE_INTERRUPTS();\r
1570                 {\r
1571                         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1572                         {\r
1573                                 /* There is room in the queue, copy the data into the queue. */\r
1574                                 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1575                                 xReturn = pdPASS;\r
1576 \r
1577                                 /* Were any co-routines waiting for data to become available? */\r
1578                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1579                                 {\r
1580                                         /* In this instance the co-routine could be placed directly\r
1581                                         into the ready list as we are within a critical section.\r
1582                                         Instead the same pending ready list mechanism is used as if\r
1583                                         the event were caused from within an interrupt. */\r
1584                                         if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1585                                         {\r
1586                                                 /* The co-routine waiting has a higher priority so record\r
1587                                                 that a yield might be appropriate. */\r
1588                                                 xReturn = errQUEUE_YIELD;\r
1589                                         }\r
1590                                 }\r
1591                         }\r
1592                         else\r
1593                         {\r
1594                                 xReturn = errQUEUE_FULL;\r
1595                         }\r
1596                 }\r
1597                 portENABLE_INTERRUPTS();\r
1598 \r
1599                 return xReturn;\r
1600         }\r
1601 \r
1602 #endif /* configUSE_CO_ROUTINES */\r
1603 /*-----------------------------------------------------------*/\r
1604 \r
1605 #if ( configUSE_CO_ROUTINES == 1 )\r
1606 \r
1607         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
1608         {\r
1609         signed portBASE_TYPE xReturn;\r
1610 \r
1611                 /* If the queue is already empty we may have to block.  A critical section\r
1612                 is required to prevent an interrupt adding something to the queue\r
1613                 between the check to see if the queue is empty and blocking on the queue. */\r
1614                 portDISABLE_INTERRUPTS();\r
1615                 {\r
1616                         if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
1617                         {\r
1618                                 /* There are no messages in the queue, do we want to block or just\r
1619                                 leave with nothing? */\r
1620                                 if( xTicksToWait > ( portTickType ) 0 )\r
1621                                 {\r
1622                                         /* As this is a co-routine we cannot block directly, but return\r
1623                                         indicating that we need to block. */\r
1624                                         vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );\r
1625                                         portENABLE_INTERRUPTS();\r
1626                                         return errQUEUE_BLOCKED;\r
1627                                 }\r
1628                                 else\r
1629                                 {\r
1630                                         portENABLE_INTERRUPTS();\r
1631                                         return errQUEUE_FULL;\r
1632                                 }\r
1633                         }\r
1634                 }\r
1635                 portENABLE_INTERRUPTS();\r
1636 \r
1637                 portNOP();\r
1638 \r
1639                 portDISABLE_INTERRUPTS();\r
1640                 {\r
1641                         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1642                         {\r
1643                                 /* Data is available from the queue. */\r
1644                                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1645                                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1646                                 {\r
1647                                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1648                                 }\r
1649                                 --( pxQueue->uxMessagesWaiting );\r
1650                                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1651 \r
1652                                 xReturn = pdPASS;\r
1653 \r
1654                                 /* Were any co-routines waiting for space to become available? */\r
1655                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1656                                 {\r
1657                                         /* In this instance the co-routine could be placed directly\r
1658                                         into the ready list as we are within a critical section.\r
1659                                         Instead the same pending ready list mechanism is used as if\r
1660                                         the event were caused from within an interrupt. */\r
1661                                         if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1662                                         {\r
1663                                                 xReturn = errQUEUE_YIELD;\r
1664                                         }\r
1665                                 }\r
1666                         }\r
1667                         else\r
1668                         {\r
1669                                 xReturn = pdFAIL;\r
1670                         }\r
1671                 }\r
1672                 portENABLE_INTERRUPTS();\r
1673 \r
1674                 return xReturn;\r
1675         }\r
1676 \r
1677 #endif /* configUSE_CO_ROUTINES */\r
1678 /*-----------------------------------------------------------*/\r
1679 \r
1680 #if ( configUSE_CO_ROUTINES == 1 )\r
1681 \r
1682         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
1683         {\r
1684                 /* Cannot block within an ISR so if there is no space on the queue then\r
1685                 exit without doing anything. */\r
1686                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1687                 {\r
1688                         prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1689 \r
1690                         /* We only want to wake one co-routine per ISR, so check that a\r
1691                         co-routine has not already been woken. */\r
1692                         if( xCoRoutinePreviouslyWoken == pdFALSE )\r
1693                         {\r
1694                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1695                                 {\r
1696                                         if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1697                                         {\r
1698                                                 return pdTRUE;\r
1699                                         }\r
1700                                 }\r
1701                         }\r
1702                 }\r
1703 \r
1704                 return xCoRoutinePreviouslyWoken;\r
1705         }\r
1706 \r
1707 #endif /* configUSE_CO_ROUTINES */\r
1708 /*-----------------------------------------------------------*/\r
1709 \r
1710 #if ( configUSE_CO_ROUTINES == 1 )\r
1711 \r
1712         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
1713         {\r
1714         signed portBASE_TYPE xReturn;\r
1715 \r
1716                 /* We cannot block from an ISR, so check there is data available. If\r
1717                 not then just leave without doing anything. */\r
1718                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1719                 {\r
1720                         /* Copy the data from the queue. */\r
1721                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1722                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1723                         {\r
1724                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
1725                         }\r
1726                         --( pxQueue->uxMessagesWaiting );\r
1727                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1728 \r
1729                         if( ( *pxCoRoutineWoken ) == pdFALSE )\r
1730                         {\r
1731                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1732                                 {\r
1733                                         if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1734                                         {\r
1735                                                 *pxCoRoutineWoken = pdTRUE;\r
1736                                         }\r
1737                                 }\r
1738                         }\r
1739 \r
1740                         xReturn = pdPASS;\r
1741                 }\r
1742                 else\r
1743                 {\r
1744                         xReturn = pdFAIL;\r
1745                 }\r
1746 \r
1747                 return xReturn;\r
1748         }\r
1749 \r
1750 #endif /* configUSE_CO_ROUTINES */\r
1751 /*-----------------------------------------------------------*/\r
1752 \r
1753 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
1754 \r
1755         void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )\r
1756         {\r
1757         unsigned portBASE_TYPE ux;\r
1758 \r
1759                 /* See if there is an empty space in the registry.  A NULL name denotes\r
1760                 a free slot. */\r
1761                 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )\r
1762                 {\r
1763                         if( xQueueRegistry[ ux ].pcQueueName == NULL )\r
1764                         {\r
1765                                 /* Store the information on this queue. */\r
1766                                 xQueueRegistry[ ux ].pcQueueName = pcQueueName;\r
1767                                 xQueueRegistry[ ux ].xHandle = xQueue;\r
1768                                 break;\r
1769                         }\r
1770                 }\r
1771         }\r
1772 \r
1773 #endif /* configQUEUE_REGISTRY_SIZE */\r
1774 /*-----------------------------------------------------------*/\r
1775 \r
1776 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
1777 \r
1778         static void vQueueUnregisterQueue( xQueueHandle xQueue )\r
1779         {\r
1780         unsigned portBASE_TYPE ux;\r
1781 \r
1782                 /* See if the handle of the queue being unregistered in actually in the\r
1783                 registry. */\r
1784                 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )\r
1785                 {\r
1786                         if( xQueueRegistry[ ux ].xHandle == xQueue )\r
1787                         {\r
1788                                 /* Set the name to NULL to show that this slot if free again. */\r
1789                                 xQueueRegistry[ ux ].pcQueueName = NULL;\r
1790                                 break;\r
1791                         }\r
1792                 }\r
1793 \r
1794         }\r
1795 \r
1796 #endif /* configQUEUE_REGISTRY_SIZE */\r
1797 /*-----------------------------------------------------------*/\r
1798 \r
1799 #if ( configUSE_TIMERS == 1 )\r
1800 \r
1801         void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )\r
1802         {\r
1803                 /* This function should not be called by application code hence the\r
1804                 'Restricted' in its name.  It is not part of the public API.  It is\r
1805                 designed for use by kernel code, and has special calling requirements.\r
1806                 It can result in vListInsert() being called on a list that can only\r
1807                 possibly ever have one item in it, so the list will be fast, but even\r
1808                 so it should be called with the scheduler locked and not from a critical\r
1809                 section. */\r
1810 \r
1811                 /* Only do anything if there are no messages in the queue.  This function\r
1812                 will not actually cause the task to block, just place it on a blocked\r
1813                 list.  It will not block until the scheduler is unlocked - at which\r
1814                 time a yield will be performed.  If an item is added to the queue while\r
1815                 the queue is locked, and the calling task blocks on the queue, then the\r
1816                 calling task will be immediately unblocked when the queue is unlocked. */\r
1817                 prvLockQueue( pxQueue );\r
1818                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )\r
1819                 {\r
1820                         /* There is nothing in the queue, block for the specified period. */\r
1821                         vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
1822                 }\r
1823                 prvUnlockQueue( pxQueue );\r
1824         }\r
1825 \r
1826 #endif /* configUSE_TIMERS */\r
1827 /*-----------------------------------------------------------*/\r
1828 \r
1829 #if ( configUSE_QUEUE_SETS == 1 )\r
1830 \r
1831         xQueueSetHandle xQueueCreateSet( unsigned portBASE_TYPE uxEventQueueLength )\r
1832         {\r
1833         xQUEUE *pxQueue;\r
1834 \r
1835                 pxQueue = xQueueGenericCreate( uxEventQueueLength, sizeof( xQUEUE * ), queueQUEUE_TYPE_SET );\r
1836 \r
1837                 return ( xQueueSetHandle ) pxQueue;\r
1838         }\r
1839 \r
1840 #endif /* configUSE_QUEUE_SETS */\r
1841 /*-----------------------------------------------------------*/\r
1842 \r
1843 #if ( configUSE_QUEUE_SETS == 1 )\r
1844 \r
1845         portBASE_TYPE xQueueAddToSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet )\r
1846         {\r
1847         portBASE_TYPE xReturn;\r
1848 \r
1849                 if( xQueueOrSemaphore->pxQueueSetContainer != NULL )\r
1850                 {\r
1851                         xReturn = pdFAIL;\r
1852                 }\r
1853                 else\r
1854                 {\r
1855                         taskENTER_CRITICAL();\r
1856                         {\r
1857                                 xQueueOrSemaphore->pxQueueSetContainer = xQueueSet;\r
1858                         }\r
1859                         taskEXIT_CRITICAL();\r
1860                         xReturn = pdPASS;\r
1861                 }\r
1862 \r
1863                 return xReturn;\r
1864         }\r
1865 \r
1866 #endif /* configUSE_QUEUE_SETS */\r
1867 /*-----------------------------------------------------------*/\r
1868 \r
1869 #if ( configUSE_QUEUE_SETS == 1 )\r
1870 \r
1871         portBASE_TYPE xQueueRemoveFromSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet )\r
1872         {\r
1873         portBASE_TYPE xReturn;\r
1874 \r
1875                 if( xQueueOrSemaphore->pxQueueSetContainer != xQueueSet )\r
1876                 {\r
1877                         /* The queue was not a member of the set. */\r
1878                         xReturn = pdFAIL;\r
1879                 }\r
1880                 else if( xQueueOrSemaphore->uxMessagesWaiting != 0 )\r
1881                 {\r
1882                         /* It is dangerous to remove a queue from a set when the queue is\r
1883                         not empty because the queue set will still hold pending events for\r
1884                         the queue. */\r
1885                         xReturn = pdFAIL;\r
1886                 }\r
1887                 else\r
1888                 {\r
1889                         taskENTER_CRITICAL();\r
1890                         {\r
1891                                 /* The queue is no longer contained in the set. */\r
1892                                 xQueueOrSemaphore->pxQueueSetContainer = NULL;\r
1893                         }\r
1894                         taskEXIT_CRITICAL();\r
1895                         xReturn = pdPASS;\r
1896                 }\r
1897 \r
1898                 return xReturn;\r
1899         }\r
1900 \r
1901 #endif /* configUSE_QUEUE_SETS */\r
1902 /*-----------------------------------------------------------*/\r
1903 \r
1904 #if ( configUSE_QUEUE_SETS == 1 )\r
1905 \r
1906         xQueueSetMemberHandle xQueueSelectFromSet( xQueueSetHandle xQueueSet, portTickType xBlockTimeTicks )\r
1907         {\r
1908         xQueueSetMemberHandle xReturn = NULL;\r
1909 \r
1910                 xQueueGenericReceive( ( xQueueHandle ) xQueueSet, &xReturn, xBlockTimeTicks, pdFALSE );\r
1911                 return xReturn;\r
1912         }\r
1913 \r
1914 #endif /* configUSE_QUEUE_SETS */\r
1915 /*-----------------------------------------------------------*/\r
1916 \r
1917 #if ( configUSE_QUEUE_SETS == 1 )\r
1918 \r
1919         xQueueSetMemberHandle xQueueSelectFromSetFromISR( xQueueSetHandle xQueueSet )\r
1920         {\r
1921         xQueueSetMemberHandle xReturn = NULL;\r
1922 \r
1923                 xQueueReceiveFromISR( ( xQueueHandle ) xQueueSet, &xReturn, NULL );\r
1924                 return xReturn;\r
1925         }\r
1926 \r
1927 #endif /* configUSE_QUEUE_SETS */\r
1928 /*-----------------------------------------------------------*/\r
1929 \r
1930 #if ( configUSE_QUEUE_SETS == 1 )\r
1931 \r
1932         static portBASE_TYPE prvNotifyQueueSetContainer( xQUEUE *pxQueue, portBASE_TYPE xCopyPosition )\r
1933         {\r
1934         xQUEUE *pxQueueSetContainer = pxQueue->pxQueueSetContainer;\r
1935         portBASE_TYPE xReturn = pdFALSE;\r
1936 \r
1937                 configASSERT( pxQueueSetContainer );\r
1938                 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );\r
1939 \r
1940                 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )\r
1941                 {\r
1942                         traceQUEUE_SEND( pxQueueSetContainer );\r
1943                         /* The data copies is the handle of the queue that contains data. */\r
1944                         prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );\r
1945                         if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )\r
1946                         {\r
1947                                 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )\r
1948                                 {\r
1949                                         /* The task waiting has a higher priority */\r
1950                                         xReturn = pdTRUE;\r
1951                                 }\r
1952                         }\r
1953                 }\r
1954 \r
1955                 return xReturn;\r
1956         }\r
1957 \r
1958 #endif /* configUSE_QUEUE_SETS */\r
1959 \r