2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 #include "FreeRTOS.h"
\r
56 #include "croutine.h"
\r
58 /*-----------------------------------------------------------
\r
59 * PUBLIC LIST API documented in list.h
\r
60 *----------------------------------------------------------*/
\r
62 /* Constants used with the cRxLock and cTxLock structure members. */
\r
63 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
64 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
66 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
68 /* For internal use only. */
\r
69 #define queueSEND_TO_BACK ( 0 )
\r
70 #define queueSEND_TO_FRONT ( 1 )
\r
72 /* Effectively make a union out of the xQUEUE structure. */
\r
73 #define pxMutexHolder pcTail
\r
74 #define uxQueueType pcHead
\r
75 #define uxRecursiveCallCount pcReadFrom
\r
76 #define queueQUEUE_IS_MUTEX NULL
\r
78 /* Semaphores do not actually store or copy data, so have an items size of
\r
80 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
81 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
82 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
85 * Definition of the queue used by the scheduler.
\r
86 * Items are queued by copy, not reference.
\r
88 typedef struct QueueDefinition
\r
90 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
91 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
93 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
94 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
96 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
97 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
99 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
100 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
101 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
103 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
104 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
107 /*-----------------------------------------------------------*/
\r
110 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
111 * To keep the definition private the API header file defines it as a
\r
114 typedef xQUEUE * xQueueHandle;
\r
117 * Prototypes for public functions are included here so we don't have to
\r
118 * include the API header file (as it defines xQueueHandle differently). These
\r
119 * functions are documented in the API header file.
\r
121 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
122 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
123 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue );
\r
124 void vQueueDelete( xQueueHandle xQueue );
\r
125 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition );
\r
126 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
127 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
128 xQueueHandle xQueueCreateMutex( void );
\r
129 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );
\r
130 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime );
\r
131 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex );
\r
132 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
133 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
134 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue );
\r
135 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue );
\r
136 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue );
\r
139 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
140 * an optional component.
\r
142 #if configUSE_CO_ROUTINES == 1
\r
143 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
144 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
145 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
146 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
150 * The queue registry is just a means for kernel aware debuggers to locate
\r
151 * queue structures. It has no other purpose so is an optional component.
\r
153 #if configQUEUE_REGISTRY_SIZE > 0
\r
155 /* The type stored within the queue registry array. This allows a name
\r
156 to be assigned to each queue making kernel aware debugging a little
\r
157 more user friendly. */
\r
158 typedef struct QUEUE_REGISTRY_ITEM
\r
160 signed portCHAR *pcQueueName;
\r
161 xQueueHandle xHandle;
\r
162 } xQueueRegistryItem;
\r
164 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
165 The pcQueueName member of a structure being NULL is indicative of the
\r
166 array position being vacant. */
\r
167 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
169 /* Removes a queue from the registry by simply setting the pcQueueName
\r
171 static void vQueueUnregisterQueue( xQueueHandle xQueue );
\r
172 void vQueueAddToRegistry( xQueueHandle xQueue, signed portCHAR *pcQueueName );
\r
176 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
177 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
178 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
179 * queue is locked it will instead increment the appropriate queue lock count
\r
180 * to indicate that a task may require unblocking. When the queue in unlocked
\r
181 * these lock counts are inspected, and the appropriate action taken.
\r
183 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
186 * Uses a critical section to determine if there is any data in a queue.
\r
188 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
190 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
193 * Uses a critical section to determine if there is any space in a queue.
\r
195 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
197 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
200 * Copies an item into the queue, either at the front of the queue or the
\r
201 * back of the queue.
\r
203 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
206 * Copies an item out of a queue.
\r
208 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
209 /*-----------------------------------------------------------*/
\r
212 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
213 * accessing the queue event lists.
\r
215 #define prvLockQueue( pxQueue ) \
\r
217 taskENTER_CRITICAL(); \
\r
219 if( pxQueue->xRxLock == queueUNLOCKED ) \
\r
221 pxQueue->xRxLock = queueLOCKED_UNMODIFIED; \
\r
223 if( pxQueue->xTxLock == queueUNLOCKED ) \
\r
225 pxQueue->xTxLock = queueLOCKED_UNMODIFIED; \
\r
228 taskEXIT_CRITICAL(); \
\r
230 /*-----------------------------------------------------------*/
\r
233 /*-----------------------------------------------------------
\r
234 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
235 *----------------------------------------------------------*/
\r
237 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
239 xQUEUE *pxNewQueue;
\r
240 size_t xQueueSizeInBytes;
\r
242 /* Allocate the new queue structure. */
\r
243 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
245 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
246 if( pxNewQueue != NULL )
\r
248 /* Create the list of pointers to queue items. The queue is one byte
\r
249 longer than asked for to make wrap checking easier/faster. */
\r
250 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
252 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
253 if( pxNewQueue->pcHead != NULL )
\r
255 /* Initialise the queue members as described above where the
\r
256 queue type is defined. */
\r
257 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
258 pxNewQueue->uxMessagesWaiting = 0;
\r
259 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
260 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
261 pxNewQueue->uxLength = uxQueueLength;
\r
262 pxNewQueue->uxItemSize = uxItemSize;
\r
263 pxNewQueue->xRxLock = queueUNLOCKED;
\r
264 pxNewQueue->xTxLock = queueUNLOCKED;
\r
266 /* Likewise ensure the event queues start with the correct state. */
\r
267 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
268 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
270 traceQUEUE_CREATE( pxNewQueue );
\r
276 traceQUEUE_CREATE_FAILED();
\r
277 vPortFree( pxNewQueue );
\r
282 /* Will only reach here if we could not allocate enough memory or no memory
\r
286 /*-----------------------------------------------------------*/
\r
288 #if ( configUSE_MUTEXES == 1 )
\r
290 xQueueHandle xQueueCreateMutex( void )
\r
292 xQUEUE *pxNewQueue;
\r
294 /* Allocate the new queue structure. */
\r
295 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
296 if( pxNewQueue != NULL )
\r
298 /* Information required for priority inheritance. */
\r
299 pxNewQueue->pxMutexHolder = NULL;
\r
300 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
302 /* Queues used as a mutex no data is actually copied into or out
\r
304 pxNewQueue->pcWriteTo = NULL;
\r
305 pxNewQueue->pcReadFrom = NULL;
\r
307 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
308 an item size of 0 as nothing is actually copied into or out
\r
310 pxNewQueue->uxMessagesWaiting = 0;
\r
311 pxNewQueue->uxLength = 1;
\r
312 pxNewQueue->uxItemSize = 0;
\r
313 pxNewQueue->xRxLock = queueUNLOCKED;
\r
314 pxNewQueue->xTxLock = queueUNLOCKED;
\r
316 /* Ensure the event queues start with the correct state. */
\r
317 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
318 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
320 /* Start with the semaphore in the expected state. */
\r
321 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
323 traceCREATE_MUTEX( pxNewQueue );
\r
327 traceCREATE_MUTEX_FAILED();
\r
333 #endif /* configUSE_MUTEXES */
\r
334 /*-----------------------------------------------------------*/
\r
336 #if configUSE_RECURSIVE_MUTEXES == 1
\r
338 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
340 portBASE_TYPE xReturn;
\r
342 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
343 change outside of this task. If this task does not hold the mutex then
\r
344 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
345 this is the only condition we are interested in it does not matter if
\r
346 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
347 mutual exclusion is required to test the pxMutexHolder variable. */
\r
348 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
350 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
352 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
353 the task handle, therefore no underflow check is required. Also,
\r
354 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
355 there can only be one, no mutual exclusion is required to modify the
\r
356 uxRecursiveCallCount member. */
\r
357 ( pxMutex->uxRecursiveCallCount )--;
\r
359 /* Have we unwound the call count? */
\r
360 if( pxMutex->uxRecursiveCallCount == 0 )
\r
362 /* Return the mutex. This will automatically unblock any other
\r
363 task that might be waiting to access the mutex. */
\r
364 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
371 /* We cannot give the mutex because we are not the holder. */
\r
374 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
380 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
381 /*-----------------------------------------------------------*/
\r
383 #if configUSE_RECURSIVE_MUTEXES == 1
\r
385 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
387 portBASE_TYPE xReturn;
\r
389 /* Comments regarding mutual exclusion as per those within
\r
390 xQueueGiveMutexRecursive(). */
\r
392 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
394 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
396 ( pxMutex->uxRecursiveCallCount )++;
\r
401 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
403 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
404 we may have blocked to reach here. */
\r
405 if( xReturn == pdPASS )
\r
407 ( pxMutex->uxRecursiveCallCount )++;
\r
414 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
415 /*-----------------------------------------------------------*/
\r
417 #if configUSE_COUNTING_SEMAPHORES == 1
\r
419 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
421 xQueueHandle pxHandle;
\r
423 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
425 if( pxHandle != NULL )
\r
427 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
429 traceCREATE_COUNTING_SEMAPHORE();
\r
433 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
439 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
440 /*-----------------------------------------------------------*/
\r
442 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
444 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
445 xTimeOutType xTimeOut;
\r
447 /* This function relaxes the coding standard somewhat to allow return
\r
448 statements within the function itself. This is done in the interest
\r
449 of execution time efficiency. */
\r
453 taskENTER_CRITICAL();
\r
455 /* Is there room on the queue now? To be running we must be
\r
456 the highest priority task wanting to access the queue. */
\r
457 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
459 traceQUEUE_SEND( pxQueue );
\r
460 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
462 /* If there was a task waiting for data to arrive on the
\r
463 queue then unblock it now. */
\r
464 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
466 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
468 /* The unblocked task has a priority higher than
\r
469 our own so yield immediately. Yes it is ok to do
\r
470 this from within the critical section - the kernel
\r
471 takes care of that. */
\r
476 taskEXIT_CRITICAL();
\r
481 if( xTicksToWait == ( portTickType ) 0 )
\r
483 /* The queue was full and no block time is specified (or
\r
484 the block time has expired) so leave now. */
\r
485 taskEXIT_CRITICAL();
\r
486 traceQUEUE_SEND_FAILED( pxQueue );
\r
487 return errQUEUE_FULL;
\r
489 else if( xEntryTimeSet == pdFALSE )
\r
491 /* The queue was full and a block time was specified so
\r
492 configure the timeout structure. */
\r
493 vTaskSetTimeOutState( &xTimeOut );
\r
494 xEntryTimeSet = pdTRUE;
\r
498 taskEXIT_CRITICAL();
\r
500 /* Interrupts and other tasks can send to and receive from the queue
\r
501 now the critical section has been exited. */
\r
504 prvLockQueue( pxQueue );
\r
506 /* Update the timeout state to see if it has expired yet. */
\r
507 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
509 if( prvIsQueueFull( pxQueue ) )
\r
511 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
512 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
514 /* Unlocking the queue means queue events can effect the
\r
515 event list. It is possible that interrupts occurring now
\r
516 remove this task from the event list again - but as the
\r
517 scheduler is suspended the task will go onto the pending
\r
518 ready last instead of the actual ready list. */
\r
519 prvUnlockQueue( pxQueue );
\r
521 /* Resuming the scheduler will move tasks from the pending
\r
522 ready list into the ready list - so it is feasible that this
\r
523 task is already in a ready list before it yields - in which
\r
524 case the yield will not cause a context switch unless there
\r
525 is also a higher priority task in the pending ready list. */
\r
526 if( !xTaskResumeAll() )
\r
534 prvUnlockQueue( pxQueue );
\r
535 ( void ) xTaskResumeAll();
\r
540 /* The timeout has expired. */
\r
541 prvUnlockQueue( pxQueue );
\r
542 ( void ) xTaskResumeAll();
\r
543 traceQUEUE_SEND_FAILED( pxQueue );
\r
544 return errQUEUE_FULL;
\r
548 /*-----------------------------------------------------------*/
\r
550 #if configUSE_ALTERNATIVE_API == 1
\r
552 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
554 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
555 xTimeOutType xTimeOut;
\r
559 taskENTER_CRITICAL();
\r
561 /* Is there room on the queue now? To be running we must be
\r
562 the highest priority task wanting to access the queue. */
\r
563 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
565 traceQUEUE_SEND( pxQueue );
\r
566 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
568 /* If there was a task waiting for data to arrive on the
\r
569 queue then unblock it now. */
\r
570 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
572 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
574 /* The unblocked task has a priority higher than
\r
575 our own so yield immediately. */
\r
580 taskEXIT_CRITICAL();
\r
585 if( xTicksToWait == ( portTickType ) 0 )
\r
587 taskEXIT_CRITICAL();
\r
588 return errQUEUE_FULL;
\r
590 else if( xEntryTimeSet == pdFALSE )
\r
592 vTaskSetTimeOutState( &xTimeOut );
\r
593 xEntryTimeSet = pdTRUE;
\r
597 taskEXIT_CRITICAL();
\r
599 taskENTER_CRITICAL();
\r
601 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
603 if( prvIsQueueFull( pxQueue ) )
\r
605 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
606 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
612 taskEXIT_CRITICAL();
\r
613 traceQUEUE_SEND_FAILED( pxQueue );
\r
614 return errQUEUE_FULL;
\r
617 taskEXIT_CRITICAL();
\r
621 #endif /* configUSE_ALTERNATIVE_API */
\r
622 /*-----------------------------------------------------------*/
\r
624 #if configUSE_ALTERNATIVE_API == 1
\r
626 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
628 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
629 xTimeOutType xTimeOut;
\r
630 signed portCHAR *pcOriginalReadPosition;
\r
634 taskENTER_CRITICAL();
\r
636 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
638 /* Remember our read position in case we are just peeking. */
\r
639 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
641 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
643 if( xJustPeeking == pdFALSE )
\r
645 traceQUEUE_RECEIVE( pxQueue );
\r
647 /* We are actually removing data. */
\r
648 --( pxQueue->uxMessagesWaiting );
\r
650 #if ( configUSE_MUTEXES == 1 )
\r
652 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
654 /* Record the information required to implement
\r
655 priority inheritance should it become necessary. */
\r
656 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
661 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
663 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
671 traceQUEUE_PEEK( pxQueue );
\r
673 /* We are not removing the data, so reset our read
\r
675 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
677 /* The data is being left in the queue, so see if there are
\r
678 any other tasks waiting for the data. */
\r
679 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
681 /* Tasks that are removed from the event list will get added to
\r
682 the pending ready list as the scheduler is still suspended. */
\r
683 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
685 /* The task waiting has a higher priority than this task. */
\r
692 taskEXIT_CRITICAL();
\r
697 if( xTicksToWait == ( portTickType ) 0 )
\r
699 taskEXIT_CRITICAL();
\r
700 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
701 return errQUEUE_EMPTY;
\r
703 else if( xEntryTimeSet == pdFALSE )
\r
705 vTaskSetTimeOutState( &xTimeOut );
\r
706 xEntryTimeSet = pdTRUE;
\r
710 taskEXIT_CRITICAL();
\r
712 taskENTER_CRITICAL();
\r
714 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
716 if( prvIsQueueEmpty( pxQueue ) )
\r
718 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
720 #if ( configUSE_MUTEXES == 1 )
\r
722 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
724 portENTER_CRITICAL();
\r
725 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
726 portEXIT_CRITICAL();
\r
731 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
737 taskEXIT_CRITICAL();
\r
738 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
739 return errQUEUE_EMPTY;
\r
742 taskEXIT_CRITICAL();
\r
747 #endif /* configUSE_ALTERNATIVE_API */
\r
748 /*-----------------------------------------------------------*/
\r
750 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
752 signed portBASE_TYPE xReturn;
\r
753 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
755 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
756 in the queue. Also we don't directly wake a task that was blocked on a
\r
757 queue read, instead we return a flag to say whether a context switch is
\r
758 required or not (i.e. has a task with a higher priority than us been woken
\r
760 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
762 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
764 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
766 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
768 /* If the queue is locked we do not alter the event list. This will
\r
769 be done when the queue is unlocked later. */
\r
770 if( pxQueue->xTxLock == queueUNLOCKED )
\r
772 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
774 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
776 /* The task waiting has a higher priority so record that a
\r
777 context switch is required. */
\r
778 *pxHigherPriorityTaskWoken = pdTRUE;
\r
784 /* Increment the lock count so the task that unlocks the queue
\r
785 knows that data was posted while it was locked. */
\r
786 ++( pxQueue->xTxLock );
\r
793 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
794 xReturn = errQUEUE_FULL;
\r
797 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
801 /*-----------------------------------------------------------*/
\r
803 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
805 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
806 xTimeOutType xTimeOut;
\r
807 signed portCHAR *pcOriginalReadPosition;
\r
809 /* This function relaxes the coding standard somewhat to allow return
\r
810 statements within the function itself. This is done in the interest
\r
811 of execution time efficiency. */
\r
815 taskENTER_CRITICAL();
\r
817 /* Is there data in the queue now? To be running we must be
\r
818 the highest priority task wanting to access the queue. */
\r
819 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
821 /* Remember our read position in case we are just peeking. */
\r
822 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
824 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
826 if( xJustPeeking == pdFALSE )
\r
828 traceQUEUE_RECEIVE( pxQueue );
\r
830 /* We are actually removing data. */
\r
831 --( pxQueue->uxMessagesWaiting );
\r
833 #if ( configUSE_MUTEXES == 1 )
\r
835 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
837 /* Record the information required to implement
\r
838 priority inheritance should it become necessary. */
\r
839 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
844 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
846 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
854 traceQUEUE_PEEK( pxQueue );
\r
856 /* We are not removing the data, so reset our read
\r
858 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
860 /* The data is being left in the queue, so see if there are
\r
861 any other tasks waiting for the data. */
\r
862 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
864 /* Tasks that are removed from the event list will get added to
\r
865 the pending ready list as the scheduler is still suspended. */
\r
866 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
868 /* The task waiting has a higher priority than this task. */
\r
875 taskEXIT_CRITICAL();
\r
880 if( xTicksToWait == ( portTickType ) 0 )
\r
882 /* The queue was empty and no block time is specified (or
\r
883 the block time has expired) so leave now. */
\r
884 taskEXIT_CRITICAL();
\r
885 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
886 return errQUEUE_EMPTY;
\r
888 else if( xEntryTimeSet == pdFALSE )
\r
890 /* The queue was empty and a block time was specified so
\r
891 configure the timeout structure. */
\r
892 vTaskSetTimeOutState( &xTimeOut );
\r
893 xEntryTimeSet = pdTRUE;
\r
897 taskEXIT_CRITICAL();
\r
899 /* Interrupts and other tasks can send to and receive from the queue
\r
900 now the critical section has been exited. */
\r
903 prvLockQueue( pxQueue );
\r
905 /* Update the timeout state to see if it has expired yet. */
\r
906 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
908 if( prvIsQueueEmpty( pxQueue ) )
\r
910 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
912 #if ( configUSE_MUTEXES == 1 )
\r
914 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
916 portENTER_CRITICAL();
\r
918 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
920 portEXIT_CRITICAL();
\r
925 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
926 prvUnlockQueue( pxQueue );
\r
927 if( !xTaskResumeAll() )
\r
935 prvUnlockQueue( pxQueue );
\r
936 ( void ) xTaskResumeAll();
\r
941 prvUnlockQueue( pxQueue );
\r
942 ( void ) xTaskResumeAll();
\r
943 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
944 return errQUEUE_EMPTY;
\r
948 /*-----------------------------------------------------------*/
\r
950 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
952 signed portBASE_TYPE xReturn;
\r
953 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
955 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
957 /* We cannot block from an ISR, so check there is data available. */
\r
958 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
960 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
962 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
963 --( pxQueue->uxMessagesWaiting );
\r
965 /* If the queue is locked we will not modify the event list. Instead
\r
966 we update the lock count so the task that unlocks the queue will know
\r
967 that an ISR has removed data while the queue was locked. */
\r
968 if( pxQueue->xRxLock == queueUNLOCKED )
\r
970 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
972 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
974 /* The task waiting has a higher priority than us so
\r
975 force a context switch. */
\r
976 *pxTaskWoken = pdTRUE;
\r
982 /* Increment the lock count so the task that unlocks the queue
\r
983 knows that data was removed while it was locked. */
\r
984 ++( pxQueue->xRxLock );
\r
992 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
995 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
999 /*-----------------------------------------------------------*/
\r
1001 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1003 unsigned portBASE_TYPE uxReturn;
\r
1005 taskENTER_CRITICAL();
\r
1006 uxReturn = pxQueue->uxMessagesWaiting;
\r
1007 taskEXIT_CRITICAL();
\r
1011 /*-----------------------------------------------------------*/
\r
1013 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1015 unsigned portBASE_TYPE uxReturn;
\r
1017 uxReturn = pxQueue->uxMessagesWaiting;
\r
1021 /*-----------------------------------------------------------*/
\r
1023 void vQueueDelete( xQueueHandle pxQueue )
\r
1025 traceQUEUE_DELETE( pxQueue );
\r
1026 vQueueUnregisterQueue( pxQueue );
\r
1027 vPortFree( pxQueue->pcHead );
\r
1028 vPortFree( pxQueue );
\r
1030 /*-----------------------------------------------------------*/
\r
1032 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1034 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1036 #if ( configUSE_MUTEXES == 1 )
\r
1038 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1040 /* The mutex is no longer being held. */
\r
1041 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1042 pxQueue->pxMutexHolder = NULL;
\r
1047 else if( xPosition == queueSEND_TO_BACK )
\r
1049 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1050 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1051 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1053 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1058 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1059 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1060 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1062 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1066 ++( pxQueue->uxMessagesWaiting );
\r
1068 /*-----------------------------------------------------------*/
\r
1070 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1072 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1074 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1075 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1077 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1079 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1082 /*-----------------------------------------------------------*/
\r
1084 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1086 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1088 /* The lock counts contains the number of extra data items placed or
\r
1089 removed from the queue while the queue was locked. When a queue is
\r
1090 locked items can be added or removed, but the event lists cannot be
\r
1092 taskENTER_CRITICAL();
\r
1094 /* See if data was added to the queue while it was locked. */
\r
1095 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1097 /* Data was posted while the queue was locked. Are any tasks
\r
1098 blocked waiting for data to become available? */
\r
1099 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1101 /* Tasks that are removed from the event list will get added to
\r
1102 the pending ready list as the scheduler is still suspended. */
\r
1103 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1105 /* The task waiting has a higher priority so record that a
\r
1106 context switch is required. */
\r
1107 vTaskMissedYield();
\r
1110 --( pxQueue->xTxLock );
\r
1118 pxQueue->xTxLock = queueUNLOCKED;
\r
1120 taskEXIT_CRITICAL();
\r
1122 /* Do the same for the Rx lock. */
\r
1123 taskENTER_CRITICAL();
\r
1125 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1127 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1129 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1131 vTaskMissedYield();
\r
1134 --( pxQueue->xRxLock );
\r
1142 pxQueue->xRxLock = queueUNLOCKED;
\r
1144 taskEXIT_CRITICAL();
\r
1146 /*-----------------------------------------------------------*/
\r
1148 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1150 signed portBASE_TYPE xReturn;
\r
1152 taskENTER_CRITICAL();
\r
1153 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1154 taskEXIT_CRITICAL();
\r
1158 /*-----------------------------------------------------------*/
\r
1160 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1162 signed portBASE_TYPE xReturn;
\r
1164 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1168 /*-----------------------------------------------------------*/
\r
1170 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1172 signed portBASE_TYPE xReturn;
\r
1174 taskENTER_CRITICAL();
\r
1175 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1176 taskEXIT_CRITICAL();
\r
1180 /*-----------------------------------------------------------*/
\r
1182 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1184 signed portBASE_TYPE xReturn;
\r
1186 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1190 /*-----------------------------------------------------------*/
\r
1192 #if configUSE_CO_ROUTINES == 1
\r
1193 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1195 signed portBASE_TYPE xReturn;
\r
1197 /* If the queue is already full we may have to block. A critical section
\r
1198 is required to prevent an interrupt removing something from the queue
\r
1199 between the check to see if the queue is full and blocking on the queue. */
\r
1200 portDISABLE_INTERRUPTS();
\r
1202 if( prvIsQueueFull( pxQueue ) )
\r
1204 /* The queue is full - do we want to block or just leave without
\r
1206 if( xTicksToWait > ( portTickType ) 0 )
\r
1208 /* As this is called from a coroutine we cannot block directly, but
\r
1209 return indicating that we need to block. */
\r
1210 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1211 portENABLE_INTERRUPTS();
\r
1212 return errQUEUE_BLOCKED;
\r
1216 portENABLE_INTERRUPTS();
\r
1217 return errQUEUE_FULL;
\r
1221 portENABLE_INTERRUPTS();
\r
1225 portDISABLE_INTERRUPTS();
\r
1227 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1229 /* There is room in the queue, copy the data into the queue. */
\r
1230 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1233 /* Were any co-routines waiting for data to become available? */
\r
1234 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1236 /* In this instance the co-routine could be placed directly
\r
1237 into the ready list as we are within a critical section.
\r
1238 Instead the same pending ready list mechanism is used as if
\r
1239 the event were caused from within an interrupt. */
\r
1240 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1242 /* The co-routine waiting has a higher priority so record
\r
1243 that a yield might be appropriate. */
\r
1244 xReturn = errQUEUE_YIELD;
\r
1250 xReturn = errQUEUE_FULL;
\r
1253 portENABLE_INTERRUPTS();
\r
1258 /*-----------------------------------------------------------*/
\r
1260 #if configUSE_CO_ROUTINES == 1
\r
1261 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1263 signed portBASE_TYPE xReturn;
\r
1265 /* If the queue is already empty we may have to block. A critical section
\r
1266 is required to prevent an interrupt adding something to the queue
\r
1267 between the check to see if the queue is empty and blocking on the queue. */
\r
1268 portDISABLE_INTERRUPTS();
\r
1270 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1272 /* There are no messages in the queue, do we want to block or just
\r
1273 leave with nothing? */
\r
1274 if( xTicksToWait > ( portTickType ) 0 )
\r
1276 /* As this is a co-routine we cannot block directly, but return
\r
1277 indicating that we need to block. */
\r
1278 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1279 portENABLE_INTERRUPTS();
\r
1280 return errQUEUE_BLOCKED;
\r
1284 portENABLE_INTERRUPTS();
\r
1285 return errQUEUE_FULL;
\r
1289 portENABLE_INTERRUPTS();
\r
1293 portDISABLE_INTERRUPTS();
\r
1295 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1297 /* Data is available from the queue. */
\r
1298 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1299 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1301 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1303 --( pxQueue->uxMessagesWaiting );
\r
1304 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1308 /* Were any co-routines waiting for space to become available? */
\r
1309 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1311 /* In this instance the co-routine could be placed directly
\r
1312 into the ready list as we are within a critical section.
\r
1313 Instead the same pending ready list mechanism is used as if
\r
1314 the event were caused from within an interrupt. */
\r
1315 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1317 xReturn = errQUEUE_YIELD;
\r
1326 portENABLE_INTERRUPTS();
\r
1331 /*-----------------------------------------------------------*/
\r
1335 #if configUSE_CO_ROUTINES == 1
\r
1336 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1338 /* Cannot block within an ISR so if there is no space on the queue then
\r
1339 exit without doing anything. */
\r
1340 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1342 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1344 /* We only want to wake one co-routine per ISR, so check that a
\r
1345 co-routine has not already been woken. */
\r
1346 if( !xCoRoutinePreviouslyWoken )
\r
1348 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1350 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1358 return xCoRoutinePreviouslyWoken;
\r
1361 /*-----------------------------------------------------------*/
\r
1363 #if configUSE_CO_ROUTINES == 1
\r
1364 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1366 signed portBASE_TYPE xReturn;
\r
1368 /* We cannot block from an ISR, so check there is data available. If
\r
1369 not then just leave without doing anything. */
\r
1370 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1372 /* Copy the data from the queue. */
\r
1373 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1374 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1376 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1378 --( pxQueue->uxMessagesWaiting );
\r
1379 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1381 if( !( *pxCoRoutineWoken ) )
\r
1383 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1385 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1387 *pxCoRoutineWoken = pdTRUE;
\r
1402 /*-----------------------------------------------------------*/
\r
1404 #if configQUEUE_REGISTRY_SIZE > 0
\r
1406 void vQueueAddToRegistry( xQueueHandle xQueue, signed portCHAR *pcQueueName )
\r
1408 unsigned portBASE_TYPE ux;
\r
1410 /* See if there is an empty space in the registry. A NULL name denotes
\r
1412 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1414 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1416 /* Store the information on this queue. */
\r
1417 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1418 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1425 /*-----------------------------------------------------------*/
\r
1427 #if configQUEUE_REGISTRY_SIZE > 0
\r
1429 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1431 unsigned portBASE_TYPE ux;
\r
1433 /* See if the handle of the queue being unregistered in actually in the
\r
1435 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1437 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1439 /* Set the name to NULL to show that this slot if free again. */
\r
1440 xQueueRegistry[ ux ].pcQueueName = NULL;
\r