2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
52 #include "FreeRTOS.h"
\r
54 #include "croutine.h"
\r
56 /*-----------------------------------------------------------
\r
57 * PUBLIC LIST API documented in list.h
\r
58 *----------------------------------------------------------*/
\r
60 /* Constants used with the cRxLock and cTxLock structure members. */
\r
61 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
62 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
64 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
66 /* For internal use only. */
\r
67 #define queueSEND_TO_BACK ( 0 )
\r
68 #define queueSEND_TO_FRONT ( 1 )
\r
70 /* Effectively make a union out of the xQUEUE structure. */
\r
71 #define pxMutexHolder pcTail
\r
72 #define uxQueueType pcHead
\r
73 #define uxRecursiveCallCount pcReadFrom
\r
74 #define queueQUEUE_IS_MUTEX NULL
\r
76 /* Semaphores do not actually store or copy data, so have an items size of
\r
78 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
79 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
80 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
82 * Definition of the queue used by the scheduler.
\r
83 * Items are queued by copy, not reference.
\r
85 typedef struct QueueDefinition
\r
87 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
88 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
90 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
91 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
93 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
94 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
96 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
97 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
98 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
100 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
101 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
103 /*-----------------------------------------------------------*/
\r
106 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
107 * To keep the definition private the API header file defines it as a
\r
110 typedef xQUEUE * xQueueHandle;
\r
113 * Prototypes for public functions are included here so we don't have to
\r
114 * include the API header file (as it defines xQueueHandle differently). These
\r
115 * functions are documented in the API header file.
\r
117 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
118 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
119 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue );
\r
120 void vQueueDelete( xQueueHandle xQueue );
\r
121 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition );
\r
122 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
123 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
124 xQueueHandle xQueueCreateMutex( void );
\r
125 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );
\r
126 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime );
\r
127 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex );
\r
128 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
129 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
130 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue );
\r
131 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue );
\r
132 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue );
\r
135 #if configUSE_CO_ROUTINES == 1
\r
136 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
137 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
138 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
139 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
143 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
144 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
145 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
146 * queue is locked it will instead increment the appropriate queue lock count
\r
147 * to indicate that a task may require unblocking. When the queue in unlocked
\r
148 * these lock counts are inspected, and the appropriate action taken.
\r
150 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
153 * Uses a critical section to determine if there is any data in a queue.
\r
155 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
157 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
160 * Uses a critical section to determine if there is any space in a queue.
\r
162 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
164 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
167 * Copies an item into the queue, either at the front of the queue or the
\r
168 * back of the queue.
\r
170 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
173 * Copies an item out of a queue.
\r
175 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
176 /*-----------------------------------------------------------*/
\r
179 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
180 * accessing the queue event lists.
\r
182 #define prvLockQueue( pxQueue ) \
\r
184 taskENTER_CRITICAL(); \
\r
186 if( pxQueue->xRxLock == queueUNLOCKED ) \
\r
188 pxQueue->xRxLock = queueLOCKED_UNMODIFIED; \
\r
190 if( pxQueue->xTxLock == queueUNLOCKED ) \
\r
192 pxQueue->xTxLock = queueLOCKED_UNMODIFIED; \
\r
195 taskEXIT_CRITICAL(); \
\r
197 /*-----------------------------------------------------------*/
\r
200 /*-----------------------------------------------------------
\r
201 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
202 *----------------------------------------------------------*/
\r
204 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
206 xQUEUE *pxNewQueue;
\r
207 size_t xQueueSizeInBytes;
\r
209 /* Allocate the new queue structure. */
\r
210 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
212 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
213 if( pxNewQueue != NULL )
\r
215 /* Create the list of pointers to queue items. The queue is one byte
\r
216 longer than asked for to make wrap checking easier/faster. */
\r
217 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
219 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
220 if( pxNewQueue->pcHead != NULL )
\r
222 /* Initialise the queue members as described above where the
\r
223 queue type is defined. */
\r
224 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
225 pxNewQueue->uxMessagesWaiting = 0;
\r
226 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
227 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
228 pxNewQueue->uxLength = uxQueueLength;
\r
229 pxNewQueue->uxItemSize = uxItemSize;
\r
230 pxNewQueue->xRxLock = queueUNLOCKED;
\r
231 pxNewQueue->xTxLock = queueUNLOCKED;
\r
233 /* Likewise ensure the event queues start with the correct state. */
\r
234 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
235 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
237 traceQUEUE_CREATE( pxNewQueue );
\r
243 traceQUEUE_CREATE_FAILED();
\r
244 vPortFree( pxNewQueue );
\r
249 /* Will only reach here if we could not allocate enough memory or no memory
\r
253 /*-----------------------------------------------------------*/
\r
255 #if ( configUSE_MUTEXES == 1 )
\r
257 xQueueHandle xQueueCreateMutex( void )
\r
259 xQUEUE *pxNewQueue;
\r
261 /* Allocate the new queue structure. */
\r
262 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
263 if( pxNewQueue != NULL )
\r
265 /* Information required for priority inheritance. */
\r
266 pxNewQueue->pxMutexHolder = NULL;
\r
267 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
269 /* Queues used as a mutex no data is actually copied into or out
\r
271 pxNewQueue->pcWriteTo = NULL;
\r
272 pxNewQueue->pcReadFrom = NULL;
\r
274 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
275 an item size of 0 as nothing is actually copied into or out
\r
277 pxNewQueue->uxMessagesWaiting = 0;
\r
278 pxNewQueue->uxLength = 1;
\r
279 pxNewQueue->uxItemSize = 0;
\r
280 pxNewQueue->xRxLock = queueUNLOCKED;
\r
281 pxNewQueue->xTxLock = queueUNLOCKED;
\r
283 /* Ensure the event queues start with the correct state. */
\r
284 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
285 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
287 /* Start with the semaphore in the expected state. */
\r
288 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
290 traceCREATE_MUTEX( pxNewQueue );
\r
294 traceCREATE_MUTEX_FAILED();
\r
300 #endif /* configUSE_MUTEXES */
\r
301 /*-----------------------------------------------------------*/
\r
303 #if configUSE_RECURSIVE_MUTEXES == 1
\r
305 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
307 portBASE_TYPE xReturn;
\r
309 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
310 change outside of this task. If this task does not hold the mutex then
\r
311 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
312 this is the only condition we are interested in it does not matter if
\r
313 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
314 mutual exclusion is required to test the pxMutexHolder variable. */
\r
315 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
317 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
319 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
320 the task handle, therefore no underflow check is required. Also,
\r
321 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
322 there can only be one, no mutual exclusion is required to modify the
\r
323 uxRecursiveCallCount member. */
\r
324 ( pxMutex->uxRecursiveCallCount )--;
\r
326 /* Have we unwound the call count? */
\r
327 if( pxMutex->uxRecursiveCallCount == 0 )
\r
329 /* Return the mutex. This will automatically unblock any other
\r
330 task that might be waiting to access the mutex. */
\r
331 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
338 /* We cannot give the mutex because we are not the holder. */
\r
341 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
347 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
348 /*-----------------------------------------------------------*/
\r
350 #if configUSE_RECURSIVE_MUTEXES == 1
\r
352 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
354 portBASE_TYPE xReturn;
\r
356 /* Comments regarding mutual exclusion as per those within
\r
357 xQueueGiveMutexRecursive(). */
\r
359 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
361 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
363 ( pxMutex->uxRecursiveCallCount )++;
\r
368 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
370 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
371 we may have blocked to reach here. */
\r
372 if( xReturn == pdPASS )
\r
374 ( pxMutex->uxRecursiveCallCount )++;
\r
381 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
382 /*-----------------------------------------------------------*/
\r
384 #if configUSE_COUNTING_SEMAPHORES == 1
\r
386 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
388 xQueueHandle pxHandle;
\r
390 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
392 if( pxHandle != NULL )
\r
394 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
396 traceCREATE_COUNTING_SEMAPHORE();
\r
400 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
406 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
407 /*-----------------------------------------------------------*/
\r
409 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
411 signed portBASE_TYPE xReturn = pdTRUE;
\r
412 xTimeOutType xTimeOut;
\r
416 /* If xTicksToWait is zero then we are not going to block even
\r
417 if there is no room in the queue to post. */
\r
418 if( xTicksToWait > ( portTickType ) 0 )
\r
421 prvLockQueue( pxQueue );
\r
423 if( xReturn == pdTRUE )
\r
425 /* This is the first time through - we need to capture the
\r
426 time while the scheduler is locked to ensure we attempt to
\r
427 block at least once. */
\r
428 vTaskSetTimeOutState( &xTimeOut );
\r
431 if( prvIsQueueFull( pxQueue ) )
\r
433 /* Need to call xTaskCheckForTimeout again as time could
\r
434 have passed since it was last called if this is not the
\r
435 first time around this loop. */
\r
436 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
438 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
439 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
441 /* Unlocking the queue means queue events can effect the
\r
442 event list. It is possible that interrupts occurring now
\r
443 remove this task from the event list again - but as the
\r
444 scheduler is suspended the task will go onto the pending
\r
445 ready last instead of the actual ready list. */
\r
446 prvUnlockQueue( pxQueue );
\r
448 /* Resuming the scheduler will move tasks from the pending
\r
449 ready list into the ready list - so it is feasible that this
\r
450 task is already in a ready list before it yields - in which
\r
451 case the yield will not cause a context switch unless there
\r
452 is also a higher priority task in the pending ready list. */
\r
453 if( !xTaskResumeAll() )
\r
460 prvUnlockQueue( pxQueue );
\r
461 ( void ) xTaskResumeAll();
\r
466 /* The queue was not full so we can just unlock the
\r
467 scheduler and queue again before carrying on. */
\r
468 prvUnlockQueue( pxQueue );
\r
469 ( void ) xTaskResumeAll();
\r
473 /* Higher priority tasks and interrupts can execute during
\r
474 this time and could possible refill the queue - even if we
\r
475 unblocked because space became available. */
\r
477 taskENTER_CRITICAL();
\r
479 /* Is there room on the queue now? To be running we must be
\r
480 the highest priority task wanting to access the queue. */
\r
481 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
483 traceQUEUE_SEND( pxQueue );
\r
484 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
487 /* If there was a task waiting for data to arrive on the
\r
488 queue then unblock it now. */
\r
489 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
491 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
493 /* The unblocked task has a priority higher than
\r
494 our own so yield immediately. */
\r
501 /* Setting xReturn to errQUEUE_FULL will force its timeout
\r
502 to be re-evaluated. This is necessary in case interrupts
\r
503 and higher priority tasks accessed the queue between this
\r
504 task being unblocked and subsequently attempting to write
\r
506 xReturn = errQUEUE_FULL;
\r
509 taskEXIT_CRITICAL();
\r
511 if( xReturn == errQUEUE_FULL )
\r
513 if( xTicksToWait > ( portTickType ) 0 )
\r
515 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
517 xReturn = queueERRONEOUS_UNBLOCK;
\r
521 traceQUEUE_SEND_FAILED( pxQueue );
\r
526 traceQUEUE_SEND_FAILED( pxQueue );
\r
530 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
534 /*-----------------------------------------------------------*/
\r
536 #if configUSE_ALTERNATIVE_API == 1
\r
538 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
540 signed portBASE_TYPE xReturn = pdPASS;
\r
541 xTimeOutType xTimeOut;
\r
543 /* The source code that implements the alternative (Alt) API is
\r
544 simpler because it makes more use of critical sections. This is
\r
545 the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
546 preferred fully featured API too. The fully featured API has more
\r
547 complex code that takes longer to execute, but makes less use of
\r
548 critical sections. */
\r
552 /* If xTicksToWait is zero then we are not going to block even
\r
553 if there is no room in the queue to post. */
\r
554 if( xTicksToWait > ( portTickType ) 0 )
\r
556 portENTER_CRITICAL();
\r
558 if( xReturn == pdPASS )
\r
560 /* This is the first time through - capture the time
\r
561 inside the critical section to ensure we attempt to
\r
562 block at least once. */
\r
563 vTaskSetTimeOutState( &xTimeOut );
\r
566 if( prvIsQueueFull( pxQueue ) )
\r
568 /* Need to call xTaskCheckForTimeout again as time could
\r
569 have passed since it was last called if this is not the
\r
570 first time around this loop. */
\r
571 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
573 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
574 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
576 /* This will exit the critical section, then re-enter when
\r
577 the task next runs. */
\r
582 portEXIT_CRITICAL();
\r
585 /* Higher priority tasks and interrupts can execute during
\r
586 this time and could possible refill the queue - even if we
\r
587 unblocked because space became available. */
\r
589 taskENTER_CRITICAL();
\r
591 /* Is there room on the queue now? To be running we must be
\r
592 the highest priority task wanting to access the queue. */
\r
593 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
595 traceQUEUE_SEND( pxQueue );
\r
596 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
599 /* If there was a task waiting for data to arrive on the
\r
600 queue then unblock it now. */
\r
601 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
603 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
605 /* The unblocked task has a priority higher than
\r
606 our own so yield immediately. */
\r
613 /* Setting xReturn to errQUEUE_FULL will force its timeout
\r
614 to be re-evaluated. This is necessary in case interrupts
\r
615 and higher priority tasks accessed the queue between this
\r
616 task being unblocked and subsequently attempting to write
\r
618 xReturn = errQUEUE_FULL;
\r
621 taskEXIT_CRITICAL();
\r
623 if( xReturn == errQUEUE_FULL )
\r
625 if( xTicksToWait > ( portTickType ) 0 )
\r
627 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
629 xReturn = queueERRONEOUS_UNBLOCK;
\r
633 traceQUEUE_SEND_FAILED( pxQueue );
\r
638 traceQUEUE_SEND_FAILED( pxQueue );
\r
642 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
647 #endif /* configUSE_ALTERNATIVE_API */
\r
648 /*-----------------------------------------------------------*/
\r
650 #if configUSE_ALTERNATIVE_API == 1
\r
652 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
654 signed portBASE_TYPE xReturn = pdTRUE;
\r
655 xTimeOutType xTimeOut;
\r
656 signed portCHAR *pcOriginalReadPosition;
\r
658 /* The source code that implements the alternative (Alt) API is
\r
659 simpler because it makes more use of critical sections. This is
\r
660 the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
661 preferred fully featured API too. The fully featured API has more
\r
662 complex code that takes longer to execute, but makes less use of
\r
663 critical sections. */
\r
667 /* If there are no messages in the queue we may have to block. */
\r
668 if( xTicksToWait > ( portTickType ) 0 )
\r
670 portENTER_CRITICAL();
\r
672 if( xReturn == pdPASS )
\r
674 /* This is the first time through - capture the time
\r
675 inside the critical section to ensure we attempt to
\r
676 block at least once. */
\r
677 vTaskSetTimeOutState( &xTimeOut );
\r
680 if( prvIsQueueEmpty( pxQueue ) )
\r
682 /* Need to call xTaskCheckForTimeout again as time could
\r
683 have passed since it was last called if this is not the
\r
684 first time around this loop. */
\r
685 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
687 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
689 #if ( configUSE_MUTEXES == 1 )
\r
691 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
693 portENTER_CRITICAL();
\r
694 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
695 portEXIT_CRITICAL();
\r
700 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
705 portEXIT_CRITICAL();
\r
708 taskENTER_CRITICAL();
\r
710 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
712 /* Remember our read position in case we are just peeking. */
\r
713 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
715 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
717 if( xJustPeeking == pdFALSE )
\r
719 traceQUEUE_RECEIVE( pxQueue );
\r
721 /* We are actually removing data. */
\r
722 --( pxQueue->uxMessagesWaiting );
\r
724 #if ( configUSE_MUTEXES == 1 )
\r
726 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
728 /* Record the information required to implement
\r
729 priority inheritance should it become necessary. */
\r
730 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
735 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
737 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
745 traceQUEUE_PEEK( pxQueue );
\r
747 /* We are not removing the data, so reset our read
\r
749 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
751 /* The data is being left in the queue, so see if there are
\r
752 any other tasks waiting for the data. */
\r
753 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
755 /* Tasks that are removed from the event list will get added to
\r
756 the pending ready list as the scheduler is still suspended. */
\r
757 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
759 /* The task waiting has a higher priority that this task. */
\r
770 xReturn = errQUEUE_EMPTY;
\r
773 taskEXIT_CRITICAL();
\r
775 if( xReturn == errQUEUE_EMPTY )
\r
777 if( xTicksToWait > ( portTickType ) 0 )
\r
779 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
781 xReturn = queueERRONEOUS_UNBLOCK;
\r
785 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
790 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
793 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
799 #endif /* configUSE_ALTERNATIVE_API */
\r
800 /*-----------------------------------------------------------*/
\r
802 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
804 signed portBASE_TYPE xReturn;
\r
806 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
807 in the queue. Also we don't directly wake a task that was blocked on a
\r
808 queue read, instead we return a flag to say whether a context switch is
\r
809 required or not (i.e. has a task with a higher priority than us been woken
\r
811 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
813 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
815 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
817 /* If the queue is locked we do not alter the event list. This will
\r
818 be done when the queue is unlocked later. */
\r
819 if( pxQueue->xTxLock == queueUNLOCKED )
\r
821 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
823 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
825 /* The task waiting has a higher priority so record that a
\r
826 context switch is required. */
\r
827 *pxHigherPriorityTaskWoken = pdTRUE;
\r
833 /* Increment the lock count so the task that unlocks the queue
\r
834 knows that data was posted while it was locked. */
\r
835 ++( pxQueue->xTxLock );
\r
842 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
843 xReturn = errQUEUE_FULL;
\r
848 /*-----------------------------------------------------------*/
\r
850 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
852 signed portBASE_TYPE xReturn = pdTRUE;
\r
853 xTimeOutType xTimeOut;
\r
854 signed portCHAR *pcOriginalReadPosition;
\r
858 /* If there are no messages in the queue we may have to block. */
\r
859 if( xTicksToWait > ( portTickType ) 0 )
\r
862 prvLockQueue( pxQueue );
\r
864 if( xReturn == pdTRUE )
\r
866 /* This is the first time through - we need to capture the
\r
867 time while the scheduler is locked to ensure we attempt to
\r
868 block at least once. */
\r
869 vTaskSetTimeOutState( &xTimeOut );
\r
872 if( prvIsQueueEmpty( pxQueue ) )
\r
874 /* Need to call xTaskCheckForTimeout again as time could
\r
875 have passed since it was last called if this is not the
\r
876 first time around this loop. */
\r
877 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
879 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
881 #if ( configUSE_MUTEXES == 1 )
\r
883 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
885 portENTER_CRITICAL();
\r
886 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
887 portEXIT_CRITICAL();
\r
892 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
893 prvUnlockQueue( pxQueue );
\r
894 if( !xTaskResumeAll() )
\r
901 prvUnlockQueue( pxQueue );
\r
902 ( void ) xTaskResumeAll();
\r
907 prvUnlockQueue( pxQueue );
\r
908 ( void ) xTaskResumeAll();
\r
912 taskENTER_CRITICAL();
\r
914 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
916 /* Remember our read position in case we are just peeking. */
\r
917 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
919 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
921 if( xJustPeeking == pdFALSE )
\r
923 traceQUEUE_RECEIVE( pxQueue );
\r
925 /* We are actually removing data. */
\r
926 --( pxQueue->uxMessagesWaiting );
\r
928 #if ( configUSE_MUTEXES == 1 )
\r
930 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
932 /* Record the information required to implement
\r
933 priority inheritance should it become necessary. */
\r
934 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
939 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
941 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
949 traceQUEUE_PEEK( pxQueue );
\r
951 /* We are not removing the data, so reset our read
\r
953 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
955 /* The data is being left in the queue, so see if there are
\r
956 any other tasks waiting for the data. */
\r
957 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
959 /* Tasks that are removed from the event list will get added to
\r
960 the pending ready list as the scheduler is still suspended. */
\r
961 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
963 /* The task waiting has a higher priority that this task. */
\r
974 xReturn = errQUEUE_EMPTY;
\r
977 taskEXIT_CRITICAL();
\r
979 if( xReturn == errQUEUE_EMPTY )
\r
981 if( xTicksToWait > ( portTickType ) 0 )
\r
983 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
985 xReturn = queueERRONEOUS_UNBLOCK;
\r
989 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
994 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
997 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
1001 /*-----------------------------------------------------------*/
\r
1003 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1005 signed portBASE_TYPE xReturn;
\r
1007 /* We cannot block from an ISR, so check there is data available. */
\r
1008 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1010 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1012 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1013 --( pxQueue->uxMessagesWaiting );
\r
1015 /* If the queue is locked we will not modify the event list. Instead
\r
1016 we update the lock count so the task that unlocks the queue will know
\r
1017 that an ISR has removed data while the queue was locked. */
\r
1018 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1020 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1022 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1024 /* The task waiting has a higher priority than us so
\r
1025 force a context switch. */
\r
1026 *pxTaskWoken = pdTRUE;
\r
1032 /* Increment the lock count so the task that unlocks the queue
\r
1033 knows that data was removed while it was locked. */
\r
1034 ++( pxQueue->xRxLock );
\r
1042 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1047 /*-----------------------------------------------------------*/
\r
1049 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1051 unsigned portBASE_TYPE uxReturn;
\r
1053 taskENTER_CRITICAL();
\r
1054 uxReturn = pxQueue->uxMessagesWaiting;
\r
1055 taskEXIT_CRITICAL();
\r
1059 /*-----------------------------------------------------------*/
\r
1061 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1063 unsigned portBASE_TYPE uxReturn;
\r
1065 uxReturn = pxQueue->uxMessagesWaiting;
\r
1069 /*-----------------------------------------------------------*/
\r
1071 void vQueueDelete( xQueueHandle pxQueue )
\r
1073 traceQUEUE_DELETE( pxQueue );
\r
1075 vPortFree( pxQueue->pcHead );
\r
1076 vPortFree( pxQueue );
\r
1078 /*-----------------------------------------------------------*/
\r
1080 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1082 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1084 #if ( configUSE_MUTEXES == 1 )
\r
1086 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1088 /* The mutex is no longer being held. */
\r
1089 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1090 pxQueue->pxMutexHolder = NULL;
\r
1095 else if( xPosition == queueSEND_TO_BACK )
\r
1097 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1098 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1099 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1101 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1106 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1107 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1108 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1110 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1114 ++( pxQueue->uxMessagesWaiting );
\r
1116 /*-----------------------------------------------------------*/
\r
1118 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1120 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1122 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1123 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1125 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1127 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1130 /*-----------------------------------------------------------*/
\r
1132 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1134 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1136 /* The lock counts contains the number of extra data items placed or
\r
1137 removed from the queue while the queue was locked. When a queue is
\r
1138 locked items can be added or removed, but the event lists cannot be
\r
1140 taskENTER_CRITICAL();
\r
1142 /* See if data was added to the queue while it was locked. */
\r
1143 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1145 /* Data was posted while the queue was locked. Are any tasks
\r
1146 blocked waiting for data to become available? */
\r
1147 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1149 /* Tasks that are removed from the event list will get added to
\r
1150 the pending ready list as the scheduler is still suspended. */
\r
1151 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1153 /* The task waiting has a higher priority so record that a
\r
1154 context switch is required. */
\r
1155 vTaskMissedYield();
\r
1158 --( pxQueue->xTxLock );
\r
1166 pxQueue->xTxLock = queueUNLOCKED;
\r
1168 taskEXIT_CRITICAL();
\r
1170 /* Do the same for the Rx lock. */
\r
1171 taskENTER_CRITICAL();
\r
1173 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1175 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1177 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1179 vTaskMissedYield();
\r
1182 --( pxQueue->xRxLock );
\r
1190 pxQueue->xRxLock = queueUNLOCKED;
\r
1192 taskEXIT_CRITICAL();
\r
1194 /*-----------------------------------------------------------*/
\r
1196 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1198 signed portBASE_TYPE xReturn;
\r
1200 taskENTER_CRITICAL();
\r
1201 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1202 taskEXIT_CRITICAL();
\r
1206 /*-----------------------------------------------------------*/
\r
1208 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1210 signed portBASE_TYPE xReturn;
\r
1212 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1216 /*-----------------------------------------------------------*/
\r
1218 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1220 signed portBASE_TYPE xReturn;
\r
1222 taskENTER_CRITICAL();
\r
1223 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1224 taskEXIT_CRITICAL();
\r
1228 /*-----------------------------------------------------------*/
\r
1230 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1232 signed portBASE_TYPE xReturn;
\r
1234 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1238 /*-----------------------------------------------------------*/
\r
1240 #if configUSE_CO_ROUTINES == 1
\r
1241 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1243 signed portBASE_TYPE xReturn;
\r
1245 /* If the queue is already full we may have to block. A critical section
\r
1246 is required to prevent an interrupt removing something from the queue
\r
1247 between the check to see if the queue is full and blocking on the queue. */
\r
1248 portDISABLE_INTERRUPTS();
\r
1250 if( prvIsQueueFull( pxQueue ) )
\r
1252 /* The queue is full - do we want to block or just leave without
\r
1254 if( xTicksToWait > ( portTickType ) 0 )
\r
1256 /* As this is called from a coroutine we cannot block directly, but
\r
1257 return indicating that we need to block. */
\r
1258 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1259 portENABLE_INTERRUPTS();
\r
1260 return errQUEUE_BLOCKED;
\r
1264 portENABLE_INTERRUPTS();
\r
1265 return errQUEUE_FULL;
\r
1269 portENABLE_INTERRUPTS();
\r
1273 portDISABLE_INTERRUPTS();
\r
1275 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1277 /* There is room in the queue, copy the data into the queue. */
\r
1278 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1281 /* Were any co-routines waiting for data to become available? */
\r
1282 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1284 /* In this instance the co-routine could be placed directly
\r
1285 into the ready list as we are within a critical section.
\r
1286 Instead the same pending ready list mechanism is used as if
\r
1287 the event were caused from within an interrupt. */
\r
1288 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1290 /* The co-routine waiting has a higher priority so record
\r
1291 that a yield might be appropriate. */
\r
1292 xReturn = errQUEUE_YIELD;
\r
1298 xReturn = errQUEUE_FULL;
\r
1301 portENABLE_INTERRUPTS();
\r
1306 /*-----------------------------------------------------------*/
\r
1308 #if configUSE_CO_ROUTINES == 1
\r
1309 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1311 signed portBASE_TYPE xReturn;
\r
1313 /* If the queue is already empty we may have to block. A critical section
\r
1314 is required to prevent an interrupt adding something to the queue
\r
1315 between the check to see if the queue is empty and blocking on the queue. */
\r
1316 portDISABLE_INTERRUPTS();
\r
1318 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1320 /* There are no messages in the queue, do we want to block or just
\r
1321 leave with nothing? */
\r
1322 if( xTicksToWait > ( portTickType ) 0 )
\r
1324 /* As this is a co-routine we cannot block directly, but return
\r
1325 indicating that we need to block. */
\r
1326 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1327 portENABLE_INTERRUPTS();
\r
1328 return errQUEUE_BLOCKED;
\r
1332 portENABLE_INTERRUPTS();
\r
1333 return errQUEUE_FULL;
\r
1337 portENABLE_INTERRUPTS();
\r
1341 portDISABLE_INTERRUPTS();
\r
1343 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1345 /* Data is available from the queue. */
\r
1346 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1347 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1349 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1351 --( pxQueue->uxMessagesWaiting );
\r
1352 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1356 /* Were any co-routines waiting for space to become available? */
\r
1357 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1359 /* In this instance the co-routine could be placed directly
\r
1360 into the ready list as we are within a critical section.
\r
1361 Instead the same pending ready list mechanism is used as if
\r
1362 the event were caused from within an interrupt. */
\r
1363 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1365 xReturn = errQUEUE_YIELD;
\r
1374 portENABLE_INTERRUPTS();
\r
1379 /*-----------------------------------------------------------*/
\r
1383 #if configUSE_CO_ROUTINES == 1
\r
1384 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1386 /* Cannot block within an ISR so if there is no space on the queue then
\r
1387 exit without doing anything. */
\r
1388 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1390 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1392 /* We only want to wake one co-routine per ISR, so check that a
\r
1393 co-routine has not already been woken. */
\r
1394 if( !xCoRoutinePreviouslyWoken )
\r
1396 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1398 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1406 return xCoRoutinePreviouslyWoken;
\r
1409 /*-----------------------------------------------------------*/
\r
1411 #if configUSE_CO_ROUTINES == 1
\r
1412 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1414 signed portBASE_TYPE xReturn;
\r
1416 /* We cannot block from an ISR, so check there is data available. If
\r
1417 not then just leave without doing anything. */
\r
1418 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1420 /* Copy the data from the queue. */
\r
1421 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1422 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1424 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1426 --( pxQueue->uxMessagesWaiting );
\r
1427 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1429 if( !( *pxCoRoutineWoken ) )
\r
1431 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1433 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1435 *pxCoRoutineWoken = pdTRUE;
\r
1450 /*-----------------------------------------------------------*/
\r