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