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