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