]> git.sur5r.net Git - freertos/blob - Source/queue.c
22f8d9a42c1c8ecd6afe36c9672211c95051a500
[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         volatile 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 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );\r
111 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );\r
112 \r
113 #if configUSE_CO_ROUTINES == 1\r
114         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );\r
115         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
116         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );\r
117         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );\r
118 #endif\r
119 \r
120 /*\r
121  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not\r
122  * prevent an ISR from adding or removing items to the queue, but does prevent\r
123  * an ISR from removing tasks from the queue event lists.  If an ISR finds a\r
124  * queue is locked it will instead increment the appropriate queue lock count\r
125  * to indicate that a task may require unblocking.  When the queue in unlocked\r
126  * these lock counts are inspected, and the appropriate action taken.\r
127  */\r
128 static void prvUnlockQueue( xQueueHandle pxQueue );\r
129 \r
130 /*\r
131  * Uses a critical section to determine if there is any data in a queue.\r
132  *\r
133  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
134  */\r
135 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );\r
136 \r
137 /*\r
138  * Uses a critical section to determine if there is any space in a queue.\r
139  *\r
140  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
141  */\r
142 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );\r
143 \r
144 /*\r
145  * Copies an item into the queue, either at the front of the queue or the\r
146  * back of the queue.\r
147  */\r
148 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );\r
149 \r
150 /*\r
151  * Copies an item out of a queue.\r
152  */\r
153 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
158  * accessing the queue event lists.\r
159  */\r
160 #define prvLockQueue( pxQueue )                 \\r
161 {                                                                               \\r
162         taskENTER_CRITICAL();                           \\r
163                 ++( pxQueue->xRxLock );                 \\r
164                 ++( pxQueue->xTxLock );                 \\r
165         taskEXIT_CRITICAL();                            \\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 \r
170 /*-----------------------------------------------------------\r
171  * PUBLIC QUEUE MANAGEMENT API documented in queue.h\r
172  *----------------------------------------------------------*/\r
173 \r
174 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )\r
175 {\r
176 xQUEUE *pxNewQueue;\r
177 size_t xQueueSizeInBytes;\r
178 \r
179         /* Allocate the new queue structure. */\r
180         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
181         {\r
182                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
183                 if( pxNewQueue != NULL )\r
184                 {\r
185                         /* Create the list of pointers to queue items.  The queue is one byte\r
186                         longer than asked for to make wrap checking easier/faster. */\r
187                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;\r
188 \r
189                         pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );\r
190                         if( pxNewQueue->pcHead != NULL )\r
191                         {\r
192                                 /* Initialise the queue members as described above where the\r
193                                 queue type is defined. */\r
194                                 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );\r
195                                 pxNewQueue->uxMessagesWaiting = 0;\r
196                                 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;\r
197                                 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );\r
198                                 pxNewQueue->uxLength = uxQueueLength;\r
199                                 pxNewQueue->uxItemSize = uxItemSize;\r
200                                 pxNewQueue->xRxLock = queueUNLOCKED;\r
201                                 pxNewQueue->xTxLock = queueUNLOCKED;\r
202 \r
203                                 /* Likewise ensure the event queues start with the correct state. */\r
204                                 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
205                                 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
206 \r
207                                 return  pxNewQueue;\r
208                         }\r
209                         else\r
210                         {\r
211                                 vPortFree( pxNewQueue );\r
212                         }\r
213                 }\r
214         }\r
215 \r
216         /* Will only reach here if we could not allocate enough memory or no memory\r
217         was required. */\r
218         return NULL;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 #if ( configUSE_MUTEXES == 1 )\r
223 \r
224         xQueueHandle xQueueCreateMutex( void )\r
225         {\r
226         xQUEUE *pxNewQueue;\r
227         \r
228                 /* Allocate the new queue structure. */\r
229                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
230                 if( pxNewQueue != NULL )\r
231                 {\r
232                         /* Information required for priority inheritance. */\r
233                         pxNewQueue->pxMutexHolder = NULL;\r
234                         pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
235         \r
236                         /* Queues used as a mutex no data is actually copied into or out\r
237                         of the queue. */\r
238                         pxNewQueue->pcWriteTo = NULL;\r
239                         pxNewQueue->pcReadFrom = NULL;\r
240                         \r
241                         /* Each mutex has a length of 1 (like a binary semaphore) and\r
242                         an item size of 0 as nothing is actually copied into or out\r
243                         of the mutex. */\r
244                         pxNewQueue->uxMessagesWaiting = 0;\r
245                         pxNewQueue->uxLength = 1;\r
246                         pxNewQueue->uxItemSize = 0;\r
247                         pxNewQueue->xRxLock = queueUNLOCKED;\r
248                         pxNewQueue->xTxLock = queueUNLOCKED;\r
249         \r
250                         /* Ensure the event queues start with the correct state. */\r
251                         vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
252                         vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
253 \r
254                         /* Start with the semaphore in the expected state. */\r
255                         xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );\r
256                 }\r
257         \r
258                 return pxNewQueue;\r
259         }\r
260 \r
261 #endif /* configUSE_MUTEXES */\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 #if configUSE_COUNTING_SEMAPHORES == 1\r
265 \r
266         xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )\r
267         {\r
268         xQueueHandle pxHandle;\r
269         \r
270                 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );\r
271 \r
272                 if( pxHandle != NULL )\r
273                 {\r
274                         pxHandle->uxMessagesWaiting = uxInitialCount;\r
275                 }\r
276 \r
277                 return pxHandle;\r
278         }\r
279 \r
280 #endif /* configUSE_COUNTING_SEMAPHORES */\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
284 {\r
285 signed portBASE_TYPE xReturn = pdPASS;\r
286 xTimeOutType xTimeOut;\r
287 \r
288         /* Make sure other tasks do not access the queue. */\r
289         vTaskSuspendAll();\r
290 \r
291         /* Capture the current time status for future reference. */\r
292         vTaskSetTimeOutState( &xTimeOut );\r
293 \r
294         /* It is important that this is the only thread/ISR that modifies the\r
295         ready or delayed lists until xTaskResumeAll() is called.  Places where\r
296         the ready/delayed lists are modified include:\r
297 \r
298                 + vTaskDelay() -  Nothing can call vTaskDelay as the scheduler is\r
299                   suspended, vTaskDelay() cannot be called from an ISR.\r
300                 + vTaskPrioritySet() - Has a critical section around the access.\r
301                 + vTaskSwitchContext() - This will not get executed while the scheduler\r
302                   is suspended.\r
303                 + prvCheckDelayedTasks() - This will not get executed while the\r
304                   scheduler is suspended.\r
305                 + xTaskCreate() - Has a critical section around the access.\r
306                 + vTaskResume() - Has a critical section around the access.\r
307                 + xTaskResumeAll() - Has a critical section around the access.\r
308                 + xTaskRemoveFromEventList - Checks to see if the scheduler is\r
309                   suspended.  If so then the TCB being removed from the event is\r
310                   removed from the event and added to the xPendingReadyList.\r
311         */\r
312 \r
313         /* Make sure interrupts do not access the queue event list. */\r
314         prvLockQueue( pxQueue );\r
315 \r
316         /* It is important that interrupts to not access the event list of the\r
317         queue being modified here.  Places where the event list is modified\r
318         include:\r
319 \r
320                 + xQueueGenericSendFromISR().  This checks the lock on the queue to see\r
321                   if it has access.  If the queue is locked then the Tx lock count is\r
322                   incremented to signify that a task waiting for data can be made ready\r
323                   once the queue lock is removed.  If the queue is not locked then\r
324                   a task can be moved from the event list, but will not be removed\r
325                   from the delayed list or placed in the ready list until the scheduler\r
326                   is unlocked.\r
327 \r
328                 + xQueueReceiveFromISR().  As per xQueueGenericSendFromISR().\r
329         */\r
330                 \r
331         /* If the queue is already full we may have to block. */\r
332         do\r
333         {\r
334                 if( prvIsQueueFull( pxQueue ) )\r
335                 {\r
336                         /* The queue is full - do we want to block or just leave without\r
337                         posting? */\r
338                         if( xTicksToWait > ( portTickType ) 0 )\r
339                         {\r
340                                 /* We are going to place ourselves on the xTasksWaitingToSend event\r
341                                 list, and will get woken should the delay expire, or space become\r
342                                 available on the queue.\r
343                                 \r
344                                 As detailed above we do not require mutual exclusion on the event\r
345                                 list as nothing else can modify it or the ready lists while we\r
346                                 have the scheduler suspended and queue locked.\r
347                                 \r
348                                 It is possible that an ISR has removed data from the queue since we\r
349                                 checked if any was available.  If this is the case then the data\r
350                                 will have been copied from the queue, and the queue variables\r
351                                 updated, but the event list will not yet have been checked to see if\r
352                                 anything is waiting as the queue is locked. */\r
353                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
354         \r
355                                 /* Force a context switch now as we are blocked.  We can do\r
356                                 this from within a critical section as the task we are\r
357                                 switching to has its own context.  When we return here (i.e. we\r
358                                 unblock) we will leave the critical section as normal.\r
359                                 \r
360                                 It is possible that an ISR has caused an event on an unrelated and\r
361                                 unlocked queue.  If this was the case then the event list for that\r
362                                 queue will have been updated but the ready lists left unchanged -\r
363                                 instead the readied task will have been added to the pending ready\r
364                                 list. */\r
365                                 taskENTER_CRITICAL();\r
366                                 {\r
367                                         /* We can safely unlock the queue and scheduler here as\r
368                                         interrupts are disabled.  We must not yield with anything\r
369                                         locked, but we can yield from within a critical section.\r
370                                         \r
371                                         Tasks that have been placed on the pending ready list cannot\r
372                                         be tasks that are waiting for events on this queue.  See\r
373                                         in comment xTaskRemoveFromEventList(). */\r
374                                         prvUnlockQueue( pxQueue );\r
375         \r
376                                         /* Resuming the scheduler may cause a yield.  If so then there\r
377                                         is no point yielding again here. */\r
378                                         if( !xTaskResumeAll() )\r
379                                         {\r
380                                                 taskYIELD();\r
381                                         }\r
382 \r
383                                         /* We want to check to see if the queue is still full\r
384                                         before leaving the critical section.  This is to prevent\r
385                                         this task placing an item into the queue due to an\r
386                                         interrupt making space on the queue between critical\r
387                                         sections (when there might be a higher priority task\r
388                                         blocked on the queue that cannot run yet because the\r
389                                         scheduler gets suspended). */\r
390                                         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
391                                         {\r
392                                                 /* We unblocked but there is no space in the queue,\r
393                                                 we probably timed out. */\r
394                                                 xReturn = errQUEUE_FULL;\r
395                                         }\r
396         \r
397                                         /* Before leaving the critical section we have to ensure\r
398                                         exclusive access again. */\r
399                                         vTaskSuspendAll();\r
400                                         prvLockQueue( pxQueue );                                \r
401                                 }\r
402                                 taskEXIT_CRITICAL();\r
403                         }\r
404                 }\r
405                         \r
406                 /* If xReturn is errQUEUE_FULL then we unblocked when the queue\r
407                 was still full.  Don't check it again now as it is possible that\r
408                 an interrupt has removed an item from the queue since we left the\r
409                 critical section and we don't want to write to the queue in case\r
410                 there is a task of higher priority blocked waiting for space to\r
411                 be available on the queue.  If this is the case the higher priority\r
412                 task will execute when the scheduler is unsupended. */\r
413                 if( xReturn != errQUEUE_FULL )\r
414                 {\r
415                         /* When we are here it is possible that we unblocked as space became\r
416                         available on the queue.  It is also possible that an ISR posted to the\r
417                         queue since we left the critical section, so it may be that again there\r
418                         is no space.  This would only happen if a task and ISR post onto the\r
419                         same queue. */\r
420                         taskENTER_CRITICAL();\r
421                         {\r
422                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
423                                 {\r
424                                         /* There is room in the queue, copy the data into the queue. */                 \r
425                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
426                                         xReturn = pdPASS;\r
427                 \r
428                                         /* Update the TxLock count so prvUnlockQueue knows to check for\r
429                                         tasks waiting for data to become available in the queue. */\r
430                                         ++( pxQueue->xTxLock );\r
431                                 }\r
432                                 else\r
433                                 {\r
434                                         xReturn = errQUEUE_FULL;\r
435                                 }\r
436                         }\r
437                         taskEXIT_CRITICAL();\r
438                 }\r
439 \r
440                 if( xReturn == errQUEUE_FULL )\r
441                 {\r
442                         if( xTicksToWait > 0 )\r
443                         {\r
444                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
445                                 {\r
446                                         xReturn = queueERRONEOUS_UNBLOCK;\r
447                                 }\r
448                         }\r
449                 }\r
450         }\r
451         while( xReturn == queueERRONEOUS_UNBLOCK );\r
452 \r
453         prvUnlockQueue( pxQueue );\r
454         xTaskResumeAll();\r
455 \r
456         return xReturn;\r
457 }\r
458 /*-----------------------------------------------------------*/\r
459 \r
460 #if configUSE_ALTERNATIVE_API == 1\r
461 \r
462         signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
463         {\r
464         signed portBASE_TYPE xReturn;\r
465         xTimeOutType xTimeOut;\r
466 \r
467                 /* The source code that implements the light weight (fast) API is much \r
468                 simpler because it executes everything from within a critical section.  \r
469                 This is the approach taken by many other RTOSes, but FreeRTOS.org has the \r
470                 fully featured API as an alternative.  The fully featured API has more \r
471                 complex code that takes longer to execute, but makes much less use of \r
472                 critical sections.  Therefore the light weight API sacrifices interrupt \r
473                 responsiveness to gain execution speed, whereas the fully featured API\r
474                 sacrifices execution speed to ensure better interrupt responsiveness.  */\r
475 \r
476                 taskENTER_CRITICAL();\r
477                 {\r
478                         /* Capture the current time status for future reference. */\r
479                         vTaskSetTimeOutState( &xTimeOut );\r
480 \r
481                         /* If the queue is already full we may have to block. */\r
482                         do\r
483                         {\r
484                                 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
485                                 {\r
486                                         /* The queue is full - do we want to block or just leave without\r
487                                         posting? */\r
488                                         if( xTicksToWait > ( portTickType ) 0 )\r
489                                         {\r
490                                                 /* We are going to place ourselves on the xTasksWaitingToSend \r
491                                                 event list, and will get woken should the delay expire, or \r
492                                                 space become available on the queue. */\r
493                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
494                         \r
495                                                 /* Force a context switch now as we are blocked.  We can do\r
496                                                 this from within a critical section as the task we are\r
497                                                 switching to has its own context.  When we return here (i.e.\r
498                                                 we unblock) we will leave the critical section as normal. */\r
499                                                 taskYIELD();\r
500                                         }\r
501                                 }\r
502                                         \r
503                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
504                                 {\r
505                                         /* There is room in the queue, copy the data into the queue. */                 \r
506                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
507                                         xReturn = pdPASS;\r
508 \r
509                                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
510                                         {\r
511                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
512                                                 {\r
513                                                         /* The task waiting has a higher priority. */\r
514                                                         taskYIELD();\r
515                                                 }\r
516                                         }                       \r
517                                 }\r
518                                 else\r
519                                 {\r
520                                         xReturn = errQUEUE_FULL;\r
521 \r
522                                         if( xTicksToWait > 0 )\r
523                                         {                                       \r
524                                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
525                                                 {\r
526                                                         /* Another task must have accessed the queue between \r
527                                                         this task unblocking and actually executing. */\r
528                                                         xReturn = queueERRONEOUS_UNBLOCK;\r
529                                                 }\r
530                                         }\r
531                                         else\r
532                                         {\r
533                                                 \r
534                                         }\r
535                                 }\r
536                         }\r
537                         while( xReturn == queueERRONEOUS_UNBLOCK );\r
538                 }\r
539                 taskEXIT_CRITICAL();\r
540 \r
541                 return xReturn;\r
542         }\r
543 \r
544 #endif /* configUSE_ALTERNATIVE_API */\r
545 /*-----------------------------------------------------------*/\r
546 \r
547 #if configUSE_ALTERNATIVE_API == 1\r
548 \r
549         signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
550         {\r
551         signed portBASE_TYPE xReturn = pdTRUE;\r
552         xTimeOutType xTimeOut;\r
553         signed portCHAR *pcOriginalReadPosition;\r
554 \r
555                 /* The source code that implements the light weight (fast) API is much \r
556                 simpler because it executes everything from within a critical section.  \r
557                 This is the approach taken by many other RTOSes, but FreeRTOS.org has the \r
558                 fully featured API as an alternative.  The fully featured API has more \r
559                 complex code that takes longer to execute, but makes much less use of \r
560                 critical sections.  Therefore the light weight API sacrifices interrupt \r
561                 responsiveness to gain execution speed, whereas the fully featured API\r
562                 sacrifices execution speed to ensure better interrupt responsiveness.  */\r
563 \r
564                 taskENTER_CRITICAL();\r
565                 {\r
566                         /* Capture the current time status for future reference. */\r
567                         vTaskSetTimeOutState( &xTimeOut );\r
568 \r
569                         do\r
570                         {\r
571                                 /* If there are no messages in the queue we may have to block. */\r
572                                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
573                                 {\r
574                                         /* There are no messages in the queue, do we want to block or just\r
575                                         leave with nothing? */                  \r
576                                         if( xTicksToWait > ( portTickType ) 0 )\r
577                                         {\r
578                                                 #if ( configUSE_MUTEXES == 1 )\r
579                                                 {\r
580                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
581                                                         {\r
582                                                                 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );\r
583                                                         }\r
584                                                 }\r
585                                                 #endif\r
586                                                 \r
587                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
588                                                 taskYIELD();\r
589                                         }\r
590                                 }\r
591                         \r
592                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
593                                 {\r
594                                         /* Remember our read position in case we are just peeking. */\r
595                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
596 \r
597                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
598 \r
599                                         if( xJustPeeking == pdFALSE )\r
600                                         {\r
601                                                 /* We are actually removing data. */\r
602                                                 --( pxQueue->uxMessagesWaiting );\r
603                                                         \r
604                                                 #if ( configUSE_MUTEXES == 1 )\r
605                                                 {\r
606                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
607                                                         {\r
608                                                                 /* Record the information required to implement\r
609                                                                 priority inheritance should it become necessary. */\r
610                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
611                                                         }\r
612                                                 }\r
613                                                 #endif\r
614 \r
615                                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
616                                                 {\r
617                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
618                                                         {\r
619                                                                 /* The task waiting has a higher priority. */\r
620                                                                 taskYIELD();\r
621                                                         }\r
622                                                 }\r
623                                         }\r
624                                         else\r
625                                         {\r
626                                                 /* We are not removing the data, so reset our read\r
627                                                 pointer. */\r
628                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;\r
629                                         }\r
630                                         \r
631                                         xReturn = pdPASS;                                       \r
632                                 }\r
633                                 else\r
634                                 {\r
635                                         xReturn = errQUEUE_EMPTY;\r
636 \r
637                                         if( xTicksToWait > 0 )\r
638                                         {\r
639                                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
640                                                 {\r
641                                                         xReturn = queueERRONEOUS_UNBLOCK;\r
642                                                 }\r
643                                         }\r
644                                 }\r
645 \r
646                         } while( xReturn == queueERRONEOUS_UNBLOCK );\r
647                 }\r
648                 taskEXIT_CRITICAL();\r
649 \r
650                 return xReturn;\r
651         }\r
652 \r
653 #endif /* configUSE_ALTERNATIVE_API */\r
654 /*-----------------------------------------------------------*/\r
655 \r
656 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )\r
657 {\r
658         /* Similar to xQueueGenericSend, except we don't block if there is no room\r
659         in the queue.  Also we don't directly wake a task that was blocked on a\r
660         queue read, instead we return a flag to say whether a context switch is\r
661         required or not (i.e. has a task with a higher priority than us been woken\r
662         by this post). */\r
663         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
664         {\r
665                 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
666 \r
667                 /* If the queue is locked we do not alter the event list.  This will\r
668                 be done when the queue is unlocked later. */\r
669                 if( pxQueue->xTxLock == queueUNLOCKED )\r
670                 {\r
671                         /* We only want to wake one task per ISR, so check that a task has\r
672                         not already been woken. */\r
673                         if( !xTaskPreviouslyWoken )             \r
674                         {\r
675                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
676                                 {\r
677                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
678                                         {\r
679                                                 /* The task waiting has a higher priority so record that a\r
680                                                 context switch is required. */\r
681                                                 return pdTRUE;\r
682                                         }\r
683                                 }\r
684                         }\r
685                 }\r
686                 else\r
687                 {\r
688                         /* Increment the lock count so the task that unlocks the queue\r
689                         knows that data was posted while it was locked. */\r
690                         ++( pxQueue->xTxLock );\r
691                 }\r
692         }\r
693 \r
694         return xTaskPreviouslyWoken;\r
695 }\r
696 /*-----------------------------------------------------------*/\r
697 \r
698 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
699 {\r
700 signed portBASE_TYPE xReturn = pdTRUE;\r
701 xTimeOutType xTimeOut;\r
702 signed portCHAR *pcOriginalReadPosition;\r
703 \r
704         /* This function is very similar to xQueueGenericSend().  See comments\r
705         within xQueueGenericSend() for a more detailed explanation.\r
706 \r
707         Make sure other tasks do not access the queue. */\r
708         vTaskSuspendAll();\r
709 \r
710         /* Capture the current time status for future reference. */\r
711         vTaskSetTimeOutState( &xTimeOut );\r
712 \r
713         /* Make sure interrupts do not access the queue. */\r
714         prvLockQueue( pxQueue );\r
715 \r
716         do\r
717         {\r
718                 /* If there are no messages in the queue we may have to block. */\r
719                 if( prvIsQueueEmpty( pxQueue ) )\r
720                 {\r
721                         /* There are no messages in the queue, do we want to block or just\r
722                         leave with nothing? */                  \r
723                         if( xTicksToWait > ( portTickType ) 0 )\r
724                         {\r
725                                 #if ( configUSE_MUTEXES == 1 )\r
726                                 {\r
727                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
728                                         {\r
729                                                 portENTER_CRITICAL();\r
730                                                         vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );\r
731                                                 portEXIT_CRITICAL();\r
732                                         }\r
733                                 }\r
734                                 #endif\r
735                                 \r
736                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
737                                 taskENTER_CRITICAL();\r
738                                 {\r
739                                         prvUnlockQueue( pxQueue );\r
740                                         if( !xTaskResumeAll() )\r
741                                         {\r
742                                                 taskYIELD();\r
743                                         }\r
744 \r
745                                         if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
746                                         {\r
747                                                 /* We unblocked but the queue is empty.  We probably\r
748                                                 timed out. */\r
749                                                 xReturn = errQUEUE_EMPTY;\r
750                                         }\r
751         \r
752                                         vTaskSuspendAll();\r
753                                         prvLockQueue( pxQueue );\r
754                                 }\r
755                                 taskEXIT_CRITICAL();\r
756                         }\r
757                 }\r
758         \r
759                 if( xReturn != errQUEUE_EMPTY )\r
760                 {\r
761                         taskENTER_CRITICAL();\r
762                         {\r
763                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
764                                 {\r
765                                         /* Remember our read position in case we are just peeking. */\r
766                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
767 \r
768                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
769 \r
770                                         if( xJustPeeking == pdFALSE )\r
771                                         {\r
772                                                 /* We are actually removing data. */\r
773                                                 --( pxQueue->uxMessagesWaiting );\r
774                                                         \r
775                                                 /* Increment the lock count so prvUnlockQueue knows to check for\r
776                                                 tasks waiting for space to become available on the queue. */\r
777                                                 ++( pxQueue->xRxLock );\r
778                                                 \r
779                                                 #if ( configUSE_MUTEXES == 1 )\r
780                                                 {\r
781                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
782                                                         {\r
783                                                                 /* Record the information required to implement\r
784                                                                 priority inheritance should it become necessary. */\r
785                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
786                                                         }\r
787                                                 }\r
788                                                 #endif\r
789                                         }\r
790                                         else\r
791                                         {\r
792                                                 /* We are not removing the data, so reset our read\r
793                                                 pointer. */\r
794                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;\r
795 \r
796                                                 /* The data is being left in the queue, so increment the\r
797                                                 lock count so prvUnlockQueue knows to check for other\r
798                                                 tasks waiting for the data to be available. */\r
799                                                 ++( pxQueue->xTxLock );                                         \r
800                                         }\r
801                                         \r
802                                         xReturn = pdPASS;                                       \r
803                                 }\r
804                                 else\r
805                                 {\r
806                                         xReturn = errQUEUE_EMPTY;\r
807                                 }\r
808                         }\r
809                         taskEXIT_CRITICAL();\r
810                 }\r
811 \r
812                 if( xReturn == errQUEUE_EMPTY )\r
813                 {\r
814                         if( xTicksToWait > 0 )\r
815                         {\r
816                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
817                                 {\r
818                                         xReturn = queueERRONEOUS_UNBLOCK;\r
819                                 }\r
820                         }\r
821                 }\r
822         } while( xReturn == queueERRONEOUS_UNBLOCK );\r
823 \r
824         /* We no longer require exclusive access to the queue. */\r
825         prvUnlockQueue( pxQueue );\r
826         xTaskResumeAll();\r
827 \r
828         return xReturn;\r
829 }\r
830 /*-----------------------------------------------------------*/\r
831 \r
832 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )\r
833 {\r
834 signed portBASE_TYPE xReturn;\r
835 \r
836         /* We cannot block from an ISR, so check there is data available. */\r
837         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
838         {\r
839                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
840                 --( pxQueue->uxMessagesWaiting );\r
841 \r
842                 /* If the queue is locked we will not modify the event list.  Instead\r
843                 we update the lock count so the task that unlocks the queue will know\r
844                 that an ISR has removed data while the queue was locked. */\r
845                 if( pxQueue->xRxLock == queueUNLOCKED )\r
846                 {\r
847                         /* We only want to wake one task per ISR, so check that a task has\r
848                         not already been woken. */\r
849                         if( !( *pxTaskWoken ) )\r
850                         {\r
851                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
852                                 {\r
853                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
854                                         {\r
855                                                 /* The task waiting has a higher priority than us so\r
856                                                 force a context switch. */\r
857                                                 *pxTaskWoken = pdTRUE;\r
858                                         }\r
859                                 }\r
860                         }\r
861                 }\r
862                 else\r
863                 {\r
864                         /* Increment the lock count so the task that unlocks the queue\r
865                         knows that data was removed while it was locked. */\r
866                         ++( pxQueue->xRxLock );\r
867                 }\r
868 \r
869                 xReturn = pdPASS;\r
870         }\r
871         else\r
872         {\r
873                 xReturn = pdFAIL;\r
874         }\r
875 \r
876         return xReturn;\r
877 }\r
878 /*-----------------------------------------------------------*/\r
879 \r
880 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )\r
881 {\r
882 unsigned portBASE_TYPE uxReturn;\r
883 \r
884         taskENTER_CRITICAL();\r
885                 uxReturn = pxQueue->uxMessagesWaiting;\r
886         taskEXIT_CRITICAL();\r
887 \r
888         return uxReturn;\r
889 }\r
890 /*-----------------------------------------------------------*/\r
891 \r
892 void vQueueDelete( xQueueHandle pxQueue )\r
893 {\r
894         vPortFree( pxQueue->pcHead );\r
895         vPortFree( pxQueue );\r
896 }\r
897 /*-----------------------------------------------------------*/\r
898 \r
899 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )\r
900 {\r
901         if( pxQueue->uxItemSize == 0 )\r
902         {\r
903                 #if ( configUSE_MUTEXES == 1 )\r
904                 {\r
905                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
906                         {\r
907                                 /* The mutex is no longer being held. */\r
908                                 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );\r
909                         }\r
910                 }\r
911                 #endif\r
912         }\r
913         else if( xPosition == queueSEND_TO_BACK )\r
914         {\r
915                 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
916                 pxQueue->pcWriteTo += pxQueue->uxItemSize;\r
917                 if( pxQueue->pcWriteTo >= pxQueue->pcTail )\r
918                 {\r
919                         pxQueue->pcWriteTo = pxQueue->pcHead;\r
920                 }\r
921         }\r
922         else\r
923         {\r
924                 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
925                 pxQueue->pcReadFrom -= pxQueue->uxItemSize;\r
926                 if( pxQueue->pcReadFrom < pxQueue->pcHead )\r
927                 {\r
928                         pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
929                 }               \r
930         }\r
931 \r
932         ++( pxQueue->uxMessagesWaiting );\r
933 }\r
934 /*-----------------------------------------------------------*/\r
935 \r
936 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )\r
937 {\r
938         if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
939         {\r
940                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
941                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
942                 {\r
943                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
944                 }\r
945                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
946         }       \r
947 }\r
948 /*-----------------------------------------------------------*/\r
949 \r
950 static void prvUnlockQueue( xQueueHandle pxQueue )\r
951 {\r
952         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
953 \r
954         /* The lock counts contains the number of extra data items placed or\r
955         removed from the queue while the queue was locked.  When a queue is\r
956         locked items can be added or removed, but the event lists cannot be\r
957         updated. */\r
958         taskENTER_CRITICAL();\r
959         {\r
960                 --( pxQueue->xTxLock );\r
961 \r
962                 /* See if data was added to the queue while it was locked. */\r
963                 if( pxQueue->xTxLock > queueUNLOCKED )\r
964                 {\r
965                         pxQueue->xTxLock = queueUNLOCKED;\r
966 \r
967                         /* Data was posted while the queue was locked.  Are any tasks\r
968                         blocked waiting for data to become available? */\r
969                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
970                         {\r
971                                 /* Tasks that are removed from the event list will get added to\r
972                                 the pending ready list as the scheduler is still suspended. */\r
973                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
974                                 {\r
975                                         /* The task waiting has a higher priority so record that a\r
976                                         context switch is required. */\r
977                                         vTaskMissedYield();\r
978                                 }\r
979                         }                       \r
980                 }\r
981         }\r
982         taskEXIT_CRITICAL();\r
983 \r
984         /* Do the same for the Rx lock. */\r
985         taskENTER_CRITICAL();\r
986         {\r
987                 --( pxQueue->xRxLock );\r
988 \r
989                 if( pxQueue->xRxLock > queueUNLOCKED )\r
990                 {\r
991                         pxQueue->xRxLock = queueUNLOCKED;\r
992 \r
993                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
994                         {\r
995                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
996                                 {\r
997                                         vTaskMissedYield();\r
998                                 }\r
999                         }                       \r
1000                 }\r
1001         }\r
1002         taskEXIT_CRITICAL();\r
1003 }\r
1004 /*-----------------------------------------------------------*/\r
1005 \r
1006 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )\r
1007 {\r
1008 signed portBASE_TYPE xReturn;\r
1009 \r
1010         taskENTER_CRITICAL();\r
1011                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
1012         taskEXIT_CRITICAL();\r
1013 \r
1014         return xReturn;\r
1015 }\r
1016 /*-----------------------------------------------------------*/\r
1017 \r
1018 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )\r
1019 {\r
1020 signed portBASE_TYPE xReturn;\r
1021 \r
1022         taskENTER_CRITICAL();\r
1023                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
1024         taskEXIT_CRITICAL();\r
1025 \r
1026         return xReturn;\r
1027 }\r
1028 /*-----------------------------------------------------------*/\r
1029 \r
1030 #if configUSE_CO_ROUTINES == 1\r
1031 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
1032 {\r
1033 signed portBASE_TYPE xReturn;\r
1034                 \r
1035         /* If the queue is already full we may have to block.  A critical section\r
1036         is required to prevent an interrupt removing something from the queue\r
1037         between the check to see if the queue is full and blocking on the queue. */\r
1038         portDISABLE_INTERRUPTS();\r
1039         {\r
1040                 if( prvIsQueueFull( pxQueue ) )\r
1041                 {\r
1042                         /* The queue is full - do we want to block or just leave without\r
1043                         posting? */\r
1044                         if( xTicksToWait > ( portTickType ) 0 )\r
1045                         {\r
1046                                 /* As this is called from a coroutine we cannot block directly, but\r
1047                                 return indicating that we need to block. */\r
1048                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );                          \r
1049                                 portENABLE_INTERRUPTS();\r
1050                                 return errQUEUE_BLOCKED;\r
1051                         }\r
1052                         else\r
1053                         {\r
1054                                 portENABLE_INTERRUPTS();\r
1055                                 return errQUEUE_FULL;\r
1056                         }\r
1057                 }\r
1058         }\r
1059         portENABLE_INTERRUPTS();\r
1060                 \r
1061         portNOP();\r
1062 \r
1063         portDISABLE_INTERRUPTS();\r
1064         {\r
1065                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1066                 {\r
1067                         /* There is room in the queue, copy the data into the queue. */                 \r
1068                         prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1069                         xReturn = pdPASS;\r
1070 \r
1071                         /* Were any co-routines waiting for data to become available? */\r
1072                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
1073                         {\r
1074                                 /* In this instance the co-routine could be placed directly\r
1075                                 into the ready list as we are within a critical section.\r
1076                                 Instead the same pending ready list mechanism is used as if\r
1077                                 the event were caused from within an interrupt. */\r
1078                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1079                                 {\r
1080                                         /* The co-routine waiting has a higher priority so record\r
1081                                         that a yield might be appropriate. */\r
1082                                         xReturn = errQUEUE_YIELD;\r
1083                                 }\r
1084                         }\r
1085                 }\r
1086                 else\r
1087                 {\r
1088                         xReturn = errQUEUE_FULL;\r
1089                 }\r
1090         }\r
1091         portENABLE_INTERRUPTS();\r
1092 \r
1093         return xReturn;\r
1094 }\r
1095 #endif\r
1096 /*-----------------------------------------------------------*/\r
1097 \r
1098 #if configUSE_CO_ROUTINES == 1\r
1099 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
1100 {\r
1101 signed portBASE_TYPE xReturn;\r
1102 \r
1103         /* If the queue is already empty we may have to block.  A critical section\r
1104         is required to prevent an interrupt adding something to the queue\r
1105         between the check to see if the queue is empty and blocking on the queue. */\r
1106         portDISABLE_INTERRUPTS();\r
1107         {\r
1108                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
1109                 {\r
1110                         /* There are no messages in the queue, do we want to block or just\r
1111                         leave with nothing? */                  \r
1112                         if( xTicksToWait > ( portTickType ) 0 )\r
1113                         {\r
1114                                 /* As this is a co-routine we cannot block directly, but return\r
1115                                 indicating that we need to block. */\r
1116                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );\r
1117                                 portENABLE_INTERRUPTS();\r
1118                                 return errQUEUE_BLOCKED;\r
1119                         }\r
1120                         else\r
1121                         {\r
1122                                 portENABLE_INTERRUPTS();\r
1123                                 return errQUEUE_FULL;\r
1124                         }\r
1125                 }\r
1126         }\r
1127         portENABLE_INTERRUPTS();\r
1128 \r
1129         portNOP();\r
1130 \r
1131         portDISABLE_INTERRUPTS();\r
1132         {\r
1133                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1134                 {\r
1135                         /* Data is available from the queue. */\r
1136                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1137                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1138                         {\r
1139                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
1140                         }\r
1141                         --( pxQueue->uxMessagesWaiting );\r
1142                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1143 \r
1144                         xReturn = pdPASS;\r
1145 \r
1146                         /* Were any co-routines waiting for space to become available? */\r
1147                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
1148                         {\r
1149                                 /* In this instance the co-routine could be placed directly\r
1150                                 into the ready list as we are within a critical section.\r
1151                                 Instead the same pending ready list mechanism is used as if\r
1152                                 the event were caused from within an interrupt. */\r
1153                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1154                                 {\r
1155                                         xReturn = errQUEUE_YIELD;\r
1156                                 }\r
1157                         }       \r
1158                 }\r
1159                 else\r
1160                 {\r
1161                         xReturn = pdFAIL;\r
1162                 }\r
1163         }\r
1164         portENABLE_INTERRUPTS();\r
1165 \r
1166         return xReturn;\r
1167 }\r
1168 #endif\r
1169 /*-----------------------------------------------------------*/\r
1170 \r
1171 \r
1172 \r
1173 #if configUSE_CO_ROUTINES == 1\r
1174 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
1175 {\r
1176         /* Cannot block within an ISR so if there is no space on the queue then\r
1177         exit without doing anything. */\r
1178         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1179         {\r
1180                 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1181 \r
1182                 /* We only want to wake one co-routine per ISR, so check that a\r
1183                 co-routine has not already been woken. */\r
1184                 if( !xCoRoutinePreviouslyWoken )                \r
1185                 {\r
1186                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
1187                         {\r
1188                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1189                                 {\r
1190                                         return pdTRUE;\r
1191                                 }\r
1192                         }\r
1193                 }\r
1194         }\r
1195 \r
1196         return xCoRoutinePreviouslyWoken;\r
1197 }\r
1198 #endif\r
1199 /*-----------------------------------------------------------*/\r
1200 \r
1201 #if configUSE_CO_ROUTINES == 1\r
1202 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
1203 {\r
1204 signed portBASE_TYPE xReturn;\r
1205 \r
1206         /* We cannot block from an ISR, so check there is data available. If\r
1207         not then just leave without doing anything. */\r
1208         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1209         {\r
1210                 /* Copy the data from the queue. */\r
1211                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1212                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1213                 {\r
1214                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1215                 }\r
1216                 --( pxQueue->uxMessagesWaiting );\r
1217                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1218 \r
1219                 if( !( *pxCoRoutineWoken ) )\r
1220                 {\r
1221                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
1222                         {\r
1223                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1224                                 {\r
1225                                         *pxCoRoutineWoken = pdTRUE;\r
1226                                 }\r
1227                         }\r
1228                 }\r
1229 \r
1230                 xReturn = pdPASS;\r
1231         }\r
1232         else\r
1233         {\r
1234                 xReturn = pdFAIL;\r
1235         }\r
1236 \r
1237         return xReturn;\r
1238 }\r
1239 #endif\r
1240 /*-----------------------------------------------------------*/\r
1241 \r