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