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