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