]> git.sur5r.net Git - freertos/blob - Source/queue.c
Update version number to V7.0.1.
[freertos] / Source / queue.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5         FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
6         Atollic AB - Atollic provides professional embedded systems development \r
7         tools for C/C++ development, code analysis and test automation.  \r
8         See http://www.atollic.com\r
9         \r
10 \r
11     ***************************************************************************\r
12      *                                                                       *\r
13      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
14      *    Complete, revised, and edited pdf reference manuals are also       *\r
15      *    available.                                                         *\r
16      *                                                                       *\r
17      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
18      *    ensuring you get running as quickly as possible and with an        *\r
19      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
20      *    the FreeRTOS project to continue with its mission of providing     *\r
21      *    professional grade, cross platform, de facto standard solutions    *\r
22      *    for microcontrollers - completely free of charge!                  *\r
23      *                                                                       *\r
24      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
25      *                                                                       *\r
26      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
27      *                                                                       *\r
28     ***************************************************************************\r
29 \r
30 \r
31     This file is part of the FreeRTOS distribution.\r
32 \r
33     FreeRTOS is free software; you can redistribute it and/or modify it under\r
34     the terms of the GNU General Public License (version 2) as published by the\r
35     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
36     >>>NOTE<<< The modification to the GPL is included to allow you to\r
37     distribute a combined work that includes FreeRTOS without being obliged to\r
38     provide the source code for proprietary components outside of the FreeRTOS\r
39     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
40     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
41     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
42     more details. You should have received a copy of the GNU General Public\r
43     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
44     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
45     by writing to Richard Barry, contact details for whom are available on the\r
46     FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     http://www.FreeRTOS.org - Documentation, latest information, license and\r
51     contact details.\r
52 \r
53     http://www.SafeRTOS.com - A version that is certified for use in safety\r
54     critical systems.\r
55 \r
56     http://www.OpenRTOS.com - Commercial support, development, porting,\r
57     licensing and training services.\r
58 */\r
59 \r
60 #include <stdlib.h>\r
61 #include <string.h>\r
62 \r
63 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
64 all the API functions to use the MPU wrappers.  That should only be done when\r
65 task.h is included from an application file. */\r
66 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
67 \r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "croutine.h"\r
71 \r
72 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
73 \r
74 /*-----------------------------------------------------------\r
75  * PUBLIC LIST API documented in list.h\r
76  *----------------------------------------------------------*/\r
77 \r
78 /* Constants used with the cRxLock and cTxLock structure members. */\r
79 #define queueUNLOCKED                                   ( ( signed portBASE_TYPE ) -1 )\r
80 #define queueLOCKED_UNMODIFIED                  ( ( signed portBASE_TYPE ) 0 )\r
81 \r
82 #define queueERRONEOUS_UNBLOCK                  ( -1 )\r
83 \r
84 /* For internal use only. */\r
85 #define queueSEND_TO_BACK                               ( 0 )\r
86 #define queueSEND_TO_FRONT                              ( 1 )\r
87 \r
88 /* Effectively make a union out of the xQUEUE structure. */\r
89 #define pxMutexHolder                                   pcTail\r
90 #define uxQueueType                                             pcHead\r
91 #define uxRecursiveCallCount                    pcReadFrom\r
92 #define queueQUEUE_IS_MUTEX                             NULL\r
93 \r
94 /* Semaphores do not actually store or copy data, so have an items size of\r
95 zero. */\r
96 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )\r
97 #define queueDONT_BLOCK                                  ( ( portTickType ) 0 )\r
98 #define queueMUTEX_GIVE_BLOCK_TIME               ( ( portTickType ) 0 )\r
99 \r
100 /*\r
101  * Definition of the queue used by the scheduler.\r
102  * Items are queued by copy, not reference.\r
103  */\r
104 typedef struct QueueDefinition\r
105 {\r
106         signed char *pcHead;                            /*< Points to the beginning of the queue storage area. */\r
107         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
108 \r
109         signed char *pcWriteTo;                         /*< Points to the free next place in the storage area. */\r
110         signed char *pcReadFrom;                        /*< Points to the last place that a queued item was read from. */\r
111 \r
112         xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
113         xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
114 \r
115         volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */\r
116         unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
117         unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */\r
118 \r
119         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
120         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
121 \r
122 } xQUEUE;\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\r
126  * Inside this file xQueueHandle is a pointer to a xQUEUE structure.\r
127  * To keep the definition private the API header file defines it as a\r
128  * pointer to void.\r
129  */\r
130 typedef xQUEUE * xQueueHandle;\r
131 \r
132 /*\r
133  * Prototypes for public functions are included here so we don't have to\r
134  * include the API header file (as it defines xQueueHandle differently).  These\r
135  * functions are documented in the API header file.\r
136  */\r
137 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize ) PRIVILEGED_FUNCTION;\r
138 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
139 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
140 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;\r
141 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
142 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;\r
143 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;\r
144 xQueueHandle xQueueCreateMutex( void ) PRIVILEGED_FUNCTION;\r
145 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;\r
146 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;\r
147 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;\r
148 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
149 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;\r
150 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
151 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
152 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
153 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;\r
154 \r
155 /*\r
156  * Co-routine queue functions differ from task queue functions.  Co-routines are\r
157  * an optional component.\r
158  */\r
159 #if configUSE_CO_ROUTINES == 1\r
160         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;\r
161         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;\r
162         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;\r
163         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;\r
164 #endif\r
165 \r
166 /*\r
167  * The queue registry is just a means for kernel aware debuggers to locate\r
168  * queue structures.  It has no other purpose so is an optional component.\r
169  */\r
170 #if configQUEUE_REGISTRY_SIZE > 0\r
171 \r
172         /* The type stored within the queue registry array.  This allows a name\r
173         to be assigned to each queue making kernel aware debugging a little\r
174         more user friendly. */\r
175         typedef struct QUEUE_REGISTRY_ITEM\r
176         {\r
177                 signed char *pcQueueName;\r
178                 xQueueHandle xHandle;\r
179         } xQueueRegistryItem;\r
180 \r
181         /* The queue registry is simply an array of xQueueRegistryItem structures.\r
182         The pcQueueName member of a structure being NULL is indicative of the\r
183         array position being vacant. */\r
184         xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];\r
185 \r
186         /* Removes a queue from the registry by simply setting the pcQueueName\r
187         member to NULL. */\r
188         static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;\r
189         void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;\r
190 #endif\r
191 \r
192 /*\r
193  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not\r
194  * prevent an ISR from adding or removing items to the queue, but does prevent\r
195  * an ISR from removing tasks from the queue event lists.  If an ISR finds a\r
196  * queue is locked it will instead increment the appropriate queue lock count\r
197  * to indicate that a task may require unblocking.  When the queue in unlocked\r
198  * these lock counts are inspected, and the appropriate action taken.\r
199  */\r
200 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
201 \r
202 /*\r
203  * Uses a critical section to determine if there is any data in a queue.\r
204  *\r
205  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
206  */\r
207 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
208 \r
209 /*\r
210  * Uses a critical section to determine if there is any space in a queue.\r
211  *\r
212  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
213  */\r
214 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;\r
215 \r
216 /*\r
217  * Copies an item into the queue, either at the front of the queue or the\r
218  * back of the queue.\r
219  */\r
220 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;\r
221 \r
222 /*\r
223  * Copies an item out of a queue.\r
224  */\r
225 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 /*\r
229  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
230  * accessing the queue event lists.\r
231  */\r
232 #define prvLockQueue( pxQueue )                                                         \\r
233         taskENTER_CRITICAL();                                                                   \\r
234         {                                                                                                               \\r
235                 if( ( pxQueue )->xRxLock == queueUNLOCKED )                     \\r
236                 {                                                                                                       \\r
237                         ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED;  \\r
238                 }                                                                                                       \\r
239                 if( ( pxQueue )->xTxLock == queueUNLOCKED )                     \\r
240                 {                                                                                                       \\r
241                         ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED;  \\r
242                 }                                                                                                       \\r
243         }                                                                                                               \\r
244         taskEXIT_CRITICAL()\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 \r
248 /*-----------------------------------------------------------\r
249  * PUBLIC QUEUE MANAGEMENT API documented in queue.h\r
250  *----------------------------------------------------------*/\r
251 \r
252 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )\r
253 {\r
254 xQUEUE *pxNewQueue;\r
255 size_t xQueueSizeInBytes;\r
256 xQueueHandle xReturn = NULL;\r
257 \r
258         /* Allocate the new queue structure. */\r
259         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
260         {\r
261                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
262                 if( pxNewQueue != NULL )\r
263                 {\r
264                         /* Create the list of pointers to queue items.  The queue is one byte\r
265                         longer than asked for to make wrap checking easier/faster. */\r
266                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;\r
267 \r
268                         pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );\r
269                         if( pxNewQueue->pcHead != NULL )\r
270                         {\r
271                                 /* Initialise the queue members as described above where the\r
272                                 queue type is defined. */\r
273                                 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );\r
274                                 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
275                                 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;\r
276                                 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - ( unsigned portBASE_TYPE ) 1U ) * uxItemSize );\r
277                                 pxNewQueue->uxLength = uxQueueLength;\r
278                                 pxNewQueue->uxItemSize = uxItemSize;\r
279                                 pxNewQueue->xRxLock = queueUNLOCKED;\r
280                                 pxNewQueue->xTxLock = queueUNLOCKED;\r
281 \r
282                                 /* Likewise ensure the event queues start with the correct state. */\r
283                                 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
284                                 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
285 \r
286                                 traceQUEUE_CREATE( pxNewQueue );\r
287                                 xReturn = pxNewQueue;\r
288                         }\r
289                         else\r
290                         {\r
291                                 traceQUEUE_CREATE_FAILED();\r
292                                 vPortFree( pxNewQueue );\r
293                         }\r
294                 }\r
295         }\r
296 \r
297         configASSERT( xReturn );\r
298 \r
299         return xReturn;\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 #if ( configUSE_MUTEXES == 1 )\r
304 \r
305         xQueueHandle xQueueCreateMutex( void )\r
306         {\r
307         xQUEUE *pxNewQueue;\r
308 \r
309                 /* Allocate the new queue structure. */\r
310                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
311                 if( pxNewQueue != NULL )\r
312                 {\r
313                         /* Information required for priority inheritance. */\r
314                         pxNewQueue->pxMutexHolder = NULL;\r
315                         pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
316 \r
317                         /* Queues used as a mutex no data is actually copied into or out\r
318                         of the queue. */\r
319                         pxNewQueue->pcWriteTo = NULL;\r
320                         pxNewQueue->pcReadFrom = NULL;\r
321 \r
322                         /* Each mutex has a length of 1 (like a binary semaphore) and\r
323                         an item size of 0 as nothing is actually copied into or out\r
324                         of the mutex. */\r
325                         pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
326                         pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;\r
327                         pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;\r
328                         pxNewQueue->xRxLock = queueUNLOCKED;\r
329                         pxNewQueue->xTxLock = queueUNLOCKED;\r
330 \r
331                         /* Ensure the event queues start with the correct state. */\r
332                         vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
333                         vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
334 \r
335                         /* Start with the semaphore in the expected state. */\r
336                         xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );\r
337 \r
338                         traceCREATE_MUTEX( pxNewQueue );\r
339                 }\r
340                 else\r
341                 {\r
342                         traceCREATE_MUTEX_FAILED();\r
343                 }\r
344 \r
345                 configASSERT( pxNewQueue );\r
346                 return pxNewQueue;\r
347         }\r
348 \r
349 #endif /* configUSE_MUTEXES */\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 #if configUSE_RECURSIVE_MUTEXES == 1\r
353 \r
354         portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )\r
355         {\r
356         portBASE_TYPE xReturn;\r
357 \r
358                 configASSERT( pxMutex );\r
359 \r
360                 /* If this is the task that holds the mutex then pxMutexHolder will not\r
361                 change outside of this task.  If this task does not hold the mutex then\r
362                 pxMutexHolder can never coincidentally equal the tasks handle, and as\r
363                 this is the only condition we are interested in it does not matter if\r
364                 pxMutexHolder is accessed simultaneously by another task.  Therefore no\r
365                 mutual exclusion is required to test the pxMutexHolder variable. */\r
366                 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
367                 {\r
368                         traceGIVE_MUTEX_RECURSIVE( pxMutex );\r
369 \r
370                         /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to\r
371                         the task handle, therefore no underflow check is required.  Also,\r
372                         uxRecursiveCallCount is only modified by the mutex holder, and as\r
373                         there can only be one, no mutual exclusion is required to modify the\r
374                         uxRecursiveCallCount member. */\r
375                         ( pxMutex->uxRecursiveCallCount )--;\r
376 \r
377                         /* Have we unwound the call count? */\r
378                         if( pxMutex->uxRecursiveCallCount == 0 )\r
379                         {\r
380                                 /* Return the mutex.  This will automatically unblock any other\r
381                                 task that might be waiting to access the mutex. */\r
382                                 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );\r
383                         }\r
384 \r
385                         xReturn = pdPASS;\r
386                 }\r
387                 else\r
388                 {\r
389                         /* We cannot give the mutex because we are not the holder. */\r
390                         xReturn = pdFAIL;\r
391 \r
392                         traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
393                 }\r
394 \r
395                 return xReturn;\r
396         }\r
397 \r
398 #endif /* configUSE_RECURSIVE_MUTEXES */\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 #if configUSE_RECURSIVE_MUTEXES == 1\r
402 \r
403         portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )\r
404         {\r
405         portBASE_TYPE xReturn;\r
406 \r
407                 configASSERT( pxMutex );\r
408 \r
409                 /* Comments regarding mutual exclusion as per those within\r
410                 xQueueGiveMutexRecursive(). */\r
411 \r
412                 traceTAKE_MUTEX_RECURSIVE( pxMutex );\r
413 \r
414                 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
415                 {\r
416                         ( pxMutex->uxRecursiveCallCount )++;\r
417                         xReturn = pdPASS;\r
418                 }\r
419                 else\r
420                 {\r
421                         xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );\r
422 \r
423                         /* pdPASS will only be returned if we successfully obtained the mutex,\r
424                         we may have blocked to reach here. */\r
425                         if( xReturn == pdPASS )\r
426                         {\r
427                                 ( pxMutex->uxRecursiveCallCount )++;\r
428                         }\r
429                         else\r
430                         {\r
431                                 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
432                         }\r
433                 }\r
434 \r
435                 return xReturn;\r
436         }\r
437 \r
438 #endif /* configUSE_RECURSIVE_MUTEXES */\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 #if configUSE_COUNTING_SEMAPHORES == 1\r
442 \r
443         xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )\r
444         {\r
445         xQueueHandle pxHandle;\r
446 \r
447                 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );\r
448 \r
449                 if( pxHandle != NULL )\r
450                 {\r
451                         pxHandle->uxMessagesWaiting = uxInitialCount;\r
452 \r
453                         traceCREATE_COUNTING_SEMAPHORE();\r
454                 }\r
455                 else\r
456                 {\r
457                         traceCREATE_COUNTING_SEMAPHORE_FAILED();\r
458                 }\r
459 \r
460                 configASSERT( pxHandle );\r
461                 return pxHandle;\r
462         }\r
463 \r
464 #endif /* configUSE_COUNTING_SEMAPHORES */\r
465 /*-----------------------------------------------------------*/\r
466 \r
467 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
468 {\r
469 signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
470 xTimeOutType xTimeOut;\r
471 \r
472         configASSERT( pxQueue );\r
473         configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
474 \r
475         /* This function relaxes the coding standard somewhat to allow return\r
476         statements within the function itself.  This is done in the interest\r
477         of execution time efficiency. */\r
478         for( ;; )\r
479         {\r
480                 taskENTER_CRITICAL();\r
481                 {\r
482                         /* Is there room on the queue now?  To be running we must be\r
483                         the highest priority task wanting to access the queue. */\r
484                         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
485                         {\r
486                                 traceQUEUE_SEND( pxQueue );\r
487                                 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
488 \r
489                                 /* If there was a task waiting for data to arrive on the\r
490                                 queue then unblock it now. */\r
491                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
492                                 {\r
493                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
494                                         {\r
495                                                 /* The unblocked task has a priority higher than\r
496                                                 our own so yield immediately.  Yes it is ok to do\r
497                                                 this from within the critical section - the kernel\r
498                                                 takes care of that. */\r
499                                                 portYIELD_WITHIN_API();\r
500                                         }\r
501                                 }\r
502 \r
503                                 taskEXIT_CRITICAL();\r
504 \r
505                                 /* Return to the original privilege level before exiting the\r
506                                 function. */\r
507                                 return pdPASS;\r
508                         }\r
509                         else\r
510                         {\r
511                                 if( xTicksToWait == ( portTickType ) 0 )\r
512                                 {\r
513                                         /* The queue was full and no block time is specified (or\r
514                                         the block time has expired) so leave now. */\r
515                                         taskEXIT_CRITICAL();\r
516 \r
517                                         /* Return to the original privilege level before exiting\r
518                                         the function. */\r
519                                         traceQUEUE_SEND_FAILED( pxQueue );\r
520                                         return errQUEUE_FULL;\r
521                                 }\r
522                                 else if( xEntryTimeSet == pdFALSE )\r
523                                 {\r
524                                         /* The queue was full and a block time was specified so\r
525                                         configure the timeout structure. */\r
526                                         vTaskSetTimeOutState( &xTimeOut );\r
527                                         xEntryTimeSet = pdTRUE;\r
528                                 }\r
529                         }\r
530                 }\r
531                 taskEXIT_CRITICAL();\r
532 \r
533                 /* Interrupts and other tasks can send to and receive from the queue\r
534                 now the critical section has been exited. */\r
535 \r
536                 vTaskSuspendAll();\r
537                 prvLockQueue( pxQueue );\r
538 \r
539                 /* Update the timeout state to see if it has expired yet. */\r
540                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
541                 {\r
542                         if( prvIsQueueFull( pxQueue ) )\r
543                         {\r
544                                 traceBLOCKING_ON_QUEUE_SEND( pxQueue );\r
545                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
546 \r
547                                 /* Unlocking the queue means queue events can effect the\r
548                                 event list.  It is possible     that interrupts occurring now\r
549                                 remove this task from the event list again - but as the\r
550                                 scheduler is suspended the task will go onto the pending\r
551                                 ready last instead of the actual ready list. */\r
552                                 prvUnlockQueue( pxQueue );\r
553 \r
554                                 /* Resuming the scheduler will move tasks from the pending\r
555                                 ready list into the ready list - so it is feasible that this\r
556                                 task is already in a ready list before it yields - in which\r
557                                 case the yield will not cause a context switch unless there\r
558                                 is also a higher priority task in the pending ready list. */\r
559                                 if( !xTaskResumeAll() )\r
560                                 {\r
561                                         portYIELD_WITHIN_API();\r
562                                 }\r
563                         }\r
564                         else\r
565                         {\r
566                                 /* Try again. */\r
567                                 prvUnlockQueue( pxQueue );\r
568                                 ( void ) xTaskResumeAll();\r
569                         }\r
570                 }\r
571                 else\r
572                 {\r
573                         /* The timeout has expired. */\r
574                         prvUnlockQueue( pxQueue );\r
575                         ( void ) xTaskResumeAll();\r
576 \r
577                         /* Return to the original privilege level before exiting the\r
578                         function. */\r
579                         traceQUEUE_SEND_FAILED( pxQueue );\r
580                         return errQUEUE_FULL;\r
581                 }\r
582         }\r
583 }\r
584 /*-----------------------------------------------------------*/\r
585 \r
586 #if configUSE_ALTERNATIVE_API == 1\r
587 \r
588         signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
589         {\r
590         signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
591         xTimeOutType xTimeOut;\r
592 \r
593                 configASSERT( pxQueue );\r
594                 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
595 \r
596                 for( ;; )\r
597                 {\r
598                         taskENTER_CRITICAL();\r
599                         {\r
600                                 /* Is there room on the queue now?  To be running we must be\r
601                                 the highest priority task wanting to access the queue. */\r
602                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
603                                 {\r
604                                         traceQUEUE_SEND( pxQueue );\r
605                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
606 \r
607                                         /* If there was a task waiting for data to arrive on the\r
608                                         queue then unblock it now. */\r
609                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
610                                         {\r
611                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
612                                                 {\r
613                                                         /* The unblocked task has a priority higher than\r
614                                                         our own so yield immediately. */\r
615                                                         portYIELD_WITHIN_API();\r
616                                                 }\r
617                                         }\r
618 \r
619                                         taskEXIT_CRITICAL();\r
620                                         return pdPASS;\r
621                                 }\r
622                                 else\r
623                                 {\r
624                                         if( xTicksToWait == ( portTickType ) 0 )\r
625                                         {\r
626                                                 taskEXIT_CRITICAL();\r
627                                                 return errQUEUE_FULL;\r
628                                         }\r
629                                         else if( xEntryTimeSet == pdFALSE )\r
630                                         {\r
631                                                 vTaskSetTimeOutState( &xTimeOut );\r
632                                                 xEntryTimeSet = pdTRUE;\r
633                                         }\r
634                                 }\r
635                         }\r
636                         taskEXIT_CRITICAL();\r
637 \r
638                         taskENTER_CRITICAL();\r
639                         {\r
640                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
641                                 {\r
642                                         if( prvIsQueueFull( pxQueue ) )\r
643                                         {\r
644                                                 traceBLOCKING_ON_QUEUE_SEND( pxQueue );\r
645                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
646                                                 portYIELD_WITHIN_API();\r
647                                         }\r
648                                 }\r
649                                 else\r
650                                 {\r
651                                         taskEXIT_CRITICAL();\r
652                                         traceQUEUE_SEND_FAILED( pxQueue );\r
653                                         return errQUEUE_FULL;\r
654                                 }\r
655                         }\r
656                         taskEXIT_CRITICAL();\r
657                 }\r
658         }\r
659 \r
660 #endif /* configUSE_ALTERNATIVE_API */\r
661 /*-----------------------------------------------------------*/\r
662 \r
663 #if configUSE_ALTERNATIVE_API == 1\r
664 \r
665         signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
666         {\r
667         signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
668         xTimeOutType xTimeOut;\r
669         signed char *pcOriginalReadPosition;\r
670 \r
671                 configASSERT( pxQueue );\r
672                 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
673 \r
674                 for( ;; )\r
675                 {\r
676                         taskENTER_CRITICAL();\r
677                         {\r
678                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
679                                 {\r
680                                         /* Remember our read position in case we are just peeking. */\r
681                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
682 \r
683                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
684 \r
685                                         if( xJustPeeking == pdFALSE )\r
686                                         {\r
687                                                 traceQUEUE_RECEIVE( pxQueue );\r
688 \r
689                                                 /* We are actually removing data. */\r
690                                                 --( pxQueue->uxMessagesWaiting );\r
691 \r
692                                                 #if ( configUSE_MUTEXES == 1 )\r
693                                                 {\r
694                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
695                                                         {\r
696                                                                 /* Record the information required to implement\r
697                                                                 priority inheritance should it become necessary. */\r
698                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
699                                                         }\r
700                                                 }\r
701                                                 #endif\r
702 \r
703                                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
704                                                 {\r
705                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
706                                                         {\r
707                                                                 portYIELD_WITHIN_API();\r
708                                                         }\r
709                                                 }\r
710                                         }\r
711                                         else\r
712                                         {\r
713                                                 traceQUEUE_PEEK( pxQueue );\r
714 \r
715                                                 /* We are not removing the data, so reset our read\r
716                                                 pointer. */\r
717                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;\r
718 \r
719                                                 /* The data is being left in the queue, so see if there are\r
720                                                 any other tasks waiting for the data. */\r
721                                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
722                                                 {\r
723                                                         /* Tasks that are removed from the event list will get added to\r
724                                                         the pending ready list as the scheduler is still suspended. */\r
725                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
726                                                         {\r
727                                                                 /* The task waiting has a higher priority than this task. */\r
728                                                                 portYIELD_WITHIN_API();\r
729                                                         }\r
730                                                 }\r
731 \r
732                                         }\r
733 \r
734                                         taskEXIT_CRITICAL();\r
735                                         return pdPASS;\r
736                                 }\r
737                                 else\r
738                                 {\r
739                                         if( xTicksToWait == ( portTickType ) 0 )\r
740                                         {\r
741                                                 taskEXIT_CRITICAL();\r
742                                                 traceQUEUE_RECEIVE_FAILED( pxQueue );\r
743                                                 return errQUEUE_EMPTY;\r
744                                         }\r
745                                         else if( xEntryTimeSet == pdFALSE )\r
746                                         {\r
747                                                 vTaskSetTimeOutState( &xTimeOut );\r
748                                                 xEntryTimeSet = pdTRUE;\r
749                                         }\r
750                                 }\r
751                         }\r
752                         taskEXIT_CRITICAL();\r
753 \r
754                         taskENTER_CRITICAL();\r
755                         {\r
756                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
757                                 {\r
758                                         if( prvIsQueueEmpty( pxQueue ) )\r
759                                         {\r
760                                                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
761 \r
762                                                 #if ( configUSE_MUTEXES == 1 )\r
763                                                 {\r
764                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
765                                                         {\r
766                                                                 portENTER_CRITICAL();\r
767                                                                         vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );\r
768                                                                 portEXIT_CRITICAL();\r
769                                                         }\r
770                                                 }\r
771                                                 #endif\r
772 \r
773                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
774                                                 portYIELD_WITHIN_API();\r
775                                         }\r
776                                 }\r
777                                 else\r
778                                 {\r
779                                         taskEXIT_CRITICAL();\r
780                                         traceQUEUE_RECEIVE_FAILED( pxQueue );\r
781                                         return errQUEUE_EMPTY;\r
782                                 }\r
783                         }\r
784                         taskEXIT_CRITICAL();\r
785                 }\r
786         }\r
787 \r
788 \r
789 #endif /* configUSE_ALTERNATIVE_API */\r
790 /*-----------------------------------------------------------*/\r
791 \r
792 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )\r
793 {\r
794 signed portBASE_TYPE xReturn;\r
795 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
796 \r
797         configASSERT( pxQueue );\r
798         configASSERT( pxHigherPriorityTaskWoken );\r
799         configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
800 \r
801         /* Similar to xQueueGenericSend, except we don't block if there is no room\r
802         in the queue.  Also we don't directly wake a task that was blocked on a\r
803         queue read, instead we return a flag to say whether a context switch is\r
804         required or not (i.e. has a task with a higher priority than us been woken\r
805         by this post). */\r
806         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
807         {\r
808                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
809                 {\r
810                         traceQUEUE_SEND_FROM_ISR( pxQueue );\r
811 \r
812                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
813 \r
814                         /* If the queue is locked we do not alter the event list.  This will\r
815                         be done when the queue is unlocked later. */\r
816                         if( pxQueue->xTxLock == queueUNLOCKED )\r
817                         {\r
818                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
819                                 {\r
820                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
821                                         {\r
822                                                 /* The task waiting has a higher priority so record that a\r
823                                                 context switch is required. */\r
824                                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
825                                         }\r
826                                 }\r
827                         }\r
828                         else\r
829                         {\r
830                                 /* Increment the lock count so the task that unlocks the queue\r
831                                 knows that data was posted while it was locked. */\r
832                                 ++( pxQueue->xTxLock );\r
833                         }\r
834 \r
835                         xReturn = pdPASS;\r
836                 }\r
837                 else\r
838                 {\r
839                         traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );\r
840                         xReturn = errQUEUE_FULL;\r
841                 }\r
842         }\r
843         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
844 \r
845         return xReturn;\r
846 }\r
847 /*-----------------------------------------------------------*/\r
848 \r
849 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
850 {\r
851 signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
852 xTimeOutType xTimeOut;\r
853 signed char *pcOriginalReadPosition;\r
854 \r
855         configASSERT( pxQueue );\r
856         configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
857 \r
858         /* This function relaxes the coding standard somewhat to allow return\r
859         statements within the function itself.  This is done in the interest\r
860         of execution time efficiency. */\r
861 \r
862         for( ;; )\r
863         {\r
864                 taskENTER_CRITICAL();\r
865                 {\r
866                         /* Is there data in the queue now?  To be running we must be\r
867                         the highest priority task wanting to access the queue. */\r
868                         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
869                         {\r
870                                 /* Remember our read position in case we are just peeking. */\r
871                                 pcOriginalReadPosition = pxQueue->pcReadFrom;\r
872 \r
873                                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
874 \r
875                                 if( xJustPeeking == pdFALSE )\r
876                                 {\r
877                                         traceQUEUE_RECEIVE( pxQueue );\r
878 \r
879                                         /* We are actually removing data. */\r
880                                         --( pxQueue->uxMessagesWaiting );\r
881 \r
882                                         #if ( configUSE_MUTEXES == 1 )\r
883                                         {\r
884                                                 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
885                                                 {\r
886                                                         /* Record the information required to implement\r
887                                                         priority inheritance should it become necessary. */\r
888                                                         pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
889                                                 }\r
890                                         }\r
891                                         #endif\r
892 \r
893                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
894                                         {\r
895                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
896                                                 {\r
897                                                         portYIELD_WITHIN_API();\r
898                                                 }\r
899                                         }\r
900                                 }\r
901                                 else\r
902                                 {\r
903                                         traceQUEUE_PEEK( pxQueue );\r
904 \r
905                                         /* We are not removing the data, so reset our read\r
906                                         pointer. */\r
907                                         pxQueue->pcReadFrom = pcOriginalReadPosition;\r
908 \r
909                                         /* The data is being left in the queue, so see if there are\r
910                                         any other tasks waiting for the data. */\r
911                                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
912                                         {\r
913                                                 /* Tasks that are removed from the event list will get added to\r
914                                                 the pending ready list as the scheduler is still suspended. */\r
915                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
916                                                 {\r
917                                                         /* The task waiting has a higher priority than this task. */\r
918                                                         portYIELD_WITHIN_API();\r
919                                                 }\r
920                                         }\r
921 \r
922                                 }\r
923 \r
924                                 taskEXIT_CRITICAL();\r
925                                 return pdPASS;\r
926                         }\r
927                         else\r
928                         {\r
929                                 if( xTicksToWait == ( portTickType ) 0 )\r
930                                 {\r
931                                         /* The queue was empty and no block time is specified (or\r
932                                         the block time has expired) so leave now. */\r
933                                         taskEXIT_CRITICAL();\r
934                                         traceQUEUE_RECEIVE_FAILED( pxQueue );\r
935                                         return errQUEUE_EMPTY;\r
936                                 }\r
937                                 else if( xEntryTimeSet == pdFALSE )\r
938                                 {\r
939                                         /* The queue was empty and a block time was specified so\r
940                                         configure the timeout structure. */\r
941                                         vTaskSetTimeOutState( &xTimeOut );\r
942                                         xEntryTimeSet = pdTRUE;\r
943                                 }\r
944                         }\r
945                 }\r
946                 taskEXIT_CRITICAL();\r
947 \r
948                 /* Interrupts and other tasks can send to and receive from the queue\r
949                 now the critical section has been exited. */\r
950 \r
951                 vTaskSuspendAll();\r
952                 prvLockQueue( pxQueue );\r
953 \r
954                 /* Update the timeout state to see if it has expired yet. */\r
955                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
956                 {\r
957                         if( prvIsQueueEmpty( pxQueue ) )\r
958                         {\r
959                                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
960 \r
961                                 #if ( configUSE_MUTEXES == 1 )\r
962                                 {\r
963                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
964                                         {\r
965                                                 portENTER_CRITICAL();\r
966                                                 {\r
967                                                         vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );\r
968                                                 }\r
969                                                 portEXIT_CRITICAL();\r
970                                         }\r
971                                 }\r
972                                 #endif\r
973 \r
974                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
975                                 prvUnlockQueue( pxQueue );\r
976                                 if( !xTaskResumeAll() )\r
977                                 {\r
978                                         portYIELD_WITHIN_API();\r
979                                 }\r
980                         }\r
981                         else\r
982                         {\r
983                                 /* Try again. */\r
984                                 prvUnlockQueue( pxQueue );\r
985                                 ( void ) xTaskResumeAll();\r
986                         }\r
987                 }\r
988                 else\r
989                 {\r
990                         prvUnlockQueue( pxQueue );\r
991                         ( void ) xTaskResumeAll();\r
992                         traceQUEUE_RECEIVE_FAILED( pxQueue );\r
993                         return errQUEUE_EMPTY;\r
994                 }\r
995         }\r
996 }\r
997 /*-----------------------------------------------------------*/\r
998 \r
999 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )\r
1000 {\r
1001 signed portBASE_TYPE xReturn;\r
1002 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
1003 \r
1004         configASSERT( pxQueue );\r
1005         configASSERT( pxTaskWoken );\r
1006         configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
1007 \r
1008         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1009         {\r
1010                 /* We cannot block from an ISR, so check there is data available. */\r
1011                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1012                 {\r
1013                         traceQUEUE_RECEIVE_FROM_ISR( pxQueue );\r
1014 \r
1015                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
1016                         --( pxQueue->uxMessagesWaiting );\r
1017 \r
1018                         /* If the queue is locked we will not modify the event list.  Instead\r
1019                         we update the lock count so the task that unlocks the queue will know\r
1020                         that an ISR has removed data while the queue was locked. */\r
1021                         if( pxQueue->xRxLock == queueUNLOCKED )\r
1022                         {\r
1023                                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1024                                 {\r
1025                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1026                                         {\r
1027                                                 /* The task waiting has a higher priority than us so\r
1028                                                 force a context switch. */\r
1029                                                 *pxTaskWoken = pdTRUE;\r
1030                                         }\r
1031                                 }\r
1032                         }\r
1033                         else\r
1034                         {\r
1035                                 /* Increment the lock count so the task that unlocks the queue\r
1036                                 knows that data was removed while it was locked. */\r
1037                                 ++( pxQueue->xRxLock );\r
1038                         }\r
1039 \r
1040                         xReturn = pdPASS;\r
1041                 }\r
1042                 else\r
1043                 {\r
1044                         xReturn = pdFAIL;\r
1045                         traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );\r
1046                 }\r
1047         }\r
1048         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1049 \r
1050         return xReturn;\r
1051 }\r
1052 /*-----------------------------------------------------------*/\r
1053 \r
1054 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )\r
1055 {\r
1056 unsigned portBASE_TYPE uxReturn;\r
1057 \r
1058         configASSERT( pxQueue );\r
1059 \r
1060         taskENTER_CRITICAL();\r
1061                 uxReturn = pxQueue->uxMessagesWaiting;\r
1062         taskEXIT_CRITICAL();\r
1063 \r
1064         return uxReturn;\r
1065 }\r
1066 /*-----------------------------------------------------------*/\r
1067 \r
1068 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )\r
1069 {\r
1070 unsigned portBASE_TYPE uxReturn;\r
1071 \r
1072         configASSERT( pxQueue );\r
1073 \r
1074         uxReturn = pxQueue->uxMessagesWaiting;\r
1075 \r
1076         return uxReturn;\r
1077 }\r
1078 /*-----------------------------------------------------------*/\r
1079 \r
1080 void vQueueDelete( xQueueHandle pxQueue )\r
1081 {\r
1082         configASSERT( pxQueue );\r
1083 \r
1084         traceQUEUE_DELETE( pxQueue );\r
1085         vQueueUnregisterQueue( pxQueue );\r
1086         vPortFree( pxQueue->pcHead );\r
1087         vPortFree( pxQueue );\r
1088 }\r
1089 /*-----------------------------------------------------------*/\r
1090 \r
1091 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )\r
1092 {\r
1093         if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )\r
1094         {\r
1095                 #if ( configUSE_MUTEXES == 1 )\r
1096                 {\r
1097                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
1098                         {\r
1099                                 /* The mutex is no longer being held. */\r
1100                                 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );\r
1101                                 pxQueue->pxMutexHolder = NULL;\r
1102                         }\r
1103                 }\r
1104                 #endif\r
1105         }\r
1106         else if( xPosition == queueSEND_TO_BACK )\r
1107         {\r
1108                 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
1109                 pxQueue->pcWriteTo += pxQueue->uxItemSize;\r
1110                 if( pxQueue->pcWriteTo >= pxQueue->pcTail )\r
1111                 {\r
1112                         pxQueue->pcWriteTo = pxQueue->pcHead;\r
1113                 }\r
1114         }\r
1115         else\r
1116         {\r
1117                 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
1118                 pxQueue->pcReadFrom -= pxQueue->uxItemSize;\r
1119                 if( pxQueue->pcReadFrom < pxQueue->pcHead )\r
1120                 {\r
1121                         pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
1122                 }\r
1123         }\r
1124 \r
1125         ++( pxQueue->uxMessagesWaiting );\r
1126 }\r
1127 /*-----------------------------------------------------------*/\r
1128 \r
1129 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )\r
1130 {\r
1131         if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
1132         {\r
1133                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1134                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1135                 {\r
1136                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1137                 }\r
1138                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1139         }\r
1140 }\r
1141 /*-----------------------------------------------------------*/\r
1142 \r
1143 static void prvUnlockQueue( xQueueHandle pxQueue )\r
1144 {\r
1145         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
1146 \r
1147         /* The lock counts contains the number of extra data items placed or\r
1148         removed from the queue while the queue was locked.  When a queue is\r
1149         locked items can be added or removed, but the event lists cannot be\r
1150         updated. */\r
1151         taskENTER_CRITICAL();\r
1152         {\r
1153                 /* See if data was added to the queue while it was locked. */\r
1154                 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )\r
1155                 {\r
1156                         /* Data was posted while the queue was locked.  Are any tasks\r
1157                         blocked waiting for data to become available? */\r
1158                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1159                         {\r
1160                                 /* Tasks that are removed from the event list will get added to\r
1161                                 the pending ready list as the scheduler is still suspended. */\r
1162                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1163                                 {\r
1164                                         /* The task waiting has a higher priority so record that a\r
1165                                         context switch is required. */\r
1166                                         vTaskMissedYield();\r
1167                                 }\r
1168 \r
1169                                 --( pxQueue->xTxLock );\r
1170                         }\r
1171                         else\r
1172                         {\r
1173                                 break;\r
1174                         }\r
1175                 }\r
1176 \r
1177                 pxQueue->xTxLock = queueUNLOCKED;\r
1178         }\r
1179         taskEXIT_CRITICAL();\r
1180 \r
1181         /* Do the same for the Rx lock. */\r
1182         taskENTER_CRITICAL();\r
1183         {\r
1184                 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )\r
1185                 {\r
1186                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1187                         {\r
1188                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1189                                 {\r
1190                                         vTaskMissedYield();\r
1191                                 }\r
1192 \r
1193                                 --( pxQueue->xRxLock );\r
1194                         }\r
1195                         else\r
1196                         {\r
1197                                 break;\r
1198                         }\r
1199                 }\r
1200 \r
1201                 pxQueue->xRxLock = queueUNLOCKED;\r
1202         }\r
1203         taskEXIT_CRITICAL();\r
1204 }\r
1205 /*-----------------------------------------------------------*/\r
1206 \r
1207 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )\r
1208 {\r
1209 signed portBASE_TYPE xReturn;\r
1210 \r
1211         taskENTER_CRITICAL();\r
1212                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
1213         taskEXIT_CRITICAL();\r
1214 \r
1215         return xReturn;\r
1216 }\r
1217 /*-----------------------------------------------------------*/\r
1218 \r
1219 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )\r
1220 {\r
1221 signed portBASE_TYPE xReturn;\r
1222 \r
1223         configASSERT( pxQueue );\r
1224         xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
1225 \r
1226         return xReturn;\r
1227 }\r
1228 /*-----------------------------------------------------------*/\r
1229 \r
1230 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )\r
1231 {\r
1232 signed portBASE_TYPE xReturn;\r
1233 \r
1234         taskENTER_CRITICAL();\r
1235                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
1236         taskEXIT_CRITICAL();\r
1237 \r
1238         return xReturn;\r
1239 }\r
1240 /*-----------------------------------------------------------*/\r
1241 \r
1242 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )\r
1243 {\r
1244 signed portBASE_TYPE xReturn;\r
1245 \r
1246         configASSERT( pxQueue );\r
1247         xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
1248 \r
1249         return xReturn;\r
1250 }\r
1251 /*-----------------------------------------------------------*/\r
1252 \r
1253 #if configUSE_CO_ROUTINES == 1\r
1254 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
1255 {\r
1256 signed portBASE_TYPE xReturn;\r
1257 \r
1258         /* If the queue is already full we may have to block.  A critical section\r
1259         is required to prevent an interrupt removing something from the queue\r
1260         between the check to see if the queue is full and blocking on the queue. */\r
1261         portDISABLE_INTERRUPTS();\r
1262         {\r
1263                 if( prvIsQueueFull( pxQueue ) )\r
1264                 {\r
1265                         /* The queue is full - do we want to block or just leave without\r
1266                         posting? */\r
1267                         if( xTicksToWait > ( portTickType ) 0 )\r
1268                         {\r
1269                                 /* As this is called from a coroutine we cannot block directly, but\r
1270                                 return indicating that we need to block. */\r
1271                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );\r
1272                                 portENABLE_INTERRUPTS();\r
1273                                 return errQUEUE_BLOCKED;\r
1274                         }\r
1275                         else\r
1276                         {\r
1277                                 portENABLE_INTERRUPTS();\r
1278                                 return errQUEUE_FULL;\r
1279                         }\r
1280                 }\r
1281         }\r
1282         portENABLE_INTERRUPTS();\r
1283 \r
1284         portNOP();\r
1285 \r
1286         portDISABLE_INTERRUPTS();\r
1287         {\r
1288                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1289                 {\r
1290                         /* There is room in the queue, copy the data into the queue. */\r
1291                         prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1292                         xReturn = pdPASS;\r
1293 \r
1294                         /* Were any co-routines waiting for data to become available? */\r
1295                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1296                         {\r
1297                                 /* In this instance the co-routine could be placed directly\r
1298                                 into the ready list as we are within a critical section.\r
1299                                 Instead the same pending ready list mechanism is used as if\r
1300                                 the event were caused from within an interrupt. */\r
1301                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1302                                 {\r
1303                                         /* The co-routine waiting has a higher priority so record\r
1304                                         that a yield might be appropriate. */\r
1305                                         xReturn = errQUEUE_YIELD;\r
1306                                 }\r
1307                         }\r
1308                 }\r
1309                 else\r
1310                 {\r
1311                         xReturn = errQUEUE_FULL;\r
1312                 }\r
1313         }\r
1314         portENABLE_INTERRUPTS();\r
1315 \r
1316         return xReturn;\r
1317 }\r
1318 #endif\r
1319 /*-----------------------------------------------------------*/\r
1320 \r
1321 #if configUSE_CO_ROUTINES == 1\r
1322 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
1323 {\r
1324 signed portBASE_TYPE xReturn;\r
1325 \r
1326         /* If the queue is already empty we may have to block.  A critical section\r
1327         is required to prevent an interrupt adding something to the queue\r
1328         between the check to see if the queue is empty and blocking on the queue. */\r
1329         portDISABLE_INTERRUPTS();\r
1330         {\r
1331                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
1332                 {\r
1333                         /* There are no messages in the queue, do we want to block or just\r
1334                         leave with nothing? */\r
1335                         if( xTicksToWait > ( portTickType ) 0 )\r
1336                         {\r
1337                                 /* As this is a co-routine we cannot block directly, but return\r
1338                                 indicating that we need to block. */\r
1339                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );\r
1340                                 portENABLE_INTERRUPTS();\r
1341                                 return errQUEUE_BLOCKED;\r
1342                         }\r
1343                         else\r
1344                         {\r
1345                                 portENABLE_INTERRUPTS();\r
1346                                 return errQUEUE_FULL;\r
1347                         }\r
1348                 }\r
1349         }\r
1350         portENABLE_INTERRUPTS();\r
1351 \r
1352         portNOP();\r
1353 \r
1354         portDISABLE_INTERRUPTS();\r
1355         {\r
1356                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1357                 {\r
1358                         /* Data is available from the queue. */\r
1359                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1360                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1361                         {\r
1362                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
1363                         }\r
1364                         --( pxQueue->uxMessagesWaiting );\r
1365                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1366 \r
1367                         xReturn = pdPASS;\r
1368 \r
1369                         /* Were any co-routines waiting for space to become available? */\r
1370                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1371                         {\r
1372                                 /* In this instance the co-routine could be placed directly\r
1373                                 into the ready list as we are within a critical section.\r
1374                                 Instead the same pending ready list mechanism is used as if\r
1375                                 the event were caused from within an interrupt. */\r
1376                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1377                                 {\r
1378                                         xReturn = errQUEUE_YIELD;\r
1379                                 }\r
1380                         }\r
1381                 }\r
1382                 else\r
1383                 {\r
1384                         xReturn = pdFAIL;\r
1385                 }\r
1386         }\r
1387         portENABLE_INTERRUPTS();\r
1388 \r
1389         return xReturn;\r
1390 }\r
1391 #endif\r
1392 /*-----------------------------------------------------------*/\r
1393 \r
1394 \r
1395 \r
1396 #if configUSE_CO_ROUTINES == 1\r
1397 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
1398 {\r
1399         /* Cannot block within an ISR so if there is no space on the queue then\r
1400         exit without doing anything. */\r
1401         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1402         {\r
1403                 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1404 \r
1405                 /* We only want to wake one co-routine per ISR, so check that a\r
1406                 co-routine has not already been woken. */\r
1407                 if( !xCoRoutinePreviouslyWoken )\r
1408                 {\r
1409                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1410                         {\r
1411                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1412                                 {\r
1413                                         return pdTRUE;\r
1414                                 }\r
1415                         }\r
1416                 }\r
1417         }\r
1418 \r
1419         return xCoRoutinePreviouslyWoken;\r
1420 }\r
1421 #endif\r
1422 /*-----------------------------------------------------------*/\r
1423 \r
1424 #if configUSE_CO_ROUTINES == 1\r
1425 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
1426 {\r
1427 signed portBASE_TYPE xReturn;\r
1428 \r
1429         /* We cannot block from an ISR, so check there is data available. If\r
1430         not then just leave without doing anything. */\r
1431         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1432         {\r
1433                 /* Copy the data from the queue. */\r
1434                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1435                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1436                 {\r
1437                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1438                 }\r
1439                 --( pxQueue->uxMessagesWaiting );\r
1440                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1441 \r
1442                 if( !( *pxCoRoutineWoken ) )\r
1443                 {\r
1444                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1445                         {\r
1446                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1447                                 {\r
1448                                         *pxCoRoutineWoken = pdTRUE;\r
1449                                 }\r
1450                         }\r
1451                 }\r
1452 \r
1453                 xReturn = pdPASS;\r
1454         }\r
1455         else\r
1456         {\r
1457                 xReturn = pdFAIL;\r
1458         }\r
1459 \r
1460         return xReturn;\r
1461 }\r
1462 #endif\r
1463 /*-----------------------------------------------------------*/\r
1464 \r
1465 #if configQUEUE_REGISTRY_SIZE > 0\r
1466 \r
1467         void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )\r
1468         {\r
1469         unsigned portBASE_TYPE ux;\r
1470 \r
1471                 /* See if there is an empty space in the registry.  A NULL name denotes\r
1472                 a free slot. */\r
1473                 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < configQUEUE_REGISTRY_SIZE; ux++ )\r
1474                 {\r
1475                         if( xQueueRegistry[ ux ].pcQueueName == NULL )\r
1476                         {\r
1477                                 /* Store the information on this queue. */\r
1478                                 xQueueRegistry[ ux ].pcQueueName = pcQueueName;\r
1479                                 xQueueRegistry[ ux ].xHandle = xQueue;\r
1480                                 break;\r
1481                         }\r
1482                 }\r
1483         }\r
1484 \r
1485 #endif\r
1486 /*-----------------------------------------------------------*/\r
1487 \r
1488 #if configQUEUE_REGISTRY_SIZE > 0\r
1489 \r
1490         static void vQueueUnregisterQueue( xQueueHandle xQueue )\r
1491         {\r
1492         unsigned portBASE_TYPE ux;\r
1493 \r
1494                 /* See if the handle of the queue being unregistered in actually in the\r
1495                 registry. */\r
1496                 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < configQUEUE_REGISTRY_SIZE; ux++ )\r
1497                 {\r
1498                         if( xQueueRegistry[ ux ].xHandle == xQueue )\r
1499                         {\r
1500                                 /* Set the name to NULL to show that this slot if free again. */\r
1501                                 xQueueRegistry[ ux ].pcQueueName = NULL;\r
1502                                 break;\r
1503                         }\r
1504                 }\r
1505 \r
1506         }\r
1507 \r
1508 #endif\r
1509 /*-----------------------------------------------------------*/\r
1510 \r
1511 #if configUSE_TIMERS == 1\r
1512 \r
1513         void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )\r
1514         {\r
1515                 /* This function should not be called by application code hence the\r
1516                 'Restricted' in its name.  It is not part of the public API.  It is\r
1517                 designed for use by kernel code, and has special calling requirements.\r
1518                 It can result in vListInsert() being called on a list that can only\r
1519                 possibly ever have one item in it, so the list will be fast, but even\r
1520                 so it should be called with the scheduler locked and not from a critical\r
1521                 section. */\r
1522 \r
1523                 /* Only do anything if there are no messages in the queue.  This function\r
1524                 will not actually cause the task to block, just place it on a blocked\r
1525                 list.  It will not block until the scheduler is unlocked - at which\r
1526                 time a yield will be performed.  If an item is added to the queue while\r
1527                 the queue is locked, and the calling task blocks on the queue, then the\r
1528                 calling task will be immediately unblocked when the queue is unlocked. */\r
1529                 prvLockQueue( pxQueue );\r
1530                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )\r
1531                 {\r
1532                         /* There is nothing in the queue, block for the specified period. */\r
1533                         vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
1534                 }\r
1535                 prvUnlockQueue( pxQueue );\r
1536         }\r
1537 \r
1538 #endif\r
1539 \r