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