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