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