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