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