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