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