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