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