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