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