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