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