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