]> git.sur5r.net Git - freertos/blob - Source/queue.c
Added xQueueSendToBack, xQueueSendToFront, xQueuePeek and xSemaphoreCreateMutex ...
[freertos] / Source / queue.c
1 /*\r
2         FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 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         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37 Changes from V1.01\r
38 \r
39         + More use of 8bit data types.\r
40         + Function name prefixes changed where the data type returned has changed.\r
41 \r
42 Changed from V2.0.0\r
43 \r
44         + Added the queue locking mechanism and make more use of the scheduler\r
45           suspension feature to minimise the time interrupts have to be disabled\r
46           when accessing a queue.\r
47 \r
48 Changed from V2.2.0\r
49 \r
50         + Explicit use of 'signed' qualifier on portCHAR types added.\r
51 \r
52 Changes from V3.0.0\r
53 \r
54         + API changes as described on the FreeRTOS.org WEB site.\r
55 \r
56 Changes from V3.2.3\r
57 \r
58         + Added the queue functions that can be used from co-routines.\r
59 \r
60 Changes from V4.0.5\r
61 \r
62         + Added a loop within xQueueSend() and xQueueReceive() to prevent the\r
63           functions exiting when a block time remains and the function has\r
64           not completed.\r
65 \r
66 Changes from V4.1.2:\r
67 \r
68         + BUG FIX:  Removed the call to prvIsQueueEmpty from within xQueueCRReceive\r
69           as it exited with interrupts enabled.  Thanks Paul Katz.\r
70 \r
71 Changes from V4.1.3:\r
72 \r
73         + Modified xQueueSend() and xQueueReceive() to handle the (very unlikely)\r
74         case whereby a task unblocking due to a temporal event can remove/send an\r
75         item from/to a queue when a higher priority task is     still blocked on the\r
76         queue.  This modification is a result of the SafeRTOS testing.\r
77 */\r
78 \r
79 #include <stdlib.h>\r
80 #include <string.h>\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "croutine.h"\r
84 \r
85 /*-----------------------------------------------------------\r
86  * PUBLIC LIST API documented in list.h\r
87  *----------------------------------------------------------*/\r
88 \r
89 /* Constants used with the cRxLock and cTxLock structure members. */\r
90 #define queueUNLOCKED   ( ( signed portBASE_TYPE ) -1 )\r
91 #define queueERRONEOUS_UNBLOCK                                  ( -1 )\r
92 \r
93 /* For internal use only. */\r
94 #define queueSEND_TO_BACK       ( 0 )\r
95 #define queueSEND_TO_FRONT      ( 1 )\r
96 \r
97 /* Effectively make a union out of the xQUEUE structure. */\r
98 #define pxMutexHolder                           pcTail\r
99 #define uxQueueType                                     pcHead\r
100 #define queueQUEUE_IS_MUTEX                     NULL\r
101 \r
102 /*\r
103  * Definition of the queue used by the scheduler.\r
104  * Items are queued by copy, not reference.\r
105  */\r
106 typedef struct QueueDefinition\r
107 {\r
108         signed portCHAR *pcHead;                                /*< Points to the beginning of the queue storage area. */\r
109         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
110 \r
111         signed portCHAR *pcWriteTo;                             /*< Points to the free next place in the storage area. */\r
112         signed portCHAR *pcReadFrom;                    /*< Points to the last place that a queued item was read from. */\r
113 \r
114         xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
115         xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
116 \r
117         unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */\r
118         unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
119         unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */\r
120 \r
121         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
122         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
123 } xQUEUE;\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Inside this file xQueueHandle is a pointer to a xQUEUE structure.\r
128  * To keep the definition private the API header file defines it as a\r
129  * pointer to void.\r
130  */\r
131 typedef xQUEUE * xQueueHandle;\r
132 \r
133 /*\r
134  * Prototypes for public functions are included here so we don't have to\r
135  * include the API header file (as it defines xQueueHandle differently).  These\r
136  * functions are documented in the API header file.\r
137  */\r
138 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );\r
139 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );\r
140 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue );\r
141 void vQueueDelete( xQueueHandle xQueue );\r
142 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition );\r
143 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );\r
144 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
145 xQueueHandle xQueueCreateMutex( void );\r
146 \r
147 #if configUSE_CO_ROUTINES == 1\r
148         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );\r
149         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
150         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );\r
151         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );\r
152 #endif\r
153 \r
154 /*\r
155  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not\r
156  * prevent an ISR from adding or removing items to the queue, but does prevent\r
157  * an ISR from removing tasks from the queue event lists.  If an ISR finds a\r
158  * queue is locked it will instead increment the appropriate queue lock count\r
159  * to indicate that a task may require unblocking.  When the queue in unlocked\r
160  * these lock counts are inspected, and the appropriate action taken.\r
161  */\r
162 static void prvUnlockQueue( xQueueHandle pxQueue );\r
163 \r
164 /*\r
165  * Uses a critical section to determine if there is any data in a queue.\r
166  *\r
167  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
168  */\r
169 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );\r
170 \r
171 /*\r
172  * Uses a critical section to determine if there is any space in a queue.\r
173  *\r
174  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
175  */\r
176 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );\r
177 \r
178 /*\r
179  * Copies an item into the queue, either at the front of the queue or the\r
180  * back of the queue.\r
181  */\r
182 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );\r
183 \r
184 /*\r
185  * Copies an item out of a queue.\r
186  */\r
187 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 /*\r
191  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
192  * accessing the queue event lists.\r
193  */\r
194 #define prvLockQueue( pxQueue )                 \\r
195 {                                                                               \\r
196         taskENTER_CRITICAL();                           \\r
197                 ++( pxQueue->xRxLock );                 \\r
198                 ++( pxQueue->xTxLock );                 \\r
199         taskEXIT_CRITICAL();                            \\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 \r
204 /*-----------------------------------------------------------\r
205  * PUBLIC QUEUE MANAGEMENT API documented in queue.h\r
206  *----------------------------------------------------------*/\r
207 \r
208 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )\r
209 {\r
210 xQUEUE *pxNewQueue;\r
211 size_t xQueueSizeInBytes;\r
212 \r
213         /* Allocate the new queue structure. */\r
214         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
215         {\r
216                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
217                 if( pxNewQueue != NULL )\r
218                 {\r
219                         /* Create the list of pointers to queue items.  The queue is one byte\r
220                         longer than asked for to make wrap checking easier/faster. */\r
221                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;\r
222 \r
223                         pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );\r
224                         if( pxNewQueue->pcHead != NULL )\r
225                         {\r
226                                 /* Initialise the queue members as described above where the\r
227                                 queue type is defined. */\r
228                                 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );\r
229                                 pxNewQueue->uxMessagesWaiting = 0;\r
230                                 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;\r
231                                 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );\r
232                                 pxNewQueue->uxLength = uxQueueLength;\r
233                                 pxNewQueue->uxItemSize = uxItemSize;\r
234                                 pxNewQueue->xRxLock = queueUNLOCKED;\r
235                                 pxNewQueue->xTxLock = queueUNLOCKED;\r
236 \r
237                                 /* Likewise ensure the event queues start with the correct state. */\r
238                                 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
239                                 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
240 \r
241                                 return  pxNewQueue;\r
242                         }\r
243                         else\r
244                         {\r
245                                 vPortFree( pxNewQueue );\r
246                         }\r
247                 }\r
248         }\r
249 \r
250         /* Will only reach here if we could not allocate enough memory or no memory\r
251         was required. */\r
252         return NULL;\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 #if ( configUSE_MUTEXES == 1 )\r
257 \r
258         xQueueHandle xQueueCreateMutex( void )\r
259         {\r
260         xQUEUE *pxNewQueue;\r
261         \r
262                 /* Allocate the new queue structure. */\r
263                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
264                 if( pxNewQueue != NULL )\r
265                 {\r
266                         /* Information required for priority inheritance. */\r
267                         pxNewQueue->pxMutexHolder = NULL;\r
268                         pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
269         \r
270                         /* Queues used as a mutex no data is actually copied into or out\r
271                         of the queue. */\r
272                         pxNewQueue->pcWriteTo = NULL;\r
273                         pxNewQueue->pcReadFrom = NULL;\r
274                         \r
275                         /* Each mutex has a length of 1 (like a binary semaphore) and\r
276                         an item size of 0 as nothing is actually copied into or out\r
277                         of the mutex. */\r
278                         pxNewQueue->uxMessagesWaiting = 0;\r
279                         pxNewQueue->uxLength = 1;\r
280                         pxNewQueue->uxItemSize = 0;\r
281                         pxNewQueue->xRxLock = queueUNLOCKED;\r
282                         pxNewQueue->xTxLock = queueUNLOCKED;\r
283         \r
284                         /* Ensure the event queues start with the correct state. */\r
285                         vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
286                         vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
287 \r
288                         /* Start with the semaphore in the expected state. */\r
289                         xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );\r
290                 }\r
291         \r
292                 return pxNewQueue;\r
293         }\r
294 \r
295 #endif /* configUSE_MUTEXES */\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
299 {\r
300 signed portBASE_TYPE xReturn = pdPASS;\r
301 xTimeOutType xTimeOut;\r
302 \r
303         /* Make sure other tasks do not access the queue. */\r
304         vTaskSuspendAll();\r
305 \r
306         /* Capture the current time status for future reference. */\r
307         vTaskSetTimeOutState( &xTimeOut );\r
308 \r
309         /* It is important that this is the only thread/ISR that modifies the\r
310         ready or delayed lists until xTaskResumeAll() is called.  Places where\r
311         the ready/delayed lists are modified include:\r
312 \r
313                 + vTaskDelay() -  Nothing can call vTaskDelay as the scheduler is\r
314                   suspended, vTaskDelay() cannot be called from an ISR.\r
315                 + vTaskPrioritySet() - Has a critical section around the access.\r
316                 + vTaskSwitchContext() - This will not get executed while the scheduler\r
317                   is suspended.\r
318                 + prvCheckDelayedTasks() - This will not get executed while the\r
319                   scheduler is suspended.\r
320                 + xTaskCreate() - Has a critical section around the access.\r
321                 + vTaskResume() - Has a critical section around the access.\r
322                 + xTaskResumeAll() - Has a critical section around the access.\r
323                 + xTaskRemoveFromEventList - Checks to see if the scheduler is\r
324                   suspended.  If so then the TCB being removed from the event is\r
325                   removed from the event and added to the xPendingReadyList.\r
326         */\r
327 \r
328         /* Make sure interrupts do not access the queue event list. */\r
329         prvLockQueue( pxQueue );\r
330 \r
331         /* It is important that interrupts to not access the event list of the\r
332         queue being modified here.  Places where the event list is modified\r
333         include:\r
334 \r
335                 + xQueueGenericSendFromISR().  This checks the lock on the queue to see\r
336                   if it has access.  If the queue is locked then the Tx lock count is\r
337                   incremented to signify that a task waiting for data can be made ready\r
338                   once the queue lock is removed.  If the queue is not locked then\r
339                   a task can be moved from the event list, but will not be removed\r
340                   from the delayed list or placed in the ready list until the scheduler\r
341                   is unlocked.\r
342 \r
343                 + xQueueReceiveFromISR().  As per xQueueGenericSendFromISR().\r
344         */\r
345                 \r
346         /* If the queue is already full we may have to block. */\r
347         do\r
348         {\r
349                 if( prvIsQueueFull( pxQueue ) )\r
350                 {\r
351                         /* The queue is full - do we want to block or just leave without\r
352                         posting? */\r
353                         if( xTicksToWait > ( portTickType ) 0 )\r
354                         {\r
355                                 /* We are going to place ourselves on the xTasksWaitingToSend event\r
356                                 list, and will get woken should the delay expire, or space become\r
357                                 available on the queue.\r
358                                 \r
359                                 As detailed above we do not require mutual exclusion on the event\r
360                                 list as nothing else can modify it or the ready lists while we\r
361                                 have the scheduler suspended and queue locked.\r
362                                 \r
363                                 It is possible that an ISR has removed data from the queue since we\r
364                                 checked if any was available.  If this is the case then the data\r
365                                 will have been copied from the queue, and the queue variables\r
366                                 updated, but the event list will not yet have been checked to see if\r
367                                 anything is waiting as the queue is locked. */\r
368                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
369         \r
370                                 /* Force a context switch now as we are blocked.  We can do\r
371                                 this from within a critical section as the task we are\r
372                                 switching to has its own context.  When we return here (i.e. we\r
373                                 unblock) we will leave the critical section as normal.\r
374                                 \r
375                                 It is possible that an ISR has caused an event on an unrelated and\r
376                                 unlocked queue.  If this was the case then the event list for that\r
377                                 queue will have been updated but the ready lists left unchanged -\r
378                                 instead the readied task will have been added to the pending ready\r
379                                 list. */\r
380                                 taskENTER_CRITICAL();\r
381                                 {\r
382                                         /* We can safely unlock the queue and scheduler here as\r
383                                         interrupts are disabled.  We must not yield with anything\r
384                                         locked, but we can yield from within a critical section.\r
385                                         \r
386                                         Tasks that have been placed on the pending ready list cannot\r
387                                         be tasks that are waiting for events on this queue.  See\r
388                                         in comment xTaskRemoveFromEventList(). */\r
389                                         prvUnlockQueue( pxQueue );\r
390         \r
391                                         /* Resuming the scheduler may cause a yield.  If so then there\r
392                                         is no point yielding again here. */\r
393                                         if( !xTaskResumeAll() )\r
394                                         {\r
395                                                 taskYIELD();\r
396                                         }\r
397 \r
398                                         /* We want to check to see if the queue is still full\r
399                                         before leaving the critical section.  This is to prevent\r
400                                         this task placing an item into the queue due to an\r
401                                         interrupt making space on the queue between critical\r
402                                         sections (when there might be a higher priority task\r
403                                         blocked on the queue that cannot run yet because the\r
404                                         scheduler gets suspended). */\r
405                                         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
406                                         {\r
407                                                 /* We unblocked but there is no space in the queue,\r
408                                                 we probably timed out. */\r
409                                                 xReturn = errQUEUE_FULL;\r
410                                         }\r
411         \r
412                                         /* Before leaving the critical section we have to ensure\r
413                                         exclusive access again. */\r
414                                         vTaskSuspendAll();\r
415                                         prvLockQueue( pxQueue );                                \r
416                                 }\r
417                                 taskEXIT_CRITICAL();\r
418                         }\r
419                 }\r
420                         \r
421                 /* If xReturn is errQUEUE_FULL then we unblocked when the queue\r
422                 was still full.  Don't check it again now as it is possible that\r
423                 an interrupt has removed an item from the queue since we left the\r
424                 critical section and we don't want to write to the queue in case\r
425                 there is a task of higher priority blocked waiting for space to\r
426                 be available on the queue.  If this is the case the higher priority\r
427                 task will execute when the scheduler is unsupended. */\r
428                 if( xReturn != errQUEUE_FULL )\r
429                 {\r
430                         /* When we are here it is possible that we unblocked as space became\r
431                         available on the queue.  It is also possible that an ISR posted to the\r
432                         queue since we left the critical section, so it may be that again there\r
433                         is no space.  This would only happen if a task and ISR post onto the\r
434                         same queue. */\r
435                         taskENTER_CRITICAL();\r
436                         {\r
437                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
438                                 {\r
439                                         /* There is room in the queue, copy the data into the queue. */                 \r
440                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
441                                         xReturn = pdPASS;\r
442                 \r
443                                         /* Update the TxLock count so prvUnlockQueue knows to check for\r
444                                         tasks waiting for data to become available in the queue. */\r
445                                         ++( pxQueue->xTxLock );\r
446                                 }\r
447                                 else\r
448                                 {\r
449                                         xReturn = errQUEUE_FULL;\r
450                                 }\r
451                         }\r
452                         taskEXIT_CRITICAL();\r
453                 }\r
454 \r
455                 if( xReturn == errQUEUE_FULL )\r
456                 {\r
457                         if( xTicksToWait > 0 )\r
458                         {\r
459                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
460                                 {\r
461                                         xReturn = queueERRONEOUS_UNBLOCK;\r
462                                 }\r
463                         }\r
464                 }\r
465         }\r
466         while( xReturn == queueERRONEOUS_UNBLOCK );\r
467 \r
468         prvUnlockQueue( pxQueue );\r
469         xTaskResumeAll();\r
470 \r
471         return xReturn;\r
472 }\r
473 /*-----------------------------------------------------------*/\r
474 \r
475 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )\r
476 {\r
477         /* Similar to xQueueGenericSend, except we don't block if there is no room\r
478         in the queue.  Also we don't directly wake a task that was blocked on a\r
479         queue read, instead we return a flag to say whether a context switch is\r
480         required or not (i.e. has a task with a higher priority than us been woken\r
481         by this post). */\r
482         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
483         {\r
484                 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
485 \r
486                 /* If the queue is locked we do not alter the event list.  This will\r
487                 be done when the queue is unlocked later. */\r
488                 if( pxQueue->xTxLock == queueUNLOCKED )\r
489                 {\r
490                         /* We only want to wake one task per ISR, so check that a task has\r
491                         not already been woken. */\r
492                         if( !xTaskPreviouslyWoken )             \r
493                         {\r
494                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
495                                 {\r
496                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
497                                         {\r
498                                                 /* The task waiting has a higher priority so record that a\r
499                                                 context switch is required. */\r
500                                                 return pdTRUE;\r
501                                         }\r
502                                 }\r
503                         }\r
504                 }\r
505                 else\r
506                 {\r
507                         /* Increment the lock count so the task that unlocks the queue\r
508                         knows that data was posted while it was locked. */\r
509                         ++( pxQueue->xTxLock );\r
510                 }\r
511         }\r
512 \r
513         return xTaskPreviouslyWoken;\r
514 }\r
515 /*-----------------------------------------------------------*/\r
516 \r
517 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
518 {\r
519 signed portBASE_TYPE xReturn = pdTRUE;\r
520 xTimeOutType xTimeOut;\r
521 signed portCHAR *pcOriginalReadPosition;\r
522 \r
523         /* This function is very similar to xQueueGenericSend().  See comments\r
524         within xQueueGenericSend() for a more detailed explanation.\r
525 \r
526         Make sure other tasks do not access the queue. */\r
527         vTaskSuspendAll();\r
528 \r
529         /* Capture the current time status for future reference. */\r
530         vTaskSetTimeOutState( &xTimeOut );\r
531 \r
532         /* Make sure interrupts do not access the queue. */\r
533         prvLockQueue( pxQueue );\r
534 \r
535         do\r
536         {\r
537                 /* If there are no messages in the queue we may have to block. */\r
538                 if( prvIsQueueEmpty( pxQueue ) )\r
539                 {\r
540                         /* There are no messages in the queue, do we want to block or just\r
541                         leave with nothing? */                  \r
542                         if( xTicksToWait > ( portTickType ) 0 )\r
543                         {\r
544                                 #if ( configUSE_MUTEXES == 1 )\r
545                                 {\r
546                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
547                                         {\r
548                                                 portENTER_CRITICAL();\r
549                                                         vTaskPriorityInherit( ( xTaskHandle * const ) pxQueue->pxMutexHolder );\r
550                                                 portEXIT_CRITICAL();\r
551                                         }\r
552                                 }\r
553                                 #endif\r
554                                 \r
555                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
556                                 taskENTER_CRITICAL();\r
557                                 {\r
558                                         prvUnlockQueue( pxQueue );\r
559                                         if( !xTaskResumeAll() )\r
560                                         {\r
561                                                 taskYIELD();\r
562                                         }\r
563 \r
564                                         if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
565                                         {\r
566                                                 /* We unblocked but the queue is empty.  We probably\r
567                                                 timed out. */\r
568                                                 xReturn = errQUEUE_EMPTY;\r
569                                         }\r
570         \r
571                                         vTaskSuspendAll();\r
572                                         prvLockQueue( pxQueue );\r
573                                 }\r
574                                 taskEXIT_CRITICAL();\r
575                         }\r
576                 }\r
577         \r
578                 if( xReturn != errQUEUE_EMPTY )\r
579                 {\r
580                         taskENTER_CRITICAL();\r
581                         {\r
582                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
583                                 {\r
584                                         /* Remember our read position in case we are just peeking. */\r
585                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
586 \r
587                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
588 \r
589                                         if( xJustPeeking == pdFALSE )\r
590                                         {\r
591                                                 /* We are actually removing data. */\r
592                                                 --( pxQueue->uxMessagesWaiting );\r
593                                                         \r
594                                                 /* Increment the lock count so prvUnlockQueue knows to check for\r
595                                                 tasks waiting for space to become available on the queue. */\r
596                                                 ++( pxQueue->xRxLock );\r
597                                                 \r
598                                                 #if ( configUSE_MUTEXES == 1 )\r
599                                                 {\r
600                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
601                                                         {\r
602                                                                 /* Record the information required to implement\r
603                                                                 priority inheritance should it become necessary. */\r
604                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
605                                                         }\r
606                                                 }\r
607                                                 #endif\r
608                                         }\r
609                                         else\r
610                                         {\r
611                                                 /* We are not removing the data, so reset our read\r
612                                                 pointer. */\r
613                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;   \r
614                                         }\r
615                                         \r
616                                         xReturn = pdPASS;                                       \r
617                                 }\r
618                                 else\r
619                                 {\r
620                                         xReturn = errQUEUE_EMPTY;\r
621                                 }\r
622                         }\r
623                         taskEXIT_CRITICAL();\r
624                 }\r
625 \r
626                 if( xReturn == errQUEUE_EMPTY )\r
627                 {\r
628                         if( xTicksToWait > 0 )\r
629                         {\r
630                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
631                                 {\r
632                                         xReturn = queueERRONEOUS_UNBLOCK;\r
633                                 }\r
634                         }\r
635                 }\r
636         } while( xReturn == queueERRONEOUS_UNBLOCK );\r
637 \r
638         /* We no longer require exclusive access to the queue. */\r
639         prvUnlockQueue( pxQueue );\r
640         xTaskResumeAll();\r
641 \r
642         return xReturn;\r
643 }\r
644 /*-----------------------------------------------------------*/\r
645 \r
646 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )\r
647 {\r
648 signed portBASE_TYPE xReturn;\r
649 \r
650         /* We cannot block from an ISR, so check there is data available. */\r
651         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
652         {\r
653                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
654                 --( pxQueue->uxMessagesWaiting );\r
655 \r
656                 /* If the queue is locked we will not modify the event list.  Instead\r
657                 we update the lock count so the task that unlocks the queue will know\r
658                 that an ISR has removed data while the queue was locked. */\r
659                 if( pxQueue->xRxLock == queueUNLOCKED )\r
660                 {\r
661                         /* We only want to wake one task per ISR, so check that a task has\r
662                         not already been woken. */\r
663                         if( !( *pxTaskWoken ) )\r
664                         {\r
665                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
666                                 {\r
667                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
668                                         {\r
669                                                 /* The task waiting has a higher priority than us so\r
670                                                 force a context switch. */\r
671                                                 *pxTaskWoken = pdTRUE;\r
672                                         }\r
673                                 }\r
674                         }\r
675                 }\r
676                 else\r
677                 {\r
678                         /* Increment the lock count so the task that unlocks the queue\r
679                         knows that data was removed while it was locked. */\r
680                         ++( pxQueue->xRxLock );\r
681                 }\r
682 \r
683                 xReturn = pdPASS;\r
684         }\r
685         else\r
686         {\r
687                 xReturn = pdFAIL;\r
688         }\r
689 \r
690         return xReturn;\r
691 }\r
692 /*-----------------------------------------------------------*/\r
693 \r
694 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue )\r
695 {\r
696 unsigned portBASE_TYPE uxReturn;\r
697 \r
698         taskENTER_CRITICAL();\r
699                 uxReturn = pxQueue->uxMessagesWaiting;\r
700         taskEXIT_CRITICAL();\r
701 \r
702         return uxReturn;\r
703 }\r
704 /*-----------------------------------------------------------*/\r
705 \r
706 void vQueueDelete( xQueueHandle pxQueue )\r
707 {\r
708         vPortFree( pxQueue->pcHead );\r
709         vPortFree( pxQueue );\r
710 }\r
711 /*-----------------------------------------------------------*/\r
712 \r
713 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )\r
714 {\r
715         if( pxQueue->uxItemSize == 0 )\r
716         {\r
717                 #if ( configUSE_MUTEXES == 1 )\r
718                 {\r
719                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
720                         {\r
721                                 /* The mutex is no longer being held. */\r
722                                 vTaskPriorityDisinherit( ( xTaskHandle * const ) pxQueue->pxMutexHolder );\r
723                         }\r
724                 }\r
725                 #endif\r
726         }\r
727         else if( xPosition == queueSEND_TO_BACK )\r
728         {\r
729                 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
730                 pxQueue->pcWriteTo += pxQueue->uxItemSize;\r
731                 if( pxQueue->pcWriteTo >= pxQueue->pcTail )\r
732                 {\r
733                         pxQueue->pcWriteTo = pxQueue->pcHead;\r
734                 }\r
735         }\r
736         else\r
737         {\r
738                 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
739                 pxQueue->pcReadFrom -= pxQueue->uxItemSize;\r
740                 if( pxQueue->pcReadFrom < pxQueue->pcHead )\r
741                 {\r
742                         pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
743                 }               \r
744         }\r
745 \r
746         ++( pxQueue->uxMessagesWaiting );\r
747 }\r
748 /*-----------------------------------------------------------*/\r
749 \r
750 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )\r
751 {\r
752         if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
753         {\r
754                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
755                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
756                 {\r
757                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
758                 }\r
759                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
760         }       \r
761 }\r
762 /*-----------------------------------------------------------*/\r
763 \r
764 static void prvUnlockQueue( xQueueHandle pxQueue )\r
765 {\r
766         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
767 \r
768         /* The lock counts contains the number of extra data items placed or\r
769         removed from the queue while the queue was locked.  When a queue is\r
770         locked items can be added or removed, but the event lists cannot be\r
771         updated. */\r
772         taskENTER_CRITICAL();\r
773         {\r
774                 --( pxQueue->xTxLock );\r
775 \r
776                 /* See if data was added to the queue while it was locked. */\r
777                 if( pxQueue->xTxLock > queueUNLOCKED )\r
778                 {\r
779                         pxQueue->xTxLock = queueUNLOCKED;\r
780 \r
781                         /* Data was posted while the queue was locked.  Are any tasks\r
782                         blocked waiting for data to become available? */\r
783                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
784                         {\r
785                                 /* Tasks that are removed from the event list will get added to\r
786                                 the pending ready list as the scheduler is still suspended. */\r
787                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
788                                 {\r
789                                         /* The task waiting has a higher priority so record that a\r
790                                         context switch is required. */\r
791                                         vTaskMissedYield();\r
792                                 }\r
793                         }                       \r
794                 }\r
795         }\r
796         taskEXIT_CRITICAL();\r
797 \r
798         /* Do the same for the Rx lock. */\r
799         taskENTER_CRITICAL();\r
800         {\r
801                 --( pxQueue->xRxLock );\r
802 \r
803                 if( pxQueue->xRxLock > queueUNLOCKED )\r
804                 {\r
805                         pxQueue->xRxLock = queueUNLOCKED;\r
806 \r
807                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
808                         {\r
809                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
810                                 {\r
811                                         vTaskMissedYield();\r
812                                 }\r
813                         }                       \r
814                 }\r
815         }\r
816         taskEXIT_CRITICAL();\r
817 }\r
818 /*-----------------------------------------------------------*/\r
819 \r
820 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )\r
821 {\r
822 signed portBASE_TYPE xReturn;\r
823 \r
824         taskENTER_CRITICAL();\r
825                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
826         taskEXIT_CRITICAL();\r
827 \r
828         return xReturn;\r
829 }\r
830 /*-----------------------------------------------------------*/\r
831 \r
832 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )\r
833 {\r
834 signed portBASE_TYPE xReturn;\r
835 \r
836         taskENTER_CRITICAL();\r
837                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
838         taskEXIT_CRITICAL();\r
839 \r
840         return xReturn;\r
841 }\r
842 /*-----------------------------------------------------------*/\r
843 \r
844 #if configUSE_CO_ROUTINES == 1\r
845 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
846 {\r
847 signed portBASE_TYPE xReturn;\r
848                 \r
849         /* If the queue is already full we may have to block.  A critical section\r
850         is required to prevent an interrupt removing something from the queue\r
851         between the check to see if the queue is full and blocking on the queue. */\r
852         portDISABLE_INTERRUPTS();\r
853         {\r
854                 if( prvIsQueueFull( pxQueue ) )\r
855                 {\r
856                         /* The queue is full - do we want to block or just leave without\r
857                         posting? */\r
858                         if( xTicksToWait > ( portTickType ) 0 )\r
859                         {\r
860                                 /* As this is called from a coroutine we cannot block directly, but\r
861                                 return indicating that we need to block. */\r
862                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );                          \r
863                                 portENABLE_INTERRUPTS();\r
864                                 return errQUEUE_BLOCKED;\r
865                         }\r
866                         else\r
867                         {\r
868                                 portENABLE_INTERRUPTS();\r
869                                 return errQUEUE_FULL;\r
870                         }\r
871                 }\r
872         }\r
873         portENABLE_INTERRUPTS();\r
874                 \r
875         portNOP();\r
876 \r
877         portDISABLE_INTERRUPTS();\r
878         {\r
879                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
880                 {\r
881                         /* There is room in the queue, copy the data into the queue. */                 \r
882                         prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
883                         xReturn = pdPASS;\r
884 \r
885                         /* Were any co-routines waiting for data to become available? */\r
886                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
887                         {\r
888                                 /* In this instance the co-routine could be placed directly\r
889                                 into the ready list as we are within a critical section.\r
890                                 Instead the same pending ready list mechanism is used as if\r
891                                 the event were caused from within an interrupt. */\r
892                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
893                                 {\r
894                                         /* The co-routine waiting has a higher priority so record\r
895                                         that a yield might be appropriate. */\r
896                                         xReturn = errQUEUE_YIELD;\r
897                                 }\r
898                         }\r
899                 }\r
900                 else\r
901                 {\r
902                         xReturn = errQUEUE_FULL;\r
903                 }\r
904         }\r
905         portENABLE_INTERRUPTS();\r
906 \r
907         return xReturn;\r
908 }\r
909 #endif\r
910 /*-----------------------------------------------------------*/\r
911 \r
912 #if configUSE_CO_ROUTINES == 1\r
913 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
914 {\r
915 signed portBASE_TYPE xReturn;\r
916 \r
917         /* If the queue is already empty we may have to block.  A critical section\r
918         is required to prevent an interrupt adding something to the queue\r
919         between the check to see if the queue is empty and blocking on the queue. */\r
920         portDISABLE_INTERRUPTS();\r
921         {\r
922                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
923                 {\r
924                         /* There are no messages in the queue, do we want to block or just\r
925                         leave with nothing? */                  \r
926                         if( xTicksToWait > ( portTickType ) 0 )\r
927                         {\r
928                                 /* As this is a co-routine we cannot block directly, but return\r
929                                 indicating that we need to block. */\r
930                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );\r
931                                 portENABLE_INTERRUPTS();\r
932                                 return errQUEUE_BLOCKED;\r
933                         }\r
934                         else\r
935                         {\r
936                                 portENABLE_INTERRUPTS();\r
937                                 return errQUEUE_FULL;\r
938                         }\r
939                 }\r
940         }\r
941         portENABLE_INTERRUPTS();\r
942 \r
943         portNOP();\r
944 \r
945         portDISABLE_INTERRUPTS();\r
946         {\r
947                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
948                 {\r
949                         /* Data is available from the queue. */\r
950                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
951                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
952                         {\r
953                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
954                         }\r
955                         --( pxQueue->uxMessagesWaiting );\r
956                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
957 \r
958                         xReturn = pdPASS;\r
959 \r
960                         /* Were any co-routines waiting for space to become available? */\r
961                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
962                         {\r
963                                 /* In this instance the co-routine could be placed directly\r
964                                 into the ready list as we are within a critical section.\r
965                                 Instead the same pending ready list mechanism is used as if\r
966                                 the event were caused from within an interrupt. */\r
967                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
968                                 {\r
969                                         xReturn = errQUEUE_YIELD;\r
970                                 }\r
971                         }       \r
972                 }\r
973                 else\r
974                 {\r
975                         xReturn = pdFAIL;\r
976                 }\r
977         }\r
978         portENABLE_INTERRUPTS();\r
979 \r
980         return xReturn;\r
981 }\r
982 #endif\r
983 /*-----------------------------------------------------------*/\r
984 \r
985 \r
986 \r
987 #if configUSE_CO_ROUTINES == 1\r
988 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
989 {\r
990         /* Cannot block within an ISR so if there is no space on the queue then\r
991         exit without doing anything. */\r
992         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
993         {\r
994                 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
995 \r
996                 /* We only want to wake one co-routine per ISR, so check that a\r
997                 co-routine has not already been woken. */\r
998                 if( !xCoRoutinePreviouslyWoken )                \r
999                 {\r
1000                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
1001                         {\r
1002                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1003                                 {\r
1004                                         return pdTRUE;\r
1005                                 }\r
1006                         }\r
1007                 }\r
1008         }\r
1009 \r
1010         return xCoRoutinePreviouslyWoken;\r
1011 }\r
1012 #endif\r
1013 /*-----------------------------------------------------------*/\r
1014 \r
1015 #if configUSE_CO_ROUTINES == 1\r
1016 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
1017 {\r
1018 signed portBASE_TYPE xReturn;\r
1019 \r
1020         /* We cannot block from an ISR, so check there is data available. If\r
1021         not then just leave without doing anything. */\r
1022         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1023         {\r
1024                 /* Copy the data from the queue. */\r
1025                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1026                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1027                 {\r
1028                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1029                 }\r
1030                 --( pxQueue->uxMessagesWaiting );\r
1031                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1032 \r
1033                 if( !( *pxCoRoutineWoken ) )\r
1034                 {\r
1035                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
1036                         {\r
1037                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1038                                 {\r
1039                                         *pxCoRoutineWoken = pdTRUE;\r
1040                                 }\r
1041                         }\r
1042                 }\r
1043 \r
1044                 xReturn = pdPASS;\r
1045         }\r
1046         else\r
1047         {\r
1048                 xReturn = pdFAIL;\r
1049         }\r
1050 \r
1051         return xReturn;\r
1052 }\r
1053 #endif\r
1054 /*-----------------------------------------------------------*/\r
1055 \r