]> git.sur5r.net Git - freertos/blob - Source/queue.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / queue.c
1 /*\r
2         FreeRTOS.org V4.3.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 /*\r
94  * Definition of the queue used by the scheduler.\r
95  * Items are queued by copy, not reference.\r
96  */\r
97 typedef struct QueueDefinition\r
98 {\r
99         signed portCHAR *pcHead;                                /*< Points to the beginning of the queue storage area. */\r
100         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
101 \r
102         signed portCHAR *pcWriteTo;                             /*< Points to the free next place in the storage area. */\r
103         signed portCHAR *pcReadFrom;                    /*< Points to the last place that a queued item was read from. */\r
104 \r
105         xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
106         xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
107 \r
108         unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */\r
109         unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
110         unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */\r
111 \r
112         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
113         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
114 } xQUEUE;\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * Inside this file xQueueHandle is a pointer to a xQUEUE structure.\r
119  * To keep the definition private the API header file defines it as a\r
120  * pointer to void.\r
121  */\r
122 typedef xQUEUE * xQueueHandle;\r
123 \r
124 /*\r
125  * Prototypes for public functions are included here so we don't have to\r
126  * include the API header file (as it defines xQueueHandle differently).  These\r
127  * functions are documented in the API header file.\r
128  */\r
129 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );\r
130 signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );\r
131 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue );\r
132 void vQueueDelete( xQueueHandle xQueue );\r
133 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );\r
134 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );\r
135 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
136 \r
137 #if configUSE_CO_ROUTINES == 1\r
138         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );\r
139         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
140         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );\r
141         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );\r
142 #endif\r
143 \r
144 /*\r
145  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not\r
146  * prevent an ISR from adding or removing items to the queue, but does prevent\r
147  * an ISR from removing tasks from the queue event lists.  If an ISR finds a\r
148  * queue is locked it will instead increment the appropriate queue lock count\r
149  * to indicate that a task may require unblocking.  When the queue in unlocked\r
150  * these lock counts are inspected, and the appropriate action taken.\r
151  */\r
152 static void prvUnlockQueue( xQueueHandle pxQueue );\r
153 \r
154 /*\r
155  * Uses a critical section to determine if there is any data in a queue.\r
156  *\r
157  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
158  */\r
159 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );\r
160 \r
161 /*\r
162  * Uses a critical section to determine if there is any space in a queue.\r
163  *\r
164  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
165  */\r
166 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );\r
167 \r
168 /*\r
169  * Macro that copies an item into the queue.  This is done by copying the item\r
170  * byte for byte, not by reference.  Updates the queue state to ensure it's\r
171  * integrity after the copy.\r
172  */\r
173 #define prvCopyQueueData( pxQueue, pvItemToQueue )                                                                                              \\r
174 {                                                                                                                                                                                               \\r
175         memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );       \\r
176         ++( pxQueue->uxMessagesWaiting );                                                                                                                       \\r
177         pxQueue->pcWriteTo += pxQueue->uxItemSize;                                                                                                      \\r
178         if( pxQueue->pcWriteTo >= pxQueue->pcTail )                                                                                                     \\r
179         {                                                                                                                                                                                       \\r
180                 pxQueue->pcWriteTo = pxQueue->pcHead;                                                                                                   \\r
181         }                                                                                                                                                                                       \\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /*\r
186  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
187  * accessing the queue event lists.\r
188  */\r
189 #define prvLockQueue( pxQueue )                 \\r
190 {                                                                               \\r
191         taskENTER_CRITICAL();                           \\r
192                 ++( pxQueue->xRxLock );                 \\r
193                 ++( pxQueue->xTxLock );                 \\r
194         taskEXIT_CRITICAL();                            \\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 \r
199 /*-----------------------------------------------------------\r
200  * PUBLIC QUEUE MANAGEMENT API documented in queue.h\r
201  *----------------------------------------------------------*/\r
202 \r
203 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )\r
204 {\r
205 xQUEUE *pxNewQueue;\r
206 size_t xQueueSizeInBytes;\r
207 \r
208         /* Allocate the new queue structure. */\r
209         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
210         {\r
211                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
212                 if( pxNewQueue != NULL )\r
213                 {\r
214                         /* Create the list of pointers to queue items.  The queue is one byte\r
215                         longer than asked for to make wrap checking easier/faster. */\r
216                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;\r
217 \r
218                         pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );\r
219                         if( pxNewQueue->pcHead != NULL )\r
220                         {\r
221                                 /* Initialise the queue members as described above where the\r
222                                 queue type is defined. */\r
223                                 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );\r
224                                 pxNewQueue->uxMessagesWaiting = 0;\r
225                                 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;\r
226                                 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );\r
227                                 pxNewQueue->uxLength = uxQueueLength;\r
228                                 pxNewQueue->uxItemSize = uxItemSize;\r
229                                 pxNewQueue->xRxLock = queueUNLOCKED;\r
230                                 pxNewQueue->xTxLock = queueUNLOCKED;\r
231 \r
232                                 /* Likewise ensure the event queues start with the correct state. */\r
233                                 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
234                                 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
235 \r
236                                 return  pxNewQueue;\r
237                         }\r
238                         else\r
239                         {\r
240                                 vPortFree( pxNewQueue );\r
241                         }\r
242                 }\r
243         }\r
244 \r
245         /* Will only reach here if we could not allocate enough memory or no memory\r
246         was required. */\r
247         return NULL;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 signed portBASE_TYPE xQueueSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
252 {\r
253 signed portBASE_TYPE xReturn = pdPASS;\r
254 xTimeOutType xTimeOut;\r
255 \r
256         /* Make sure other tasks do not access the queue. */\r
257         vTaskSuspendAll();\r
258 \r
259         /* Capture the current time status for future reference. */\r
260         vTaskSetTimeOutState( &xTimeOut );\r
261 \r
262         /* It is important that this is the only thread/ISR that modifies the\r
263         ready or delayed lists until xTaskResumeAll() is called.  Places where\r
264         the ready/delayed lists are modified include:\r
265 \r
266                 + vTaskDelay() -  Nothing can call vTaskDelay as the scheduler is\r
267                   suspended, vTaskDelay() cannot be called from an ISR.\r
268                 + vTaskPrioritySet() - Has a critical section around the access.\r
269                 + vTaskSwitchContext() - This will not get executed while the scheduler\r
270                   is suspended.\r
271                 + prvCheckDelayedTasks() - This will not get executed while the\r
272                   scheduler is suspended.\r
273                 + xTaskCreate() - Has a critical section around the access.\r
274                 + vTaskResume() - Has a critical section around the access.\r
275                 + xTaskResumeAll() - Has a critical section around the access.\r
276                 + xTaskRemoveFromEventList - Checks to see if the scheduler is\r
277                   suspended.  If so then the TCB being removed from the event is\r
278                   removed from the event and added to the xPendingReadyList.\r
279         */\r
280 \r
281         /* Make sure interrupts do not access the queue event list. */\r
282         prvLockQueue( pxQueue );\r
283 \r
284         /* It is important that interrupts to not access the event list of the\r
285         queue being modified here.  Places where the event list is modified\r
286         include:\r
287 \r
288                 + xQueueSendFromISR().  This checks the lock on the queue to see if\r
289                   it has access.  If the queue is locked then the Tx lock count is\r
290                   incremented to signify that a task waiting for data can be made ready\r
291                   once the queue lock is removed.  If the queue is not locked then\r
292                   a task can be moved from the event list, but will not be removed\r
293                   from the delayed list or placed in the ready list until the scheduler\r
294                   is unlocked.\r
295 \r
296                 + xQueueReceiveFromISR().  As per xQueueSendFromISR().\r
297         */\r
298                 \r
299         /* If the queue is already full we may have to block. */\r
300         do\r
301         {\r
302                 if( prvIsQueueFull( pxQueue ) )\r
303                 {\r
304                         /* The queue is full - do we want to block or just leave without\r
305                         posting? */\r
306                         if( xTicksToWait > ( portTickType ) 0 )\r
307                         {\r
308                                 /* We are going to place ourselves on the xTasksWaitingToSend event\r
309                                 list, and will get woken should the delay expire, or space become\r
310                                 available on the queue.\r
311                                 \r
312                                 As detailed above we do not require mutual exclusion on the event\r
313                                 list as nothing else can modify it or the ready lists while we\r
314                                 have the scheduler suspended and queue locked.\r
315                                 \r
316                                 It is possible that an ISR has removed data from the queue since we\r
317                                 checked if any was available.  If this is the case then the data\r
318                                 will have been copied from the queue, and the queue variables\r
319                                 updated, but the event list will not yet have been checked to see if\r
320                                 anything is waiting as the queue is locked. */\r
321                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
322         \r
323                                 /* Force a context switch now as we are blocked.  We can do\r
324                                 this from within a critical section as the task we are\r
325                                 switching to has its own context.  When we return here (i.e. we\r
326                                 unblock) we will leave the critical section as normal.\r
327                                 \r
328                                 It is possible that an ISR has caused an event on an unrelated and\r
329                                 unlocked queue.  If this was the case then the event list for that\r
330                                 queue will have been updated but the ready lists left unchanged -\r
331                                 instead the readied task will have been added to the pending ready\r
332                                 list. */\r
333                                 taskENTER_CRITICAL();\r
334                                 {\r
335                                         /* We can safely unlock the queue and scheduler here as\r
336                                         interrupts are disabled.  We must not yield with anything\r
337                                         locked, but we can yield from within a critical section.\r
338                                         \r
339                                         Tasks that have been placed on the pending ready list cannot\r
340                                         be tasks that are waiting for events on this queue.  See\r
341                                         in comment xTaskRemoveFromEventList(). */\r
342                                         prvUnlockQueue( pxQueue );\r
343         \r
344                                         /* Resuming the scheduler may cause a yield.  If so then there\r
345                                         is no point yielding again here. */\r
346                                         if( !xTaskResumeAll() )\r
347                                         {\r
348                                                 taskYIELD();\r
349                                         }\r
350 \r
351                                         /* We want to check to see if the queue is still full\r
352                                         before leaving the critical section.  This is to prevent\r
353                                         this task placing an item into the queue due to an\r
354                                         interrupt making space on the queue between critical\r
355                                         sections (when there might be a higher priority task\r
356                                         blocked on the queue that cannot run yet because the\r
357                                         scheduler gets suspended). */\r
358                                         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
359                                         {\r
360                                                 /* We unblocked but there is no space in the queue,\r
361                                                 we probably timed out. */\r
362                                                 xReturn = errQUEUE_FULL;\r
363                                         }\r
364         \r
365                                         /* Before leaving the critical section we have to ensure\r
366                                         exclusive access again. */\r
367                                         vTaskSuspendAll();\r
368                                         prvLockQueue( pxQueue );                                \r
369                                 }\r
370                                 taskEXIT_CRITICAL();\r
371                         }\r
372                 }\r
373                         \r
374                 /* If xReturn is errQUEUE_FULL then we unblocked when the queue\r
375                 was still full.  Don't check it again now as it is possible that\r
376                 an interrupt has removed an item from the queue since we left the\r
377                 critical section and we don't want to write to the queue in case\r
378                 there is a task of higher priority blocked waiting for space to\r
379                 be available on the queue.  If this is the case the higher priority\r
380                 task will execute when the scheduler is unsupended. */\r
381                 if( xReturn != errQUEUE_FULL )\r
382                 {\r
383                         /* When we are here it is possible that we unblocked as space became\r
384                         available on the queue.  It is also possible that an ISR posted to the\r
385                         queue since we left the critical section, so it may be that again there\r
386                         is no space.  This would only happen if a task and ISR post onto the\r
387                         same queue. */\r
388                         taskENTER_CRITICAL();\r
389                         {\r
390                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
391                                 {\r
392                                         /* There is room in the queue, copy the data into the queue. */                 \r
393                                         prvCopyQueueData( pxQueue, pvItemToQueue );             \r
394                                         xReturn = pdPASS;\r
395                 \r
396                                         /* Update the TxLock count so prvUnlockQueue knows to check for\r
397                                         tasks waiting for data to become available in the queue. */\r
398                                         ++( pxQueue->xTxLock );\r
399                                 }\r
400                                 else\r
401                                 {\r
402                                         xReturn = errQUEUE_FULL;\r
403                                 }\r
404                         }\r
405                         taskEXIT_CRITICAL();\r
406                 }\r
407 \r
408                 if( xReturn == errQUEUE_FULL )\r
409                 {\r
410                         if( xTicksToWait > 0 )\r
411                         {\r
412                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
413                                 {\r
414                                         xReturn = queueERRONEOUS_UNBLOCK;\r
415                                 }\r
416                         }\r
417                 }\r
418         }\r
419         while( xReturn == queueERRONEOUS_UNBLOCK );\r
420 \r
421         prvUnlockQueue( pxQueue );\r
422         xTaskResumeAll();\r
423 \r
424         return xReturn;\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken )\r
429 {\r
430         /* Similar to xQueueSend, except we don't block if there is no room in the\r
431         queue.  Also we don't directly wake a task that was blocked on a queue\r
432         read, instead we return a flag to say whether a context switch is required\r
433         or not (i.e. has a task with a higher priority than us been woken by this\r
434         post). */\r
435         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
436         {\r
437                 prvCopyQueueData( pxQueue, pvItemToQueue );\r
438 \r
439                 /* If the queue is locked we do not alter the event list.  This will\r
440                 be done when the queue is unlocked later. */\r
441                 if( pxQueue->xTxLock == queueUNLOCKED )\r
442                 {\r
443                         /* We only want to wake one task per ISR, so check that a task has\r
444                         not already been woken. */\r
445                         if( !xTaskPreviouslyWoken )             \r
446                         {\r
447                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
448                                 {\r
449                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
450                                         {\r
451                                                 /* The task waiting has a higher priority so record that a\r
452                                                 context switch is required. */\r
453                                                 return pdTRUE;\r
454                                         }\r
455                                 }\r
456                         }\r
457                 }\r
458                 else\r
459                 {\r
460                         /* Increment the lock count so the task that unlocks the queue\r
461                         knows that data was posted while it was locked. */\r
462                         ++( pxQueue->xTxLock );\r
463                 }\r
464         }\r
465 \r
466         return xTaskPreviouslyWoken;\r
467 }\r
468 /*-----------------------------------------------------------*/\r
469 \r
470 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
471 {\r
472 signed portBASE_TYPE xReturn = pdTRUE;\r
473 xTimeOutType xTimeOut;\r
474 \r
475         /* This function is very similar to xQueueSend().  See comments within\r
476         xQueueSend() for a more detailed explanation.\r
477 \r
478         Make sure other tasks do not access the queue. */\r
479         vTaskSuspendAll();\r
480 \r
481         /* Capture the current time status for future reference. */\r
482         vTaskSetTimeOutState( &xTimeOut );\r
483 \r
484         /* Make sure interrupts do not access the queue. */\r
485         prvLockQueue( pxQueue );\r
486 \r
487         do\r
488         {\r
489                 /* If there are no messages in the queue we may have to block. */\r
490                 if( prvIsQueueEmpty( pxQueue ) )\r
491                 {\r
492                         /* There are no messages in the queue, do we want to block or just\r
493                         leave with nothing? */                  \r
494                         if( xTicksToWait > ( portTickType ) 0 )\r
495                         {\r
496                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
497                                 taskENTER_CRITICAL();\r
498                                 {\r
499                                         prvUnlockQueue( pxQueue );\r
500                                         if( !xTaskResumeAll() )\r
501                                         {\r
502                                                 taskYIELD();\r
503                                         }\r
504 \r
505                                         if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
506                                         {\r
507                                                 /* We unblocked but the queue is empty.  We probably\r
508                                                 timed out. */\r
509                                                 xReturn = errQUEUE_EMPTY;\r
510                                         }\r
511         \r
512                                         vTaskSuspendAll();\r
513                                         prvLockQueue( pxQueue );\r
514                                 }\r
515                                 taskEXIT_CRITICAL();\r
516                         }\r
517                 }\r
518         \r
519                 if( xReturn != errQUEUE_EMPTY )\r
520                 {\r
521                         taskENTER_CRITICAL();\r
522                         {\r
523                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
524                                 {\r
525                                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
526                                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
527                                         {\r
528                                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
529                                         }\r
530                                         --( pxQueue->uxMessagesWaiting );\r
531                                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
532                 \r
533                                         /* Increment the lock count so prvUnlockQueue knows to check for\r
534                                         tasks waiting for space to become available on the queue. */\r
535                                         ++( pxQueue->xRxLock );\r
536                                         xReturn = pdPASS;\r
537                                 }\r
538                                 else\r
539                                 {\r
540                                         xReturn = errQUEUE_EMPTY;\r
541                                 }\r
542                         }\r
543                         taskEXIT_CRITICAL();\r
544                 }\r
545 \r
546                 if( xReturn == errQUEUE_EMPTY )\r
547                 {\r
548                         if( xTicksToWait > 0 )\r
549                         {\r
550                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
551                                 {\r
552                                         xReturn = queueERRONEOUS_UNBLOCK;\r
553                                 }\r
554                         }\r
555                 }\r
556         } while( xReturn == queueERRONEOUS_UNBLOCK );\r
557 \r
558         /* We no longer require exclusive access to the queue. */\r
559         prvUnlockQueue( pxQueue );\r
560         xTaskResumeAll();\r
561 \r
562         return xReturn;\r
563 }\r
564 /*-----------------------------------------------------------*/\r
565 \r
566 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken )\r
567 {\r
568 signed portBASE_TYPE xReturn;\r
569 \r
570         /* We cannot block from an ISR, so check there is data available. */\r
571         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
572         {\r
573                 /* Copy the data from the queue. */\r
574                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
575                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
576                 {\r
577                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
578                 }\r
579                 --( pxQueue->uxMessagesWaiting );\r
580                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
581 \r
582                 /* If the queue is locked we will not modify the event list.  Instead\r
583                 we update the lock count so the task that unlocks the queue will know\r
584                 that an ISR has removed data while the queue was locked. */\r
585                 if( pxQueue->xRxLock == queueUNLOCKED )\r
586                 {\r
587                         /* We only want to wake one task per ISR, so check that a task has\r
588                         not already been woken. */\r
589                         if( !( *pxTaskWoken ) )\r
590                         {\r
591                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
592                                 {\r
593                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
594                                         {\r
595                                                 /* The task waiting has a higher priority than us so\r
596                                                 force a context switch. */\r
597                                                 *pxTaskWoken = pdTRUE;\r
598                                         }\r
599                                 }\r
600                         }\r
601                 }\r
602                 else\r
603                 {\r
604                         /* Increment the lock count so the task that unlocks the queue\r
605                         knows that data was removed while it was locked. */\r
606                         ++( pxQueue->xRxLock );\r
607                 }\r
608 \r
609                 xReturn = pdPASS;\r
610         }\r
611         else\r
612         {\r
613                 xReturn = pdFAIL;\r
614         }\r
615 \r
616         return xReturn;\r
617 }\r
618 /*-----------------------------------------------------------*/\r
619 \r
620 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue )\r
621 {\r
622 unsigned portBASE_TYPE uxReturn;\r
623 \r
624         taskENTER_CRITICAL();\r
625                 uxReturn = pxQueue->uxMessagesWaiting;\r
626         taskEXIT_CRITICAL();\r
627 \r
628         return uxReturn;\r
629 }\r
630 /*-----------------------------------------------------------*/\r
631 \r
632 void vQueueDelete( xQueueHandle pxQueue )\r
633 {\r
634         vPortFree( pxQueue->pcHead );\r
635         vPortFree( pxQueue );\r
636 }\r
637 /*-----------------------------------------------------------*/\r
638 \r
639 static void prvUnlockQueue( xQueueHandle pxQueue )\r
640 {\r
641         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
642 \r
643         /* The lock counts contains the number of extra data items placed or\r
644         removed from the queue while the queue was locked.  When a queue is\r
645         locked items can be added or removed, but the event lists cannot be\r
646         updated. */\r
647         taskENTER_CRITICAL();\r
648         {\r
649                 --( pxQueue->xTxLock );\r
650 \r
651                 /* See if data was added to the queue while it was locked. */\r
652                 if( pxQueue->xTxLock > queueUNLOCKED )\r
653                 {\r
654                         pxQueue->xTxLock = queueUNLOCKED;\r
655 \r
656                         /* Data was posted while the queue was locked.  Are any tasks\r
657                         blocked waiting for data to become available? */\r
658                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
659                         {\r
660                                 /* Tasks that are removed from the event list will get added to\r
661                                 the pending ready list as the scheduler is still suspended. */\r
662                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
663                                 {\r
664                                         /* The task waiting has a higher priority so record that a\r
665                                         context switch is required. */\r
666                                         vTaskMissedYield();\r
667                                 }\r
668                         }                       \r
669                 }\r
670         }\r
671         taskEXIT_CRITICAL();\r
672 \r
673         /* Do the same for the Rx lock. */\r
674         taskENTER_CRITICAL();\r
675         {\r
676                 --( pxQueue->xRxLock );\r
677 \r
678                 if( pxQueue->xRxLock > queueUNLOCKED )\r
679                 {\r
680                         pxQueue->xRxLock = queueUNLOCKED;\r
681 \r
682                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
683                         {\r
684                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
685                                 {\r
686                                         vTaskMissedYield();\r
687                                 }\r
688                         }                       \r
689                 }\r
690         }\r
691         taskEXIT_CRITICAL();\r
692 }\r
693 /*-----------------------------------------------------------*/\r
694 \r
695 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )\r
696 {\r
697 signed portBASE_TYPE xReturn;\r
698 \r
699         taskENTER_CRITICAL();\r
700                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
701         taskEXIT_CRITICAL();\r
702 \r
703         return xReturn;\r
704 }\r
705 /*-----------------------------------------------------------*/\r
706 \r
707 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )\r
708 {\r
709 signed portBASE_TYPE xReturn;\r
710 \r
711         taskENTER_CRITICAL();\r
712                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
713         taskEXIT_CRITICAL();\r
714 \r
715         return xReturn;\r
716 }\r
717 /*-----------------------------------------------------------*/\r
718 \r
719 #if configUSE_CO_ROUTINES == 1\r
720 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
721 {\r
722 signed portBASE_TYPE xReturn;\r
723                 \r
724         /* If the queue is already full we may have to block.  A critical section\r
725         is required to prevent an interrupt removing something from the queue \r
726         between the check to see if the queue is full and blocking on the queue. */\r
727         portDISABLE_INTERRUPTS();\r
728         {\r
729                 if( prvIsQueueFull( pxQueue ) )\r
730                 {\r
731                         /* The queue is full - do we want to block or just leave without\r
732                         posting? */\r
733                         if( xTicksToWait > ( portTickType ) 0 )\r
734                         {\r
735                                 /* As this is called from a coroutine we cannot block directly, but\r
736                                 return indicating that we need to block. */\r
737                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );                          \r
738                                 portENABLE_INTERRUPTS();\r
739                                 return errQUEUE_BLOCKED;\r
740                         }\r
741                         else\r
742                         {\r
743                                 portENABLE_INTERRUPTS();\r
744                                 return errQUEUE_FULL;\r
745                         }\r
746                 }\r
747         }\r
748         portENABLE_INTERRUPTS();\r
749                 \r
750         portNOP();\r
751 \r
752         portDISABLE_INTERRUPTS();\r
753         {\r
754                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
755                 {\r
756                         /* There is room in the queue, copy the data into the queue. */                 \r
757                         prvCopyQueueData( pxQueue, pvItemToQueue );             \r
758                         xReturn = pdPASS;\r
759 \r
760                         /* Were any co-routines waiting for data to become available? */\r
761                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
762                         {\r
763                                 /* In this instance the co-routine could be placed directly \r
764                                 into the ready list as we are within a critical section.  \r
765                                 Instead the same pending ready list mechansim is used as if\r
766                                 the event were caused from within an interrupt. */\r
767                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
768                                 {\r
769                                         /* The co-routine waiting has a higher priority so record \r
770                                         that a yield might be appropriate. */\r
771                                         xReturn = errQUEUE_YIELD;\r
772                                 }\r
773                         }\r
774                 }\r
775                 else\r
776                 {\r
777                         xReturn = errQUEUE_FULL;\r
778                 }\r
779         }\r
780         portENABLE_INTERRUPTS();\r
781 \r
782         return xReturn;\r
783 }\r
784 #endif\r
785 /*-----------------------------------------------------------*/\r
786 \r
787 #if configUSE_CO_ROUTINES == 1\r
788 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
789 {\r
790 signed portBASE_TYPE xReturn;\r
791 \r
792         /* If the queue is already empty we may have to block.  A critical section\r
793         is required to prevent an interrupt adding something to the queue \r
794         between the check to see if the queue is empty and blocking on the queue. */\r
795         portDISABLE_INTERRUPTS();\r
796         {\r
797                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
798                 {\r
799                         /* There are no messages in the queue, do we want to block or just\r
800                         leave with nothing? */                  \r
801                         if( xTicksToWait > ( portTickType ) 0 )\r
802                         {\r
803                                 /* As this is a co-routine we cannot block directly, but return\r
804                                 indicating that we need to block. */\r
805                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );\r
806                                 portENABLE_INTERRUPTS();\r
807                                 return errQUEUE_BLOCKED;\r
808                         }\r
809                         else\r
810                         {\r
811                                 portENABLE_INTERRUPTS();\r
812                                 return errQUEUE_FULL;\r
813                         }\r
814                 }\r
815         }\r
816         portENABLE_INTERRUPTS();\r
817 \r
818         portNOP();\r
819 \r
820         portDISABLE_INTERRUPTS();\r
821         {\r
822                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
823                 {\r
824                         /* Data is available from the queue. */\r
825                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
826                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
827                         {\r
828                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
829                         }\r
830                         --( pxQueue->uxMessagesWaiting );\r
831                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
832 \r
833                         xReturn = pdPASS;\r
834 \r
835                         /* Were any co-routines waiting for space to become available? */\r
836                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
837                         {\r
838                                 /* In this instance the co-routine could be placed directly \r
839                                 into the ready list as we are within a critical section.  \r
840                                 Instead the same pending ready list mechansim is used as if\r
841                                 the event were caused from within an interrupt. */\r
842                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
843                                 {\r
844                                         xReturn = errQUEUE_YIELD;\r
845                                 }\r
846                         }       \r
847                 }\r
848                 else\r
849                 {\r
850                         xReturn = pdFAIL;\r
851                 }\r
852         }\r
853         portENABLE_INTERRUPTS();\r
854 \r
855         return xReturn;\r
856 }\r
857 #endif\r
858 /*-----------------------------------------------------------*/\r
859 \r
860 \r
861 \r
862 #if configUSE_CO_ROUTINES == 1\r
863 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
864 {\r
865         /* Cannot block within an ISR so if there is no space on the queue then\r
866         exit without doing anything. */\r
867         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
868         {\r
869                 prvCopyQueueData( pxQueue, pvItemToQueue );\r
870 \r
871                 /* We only want to wake one co-routine per ISR, so check that a \r
872                 co-routine has not already been woken. */\r
873                 if( !xCoRoutinePreviouslyWoken )                \r
874                 {\r
875                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
876                         {\r
877                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
878                                 {\r
879                                         return pdTRUE;\r
880                                 }\r
881                         }\r
882                 }\r
883         }\r
884 \r
885         return xCoRoutinePreviouslyWoken;\r
886 }\r
887 #endif\r
888 /*-----------------------------------------------------------*/\r
889 \r
890 #if configUSE_CO_ROUTINES == 1\r
891 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
892 {\r
893 signed portBASE_TYPE xReturn;\r
894 \r
895         /* We cannot block from an ISR, so check there is data available. If\r
896         not then just leave without doing anything. */\r
897         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
898         {\r
899                 /* Copy the data from the queue. */\r
900                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
901                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
902                 {\r
903                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
904                 }\r
905                 --( pxQueue->uxMessagesWaiting );\r
906                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
907 \r
908                 if( !( *pxCoRoutineWoken ) )\r
909                 {\r
910                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
911                         {\r
912                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
913                                 {\r
914                                         *pxCoRoutineWoken = pdTRUE;\r
915                                 }\r
916                         }\r
917                 }\r
918 \r
919                 xReturn = pdPASS;\r
920         }\r
921         else\r
922         {\r
923                 xReturn = pdFAIL;\r
924         }\r
925 \r
926         return xReturn;\r
927 }\r
928 #endif\r
929 /*-----------------------------------------------------------*/\r
930 \r