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