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