2 FreeRTOS.org V5.0.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, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
123 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, 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, 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, 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 portSET_INTERRUPT_MASK_FROM_ISR();
\r
813 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
815 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
817 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
819 /* If the queue is locked we do not alter the event list. This will
\r
820 be done when the queue is unlocked later. */
\r
821 if( pxQueue->xTxLock == queueUNLOCKED )
\r
823 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
825 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
827 /* The task waiting has a higher priority so record that a
\r
828 context switch is required. */
\r
829 *pxHigherPriorityTaskWoken = pdTRUE;
\r
835 /* Increment the lock count so the task that unlocks the queue
\r
836 knows that data was posted while it was locked. */
\r
837 ++( pxQueue->xTxLock );
\r
844 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
845 xReturn = errQUEUE_FULL;
\r
848 portCLEAR_INTERRUPT_MASK_FROM_ISR();
\r
852 /*-----------------------------------------------------------*/
\r
854 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
856 signed portBASE_TYPE xReturn = pdTRUE;
\r
857 xTimeOutType xTimeOut;
\r
858 signed portCHAR *pcOriginalReadPosition;
\r
862 /* If there are no messages in the queue we may have to block. */
\r
863 if( xTicksToWait > ( portTickType ) 0 )
\r
866 prvLockQueue( pxQueue );
\r
868 if( xReturn == pdTRUE )
\r
870 /* This is the first time through - we need to capture the
\r
871 time while the scheduler is locked to ensure we attempt to
\r
872 block at least once. */
\r
873 vTaskSetTimeOutState( &xTimeOut );
\r
876 if( prvIsQueueEmpty( pxQueue ) )
\r
878 /* Need to call xTaskCheckForTimeout again as time could
\r
879 have passed since it was last called if this is not the
\r
880 first time around this loop. */
\r
881 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
883 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
885 #if ( configUSE_MUTEXES == 1 )
\r
887 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
889 portENTER_CRITICAL();
\r
890 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
891 portEXIT_CRITICAL();
\r
896 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
897 prvUnlockQueue( pxQueue );
\r
898 if( !xTaskResumeAll() )
\r
905 prvUnlockQueue( pxQueue );
\r
906 ( void ) xTaskResumeAll();
\r
911 prvUnlockQueue( pxQueue );
\r
912 ( void ) xTaskResumeAll();
\r
916 taskENTER_CRITICAL();
\r
918 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
920 /* Remember our read position in case we are just peeking. */
\r
921 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
923 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
925 if( xJustPeeking == pdFALSE )
\r
927 traceQUEUE_RECEIVE( pxQueue );
\r
929 /* We are actually removing data. */
\r
930 --( pxQueue->uxMessagesWaiting );
\r
932 #if ( configUSE_MUTEXES == 1 )
\r
934 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
936 /* Record the information required to implement
\r
937 priority inheritance should it become necessary. */
\r
938 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
943 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
945 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
953 traceQUEUE_PEEK( pxQueue );
\r
955 /* We are not removing the data, so reset our read
\r
957 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
959 /* The data is being left in the queue, so see if there are
\r
960 any other tasks waiting for the data. */
\r
961 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
963 /* Tasks that are removed from the event list will get added to
\r
964 the pending ready list as the scheduler is still suspended. */
\r
965 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
967 /* The task waiting has a higher priority that this task. */
\r
978 xReturn = errQUEUE_EMPTY;
\r
981 taskEXIT_CRITICAL();
\r
983 if( xReturn == errQUEUE_EMPTY )
\r
985 if( xTicksToWait > ( portTickType ) 0 )
\r
987 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
989 xReturn = queueERRONEOUS_UNBLOCK;
\r
993 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
998 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1001 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
1005 /*-----------------------------------------------------------*/
\r
1007 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1009 signed portBASE_TYPE xReturn;
\r
1011 portSET_INTERRUPT_MASK_FROM_ISR();
\r
1013 /* We cannot block from an ISR, so check there is data available. */
\r
1014 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1016 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1018 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1019 --( pxQueue->uxMessagesWaiting );
\r
1021 /* If the queue is locked we will not modify the event list. Instead
\r
1022 we update the lock count so the task that unlocks the queue will know
\r
1023 that an ISR has removed data while the queue was locked. */
\r
1024 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1026 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1028 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1030 /* The task waiting has a higher priority than us so
\r
1031 force a context switch. */
\r
1032 *pxTaskWoken = pdTRUE;
\r
1038 /* Increment the lock count so the task that unlocks the queue
\r
1039 knows that data was removed while it was locked. */
\r
1040 ++( pxQueue->xRxLock );
\r
1048 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1051 portCLEAR_INTERRUPT_MASK_FROM_ISR();
\r
1055 /*-----------------------------------------------------------*/
\r
1057 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1059 unsigned portBASE_TYPE uxReturn;
\r
1061 taskENTER_CRITICAL();
\r
1062 uxReturn = pxQueue->uxMessagesWaiting;
\r
1063 taskEXIT_CRITICAL();
\r
1067 /*-----------------------------------------------------------*/
\r
1069 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1071 unsigned portBASE_TYPE uxReturn;
\r
1073 uxReturn = pxQueue->uxMessagesWaiting;
\r
1077 /*-----------------------------------------------------------*/
\r
1079 void vQueueDelete( xQueueHandle pxQueue )
\r
1081 traceQUEUE_DELETE( pxQueue );
\r
1083 vPortFree( pxQueue->pcHead );
\r
1084 vPortFree( pxQueue );
\r
1086 /*-----------------------------------------------------------*/
\r
1088 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1090 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1092 #if ( configUSE_MUTEXES == 1 )
\r
1094 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1096 /* The mutex is no longer being held. */
\r
1097 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1098 pxQueue->pxMutexHolder = NULL;
\r
1103 else if( xPosition == queueSEND_TO_BACK )
\r
1105 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1106 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1107 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1109 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1114 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1115 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1116 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1118 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1122 ++( pxQueue->uxMessagesWaiting );
\r
1124 /*-----------------------------------------------------------*/
\r
1126 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1128 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1130 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1131 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1133 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1135 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1138 /*-----------------------------------------------------------*/
\r
1140 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1142 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1144 /* The lock counts contains the number of extra data items placed or
\r
1145 removed from the queue while the queue was locked. When a queue is
\r
1146 locked items can be added or removed, but the event lists cannot be
\r
1148 taskENTER_CRITICAL();
\r
1150 /* See if data was added to the queue while it was locked. */
\r
1151 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1153 /* Data was posted while the queue was locked. Are any tasks
\r
1154 blocked waiting for data to become available? */
\r
1155 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1157 /* Tasks that are removed from the event list will get added to
\r
1158 the pending ready list as the scheduler is still suspended. */
\r
1159 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1161 /* The task waiting has a higher priority so record that a
\r
1162 context switch is required. */
\r
1163 vTaskMissedYield();
\r
1166 --( pxQueue->xTxLock );
\r
1174 pxQueue->xTxLock = queueUNLOCKED;
\r
1176 taskEXIT_CRITICAL();
\r
1178 /* Do the same for the Rx lock. */
\r
1179 taskENTER_CRITICAL();
\r
1181 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1183 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1185 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1187 vTaskMissedYield();
\r
1190 --( pxQueue->xRxLock );
\r
1198 pxQueue->xRxLock = queueUNLOCKED;
\r
1200 taskEXIT_CRITICAL();
\r
1202 /*-----------------------------------------------------------*/
\r
1204 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1206 signed portBASE_TYPE xReturn;
\r
1208 taskENTER_CRITICAL();
\r
1209 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1210 taskEXIT_CRITICAL();
\r
1214 /*-----------------------------------------------------------*/
\r
1216 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1218 signed portBASE_TYPE xReturn;
\r
1220 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1224 /*-----------------------------------------------------------*/
\r
1226 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1228 signed portBASE_TYPE xReturn;
\r
1230 taskENTER_CRITICAL();
\r
1231 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1232 taskEXIT_CRITICAL();
\r
1236 /*-----------------------------------------------------------*/
\r
1238 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1240 signed portBASE_TYPE xReturn;
\r
1242 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1246 /*-----------------------------------------------------------*/
\r
1248 #if configUSE_CO_ROUTINES == 1
\r
1249 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1251 signed portBASE_TYPE xReturn;
\r
1253 /* If the queue is already full we may have to block. A critical section
\r
1254 is required to prevent an interrupt removing something from the queue
\r
1255 between the check to see if the queue is full and blocking on the queue. */
\r
1256 portDISABLE_INTERRUPTS();
\r
1258 if( prvIsQueueFull( pxQueue ) )
\r
1260 /* The queue is full - do we want to block or just leave without
\r
1262 if( xTicksToWait > ( portTickType ) 0 )
\r
1264 /* As this is called from a coroutine we cannot block directly, but
\r
1265 return indicating that we need to block. */
\r
1266 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1267 portENABLE_INTERRUPTS();
\r
1268 return errQUEUE_BLOCKED;
\r
1272 portENABLE_INTERRUPTS();
\r
1273 return errQUEUE_FULL;
\r
1277 portENABLE_INTERRUPTS();
\r
1281 portDISABLE_INTERRUPTS();
\r
1283 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1285 /* There is room in the queue, copy the data into the queue. */
\r
1286 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1289 /* Were any co-routines waiting for data to become available? */
\r
1290 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1292 /* In this instance the co-routine could be placed directly
\r
1293 into the ready list as we are within a critical section.
\r
1294 Instead the same pending ready list mechanism is used as if
\r
1295 the event were caused from within an interrupt. */
\r
1296 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1298 /* The co-routine waiting has a higher priority so record
\r
1299 that a yield might be appropriate. */
\r
1300 xReturn = errQUEUE_YIELD;
\r
1306 xReturn = errQUEUE_FULL;
\r
1309 portENABLE_INTERRUPTS();
\r
1314 /*-----------------------------------------------------------*/
\r
1316 #if configUSE_CO_ROUTINES == 1
\r
1317 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1319 signed portBASE_TYPE xReturn;
\r
1321 /* If the queue is already empty we may have to block. A critical section
\r
1322 is required to prevent an interrupt adding something to the queue
\r
1323 between the check to see if the queue is empty and blocking on the queue. */
\r
1324 portDISABLE_INTERRUPTS();
\r
1326 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1328 /* There are no messages in the queue, do we want to block or just
\r
1329 leave with nothing? */
\r
1330 if( xTicksToWait > ( portTickType ) 0 )
\r
1332 /* As this is a co-routine we cannot block directly, but return
\r
1333 indicating that we need to block. */
\r
1334 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1335 portENABLE_INTERRUPTS();
\r
1336 return errQUEUE_BLOCKED;
\r
1340 portENABLE_INTERRUPTS();
\r
1341 return errQUEUE_FULL;
\r
1345 portENABLE_INTERRUPTS();
\r
1349 portDISABLE_INTERRUPTS();
\r
1351 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1353 /* Data is available from the queue. */
\r
1354 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1355 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1357 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1359 --( pxQueue->uxMessagesWaiting );
\r
1360 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1364 /* Were any co-routines waiting for space to become available? */
\r
1365 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1367 /* In this instance the co-routine could be placed directly
\r
1368 into the ready list as we are within a critical section.
\r
1369 Instead the same pending ready list mechanism is used as if
\r
1370 the event were caused from within an interrupt. */
\r
1371 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1373 xReturn = errQUEUE_YIELD;
\r
1382 portENABLE_INTERRUPTS();
\r
1387 /*-----------------------------------------------------------*/
\r
1391 #if configUSE_CO_ROUTINES == 1
\r
1392 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1394 /* Cannot block within an ISR so if there is no space on the queue then
\r
1395 exit without doing anything. */
\r
1396 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1398 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1400 /* We only want to wake one co-routine per ISR, so check that a
\r
1401 co-routine has not already been woken. */
\r
1402 if( !xCoRoutinePreviouslyWoken )
\r
1404 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1406 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1414 return xCoRoutinePreviouslyWoken;
\r
1417 /*-----------------------------------------------------------*/
\r
1419 #if configUSE_CO_ROUTINES == 1
\r
1420 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1422 signed portBASE_TYPE xReturn;
\r
1424 /* We cannot block from an ISR, so check there is data available. If
\r
1425 not then just leave without doing anything. */
\r
1426 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1428 /* Copy the data from the queue. */
\r
1429 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1430 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1432 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1434 --( pxQueue->uxMessagesWaiting );
\r
1435 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1437 if( !( *pxCoRoutineWoken ) )
\r
1439 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1441 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1443 *pxCoRoutineWoken = pdTRUE;
\r
1458 /*-----------------------------------------------------------*/
\r