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