2 FreeRTOS.org V5.1.1 - 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
83 * Definition of the queue used by the scheduler.
\r
84 * Items are queued by copy, not reference.
\r
86 typedef struct QueueDefinition
\r
88 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
89 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
91 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
92 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
94 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
95 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
97 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
98 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
99 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
101 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
102 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
105 /*-----------------------------------------------------------*/
\r
108 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
109 * To keep the definition private the API header file defines it as a
\r
112 typedef xQUEUE * xQueueHandle;
\r
115 * Prototypes for public functions are included here so we don't have to
\r
116 * include the API header file (as it defines xQueueHandle differently). These
\r
117 * functions are documented in the API header file.
\r
119 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
120 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
121 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue );
\r
122 void vQueueDelete( xQueueHandle xQueue );
\r
123 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition );
\r
124 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
125 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
126 xQueueHandle xQueueCreateMutex( void );
\r
127 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );
\r
128 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime );
\r
129 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex );
\r
130 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
131 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
132 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue );
\r
133 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue );
\r
134 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue );
\r
137 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
138 * an optional component.
\r
140 #if configUSE_CO_ROUTINES == 1
\r
141 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
142 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
143 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
144 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
148 * The queue registry is just a means for kernel aware debuggers to locate
\r
149 * queue structures. It has no other purpose so is an optional component.
\r
151 #if configQUEUE_REGISTRY_SIZE > 0
\r
153 /* The type stored within the queue registry array. This allows a name
\r
154 to be assigned to each queue making kernel aware debugging a little
\r
155 more user friendly. */
\r
156 typedef struct QUEUE_REGISTRY_ITEM
\r
158 signed portCHAR *pcQueueName;
\r
159 xQueueHandle xHandle;
\r
160 } xQueueRegistryItem;
\r
162 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
163 The pcQueueName member of a structure being NULL is indicative of the
\r
164 array position being vacant. */
\r
165 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
167 /* Removes a queue from the registry by simply setting the pcQueueName
\r
169 static void vQueueUnregisterQueue( xQueueHandle xQueue );
\r
170 void vQueueAddToRegistry( xQueueHandle xQueue, signed portCHAR *pcQueueName );
\r
174 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
175 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
176 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
177 * queue is locked it will instead increment the appropriate queue lock count
\r
178 * to indicate that a task may require unblocking. When the queue in unlocked
\r
179 * these lock counts are inspected, and the appropriate action taken.
\r
181 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
184 * Uses a critical section to determine if there is any data in a queue.
\r
186 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
188 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
191 * Uses a critical section to determine if there is any space in a queue.
\r
193 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
195 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
198 * Copies an item into the queue, either at the front of the queue or the
\r
199 * back of the queue.
\r
201 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
204 * Copies an item out of a queue.
\r
206 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
207 /*-----------------------------------------------------------*/
\r
210 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
211 * accessing the queue event lists.
\r
213 #define prvLockQueue( pxQueue ) \
\r
215 taskENTER_CRITICAL(); \
\r
217 if( pxQueue->xRxLock == queueUNLOCKED ) \
\r
219 pxQueue->xRxLock = queueLOCKED_UNMODIFIED; \
\r
221 if( pxQueue->xTxLock == queueUNLOCKED ) \
\r
223 pxQueue->xTxLock = queueLOCKED_UNMODIFIED; \
\r
226 taskEXIT_CRITICAL(); \
\r
228 /*-----------------------------------------------------------*/
\r
231 /*-----------------------------------------------------------
\r
232 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
233 *----------------------------------------------------------*/
\r
235 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
237 xQUEUE *pxNewQueue;
\r
238 size_t xQueueSizeInBytes;
\r
240 /* Allocate the new queue structure. */
\r
241 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
243 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
244 if( pxNewQueue != NULL )
\r
246 /* Create the list of pointers to queue items. The queue is one byte
\r
247 longer than asked for to make wrap checking easier/faster. */
\r
248 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
250 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
251 if( pxNewQueue->pcHead != NULL )
\r
253 /* Initialise the queue members as described above where the
\r
254 queue type is defined. */
\r
255 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
256 pxNewQueue->uxMessagesWaiting = 0;
\r
257 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
258 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
259 pxNewQueue->uxLength = uxQueueLength;
\r
260 pxNewQueue->uxItemSize = uxItemSize;
\r
261 pxNewQueue->xRxLock = queueUNLOCKED;
\r
262 pxNewQueue->xTxLock = queueUNLOCKED;
\r
264 /* Likewise ensure the event queues start with the correct state. */
\r
265 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
266 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
268 traceQUEUE_CREATE( pxNewQueue );
\r
274 traceQUEUE_CREATE_FAILED();
\r
275 vPortFree( pxNewQueue );
\r
280 /* Will only reach here if we could not allocate enough memory or no memory
\r
284 /*-----------------------------------------------------------*/
\r
286 #if ( configUSE_MUTEXES == 1 )
\r
288 xQueueHandle xQueueCreateMutex( void )
\r
290 xQUEUE *pxNewQueue;
\r
292 /* Allocate the new queue structure. */
\r
293 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
294 if( pxNewQueue != NULL )
\r
296 /* Information required for priority inheritance. */
\r
297 pxNewQueue->pxMutexHolder = NULL;
\r
298 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
300 /* Queues used as a mutex no data is actually copied into or out
\r
302 pxNewQueue->pcWriteTo = NULL;
\r
303 pxNewQueue->pcReadFrom = NULL;
\r
305 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
306 an item size of 0 as nothing is actually copied into or out
\r
308 pxNewQueue->uxMessagesWaiting = 0;
\r
309 pxNewQueue->uxLength = 1;
\r
310 pxNewQueue->uxItemSize = 0;
\r
311 pxNewQueue->xRxLock = queueUNLOCKED;
\r
312 pxNewQueue->xTxLock = queueUNLOCKED;
\r
314 /* Ensure the event queues start with the correct state. */
\r
315 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
316 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
318 /* Start with the semaphore in the expected state. */
\r
319 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
321 traceCREATE_MUTEX( pxNewQueue );
\r
325 traceCREATE_MUTEX_FAILED();
\r
331 #endif /* configUSE_MUTEXES */
\r
332 /*-----------------------------------------------------------*/
\r
334 #if configUSE_RECURSIVE_MUTEXES == 1
\r
336 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
338 portBASE_TYPE xReturn;
\r
340 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
341 change outside of this task. If this task does not hold the mutex then
\r
342 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
343 this is the only condition we are interested in it does not matter if
\r
344 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
345 mutual exclusion is required to test the pxMutexHolder variable. */
\r
346 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
348 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
350 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
351 the task handle, therefore no underflow check is required. Also,
\r
352 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
353 there can only be one, no mutual exclusion is required to modify the
\r
354 uxRecursiveCallCount member. */
\r
355 ( pxMutex->uxRecursiveCallCount )--;
\r
357 /* Have we unwound the call count? */
\r
358 if( pxMutex->uxRecursiveCallCount == 0 )
\r
360 /* Return the mutex. This will automatically unblock any other
\r
361 task that might be waiting to access the mutex. */
\r
362 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
369 /* We cannot give the mutex because we are not the holder. */
\r
372 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
378 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
379 /*-----------------------------------------------------------*/
\r
381 #if configUSE_RECURSIVE_MUTEXES == 1
\r
383 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
385 portBASE_TYPE xReturn;
\r
387 /* Comments regarding mutual exclusion as per those within
\r
388 xQueueGiveMutexRecursive(). */
\r
390 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
392 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
394 ( pxMutex->uxRecursiveCallCount )++;
\r
399 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
401 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
402 we may have blocked to reach here. */
\r
403 if( xReturn == pdPASS )
\r
405 ( pxMutex->uxRecursiveCallCount )++;
\r
412 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
413 /*-----------------------------------------------------------*/
\r
415 #if configUSE_COUNTING_SEMAPHORES == 1
\r
417 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
419 xQueueHandle pxHandle;
\r
421 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
423 if( pxHandle != NULL )
\r
425 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
427 traceCREATE_COUNTING_SEMAPHORE();
\r
431 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
437 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
438 /*-----------------------------------------------------------*/
\r
440 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
442 signed portBASE_TYPE xReturn = pdTRUE;
\r
443 xTimeOutType xTimeOut;
\r
447 /* If xTicksToWait is zero then we are not going to block even
\r
448 if there is no room in the queue to post. */
\r
449 if( xTicksToWait > ( portTickType ) 0 )
\r
452 prvLockQueue( pxQueue );
\r
454 if( xReturn == pdTRUE )
\r
456 /* This is the first time through - we need to capture the
\r
457 time while the scheduler is locked to ensure we attempt to
\r
458 block at least once. */
\r
459 vTaskSetTimeOutState( &xTimeOut );
\r
462 if( prvIsQueueFull( pxQueue ) )
\r
464 /* Need to call xTaskCheckForTimeout again as time could
\r
465 have passed since it was last called if this is not the
\r
466 first time around this loop. */
\r
467 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
469 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
470 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
472 /* Unlocking the queue means queue events can effect the
\r
473 event list. It is possible that interrupts occurring now
\r
474 remove this task from the event list again - but as the
\r
475 scheduler is suspended the task will go onto the pending
\r
476 ready last instead of the actual ready list. */
\r
477 prvUnlockQueue( pxQueue );
\r
479 /* Resuming the scheduler will move tasks from the pending
\r
480 ready list into the ready list - so it is feasible that this
\r
481 task is already in a ready list before it yields - in which
\r
482 case the yield will not cause a context switch unless there
\r
483 is also a higher priority task in the pending ready list. */
\r
484 if( !xTaskResumeAll() )
\r
491 prvUnlockQueue( pxQueue );
\r
492 ( void ) xTaskResumeAll();
\r
497 /* The queue was not full so we can just unlock the
\r
498 scheduler and queue again before carrying on. */
\r
499 prvUnlockQueue( pxQueue );
\r
500 ( void ) xTaskResumeAll();
\r
504 /* Higher priority tasks and interrupts can execute during
\r
505 this time and could possible refill the queue - even if we
\r
506 unblocked because space became available. */
\r
508 taskENTER_CRITICAL();
\r
510 /* Is there room on the queue now? To be running we must be
\r
511 the highest priority task wanting to access the queue. */
\r
512 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
514 traceQUEUE_SEND( pxQueue );
\r
515 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
518 /* If there was a task waiting for data to arrive on the
\r
519 queue then unblock it now. */
\r
520 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
522 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
524 /* The unblocked task has a priority higher than
\r
525 our own so yield immediately. */
\r
532 /* Setting xReturn to errQUEUE_FULL will force its timeout
\r
533 to be re-evaluated. This is necessary in case interrupts
\r
534 and higher priority tasks accessed the queue between this
\r
535 task being unblocked and subsequently attempting to write
\r
537 xReturn = errQUEUE_FULL;
\r
540 taskEXIT_CRITICAL();
\r
542 if( xReturn == errQUEUE_FULL )
\r
544 if( xTicksToWait > ( portTickType ) 0 )
\r
546 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
548 xReturn = queueERRONEOUS_UNBLOCK;
\r
552 traceQUEUE_SEND_FAILED( pxQueue );
\r
557 traceQUEUE_SEND_FAILED( pxQueue );
\r
561 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
565 /*-----------------------------------------------------------*/
\r
567 #if configUSE_ALTERNATIVE_API == 1
\r
569 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
571 signed portBASE_TYPE xReturn = pdPASS;
\r
572 xTimeOutType xTimeOut;
\r
574 /* The source code that implements the alternative (Alt) API is
\r
575 simpler because it makes more use of critical sections. This is
\r
576 the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
577 preferred fully featured API too. The fully featured API has more
\r
578 complex code that takes longer to execute, but makes less use of
\r
579 critical sections. */
\r
583 /* If xTicksToWait is zero then we are not going to block even
\r
584 if there is no room in the queue to post. */
\r
585 if( xTicksToWait > ( portTickType ) 0 )
\r
587 portENTER_CRITICAL();
\r
589 if( xReturn == pdPASS )
\r
591 /* This is the first time through - capture the time
\r
592 inside the critical section to ensure we attempt to
\r
593 block at least once. */
\r
594 vTaskSetTimeOutState( &xTimeOut );
\r
597 if( prvIsQueueFull( pxQueue ) )
\r
599 /* Need to call xTaskCheckForTimeout again as time could
\r
600 have passed since it was last called if this is not the
\r
601 first time around this loop. */
\r
602 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
604 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
605 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
607 /* This will exit the critical section, then re-enter when
\r
608 the task next runs. */
\r
613 portEXIT_CRITICAL();
\r
616 /* Higher priority tasks and interrupts can execute during
\r
617 this time and could possible refill the queue - even if we
\r
618 unblocked because space became available. */
\r
620 taskENTER_CRITICAL();
\r
622 /* Is there room on the queue now? To be running we must be
\r
623 the highest priority task wanting to access the queue. */
\r
624 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
626 traceQUEUE_SEND( pxQueue );
\r
627 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
630 /* If there was a task waiting for data to arrive on the
\r
631 queue then unblock it now. */
\r
632 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
634 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
636 /* The unblocked task has a priority higher than
\r
637 our own so yield immediately. */
\r
644 /* Setting xReturn to errQUEUE_FULL will force its timeout
\r
645 to be re-evaluated. This is necessary in case interrupts
\r
646 and higher priority tasks accessed the queue between this
\r
647 task being unblocked and subsequently attempting to write
\r
649 xReturn = errQUEUE_FULL;
\r
652 taskEXIT_CRITICAL();
\r
654 if( xReturn == errQUEUE_FULL )
\r
656 if( xTicksToWait > ( portTickType ) 0 )
\r
658 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
660 xReturn = queueERRONEOUS_UNBLOCK;
\r
664 traceQUEUE_SEND_FAILED( pxQueue );
\r
669 traceQUEUE_SEND_FAILED( pxQueue );
\r
673 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
678 #endif /* configUSE_ALTERNATIVE_API */
\r
679 /*-----------------------------------------------------------*/
\r
681 #if configUSE_ALTERNATIVE_API == 1
\r
683 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
685 signed portBASE_TYPE xReturn = pdTRUE;
\r
686 xTimeOutType xTimeOut;
\r
687 signed portCHAR *pcOriginalReadPosition;
\r
689 /* The source code that implements the alternative (Alt) API is
\r
690 simpler because it makes more use of critical sections. This is
\r
691 the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
692 preferred fully featured API too. The fully featured API has more
\r
693 complex code that takes longer to execute, but makes less use of
\r
694 critical sections. */
\r
698 /* If there are no messages in the queue we may have to block. */
\r
699 if( xTicksToWait > ( portTickType ) 0 )
\r
701 portENTER_CRITICAL();
\r
703 if( xReturn == pdPASS )
\r
705 /* This is the first time through - capture the time
\r
706 inside the critical section to ensure we attempt to
\r
707 block at least once. */
\r
708 vTaskSetTimeOutState( &xTimeOut );
\r
711 if( prvIsQueueEmpty( pxQueue ) )
\r
713 /* Need to call xTaskCheckForTimeout again as time could
\r
714 have passed since it was last called if this is not the
\r
715 first time around this loop. */
\r
716 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
718 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
720 #if ( configUSE_MUTEXES == 1 )
\r
722 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
724 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
729 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
734 portEXIT_CRITICAL();
\r
737 taskENTER_CRITICAL();
\r
739 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
741 /* Remember our read position in case we are just peeking. */
\r
742 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
744 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
746 if( xJustPeeking == pdFALSE )
\r
748 traceQUEUE_RECEIVE( pxQueue );
\r
750 /* We are actually removing data. */
\r
751 --( pxQueue->uxMessagesWaiting );
\r
753 #if ( configUSE_MUTEXES == 1 )
\r
755 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
757 /* Record the information required to implement
\r
758 priority inheritance should it become necessary. */
\r
759 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
764 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
766 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
774 traceQUEUE_PEEK( pxQueue );
\r
776 /* We are not removing the data, so reset our read
\r
778 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
780 /* The data is being left in the queue, so see if there are
\r
781 any other tasks waiting for the data. */
\r
782 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
784 /* Tasks that are removed from the event list will get added to
\r
785 the pending ready list as the scheduler is still suspended. */
\r
786 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
788 /* The task waiting has a higher priority that this task. */
\r
799 xReturn = errQUEUE_EMPTY;
\r
802 taskEXIT_CRITICAL();
\r
804 if( xReturn == errQUEUE_EMPTY )
\r
806 if( xTicksToWait > ( portTickType ) 0 )
\r
808 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
810 xReturn = queueERRONEOUS_UNBLOCK;
\r
814 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
819 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
822 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
828 #endif /* configUSE_ALTERNATIVE_API */
\r
829 /*-----------------------------------------------------------*/
\r
831 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
833 signed portBASE_TYPE xReturn;
\r
834 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
836 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
837 in the queue. Also we don't directly wake a task that was blocked on a
\r
838 queue read, instead we return a flag to say whether a context switch is
\r
839 required or not (i.e. has a task with a higher priority than us been woken
\r
841 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
843 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
845 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
847 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
849 /* If the queue is locked we do not alter the event list. This will
\r
850 be done when the queue is unlocked later. */
\r
851 if( pxQueue->xTxLock == queueUNLOCKED )
\r
853 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
855 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
857 /* The task waiting has a higher priority so record that a
\r
858 context switch is required. */
\r
859 *pxHigherPriorityTaskWoken = pdTRUE;
\r
865 /* Increment the lock count so the task that unlocks the queue
\r
866 knows that data was posted while it was locked. */
\r
867 ++( pxQueue->xTxLock );
\r
874 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
875 xReturn = errQUEUE_FULL;
\r
878 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
882 /*-----------------------------------------------------------*/
\r
884 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
886 signed portBASE_TYPE xReturn = pdTRUE;
\r
887 xTimeOutType xTimeOut;
\r
888 signed portCHAR *pcOriginalReadPosition;
\r
892 /* If there are no messages in the queue we may have to block. */
\r
893 if( xTicksToWait > ( portTickType ) 0 )
\r
896 prvLockQueue( pxQueue );
\r
898 if( xReturn == pdTRUE )
\r
900 /* This is the first time through - we need to capture the
\r
901 time while the scheduler is locked to ensure we attempt to
\r
902 block at least once. */
\r
903 vTaskSetTimeOutState( &xTimeOut );
\r
906 if( prvIsQueueEmpty( pxQueue ) )
\r
908 /* Need to call xTaskCheckForTimeout again as time could
\r
909 have passed since it was last called if this is not the
\r
910 first time around this loop. */
\r
911 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
913 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
915 #if ( configUSE_MUTEXES == 1 )
\r
917 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
919 portENTER_CRITICAL();
\r
920 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
921 portEXIT_CRITICAL();
\r
926 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
927 prvUnlockQueue( pxQueue );
\r
928 if( !xTaskResumeAll() )
\r
935 prvUnlockQueue( pxQueue );
\r
936 ( void ) xTaskResumeAll();
\r
941 prvUnlockQueue( pxQueue );
\r
942 ( void ) xTaskResumeAll();
\r
946 taskENTER_CRITICAL();
\r
948 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
950 /* Remember our read position in case we are just peeking. */
\r
951 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
953 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
955 if( xJustPeeking == pdFALSE )
\r
957 traceQUEUE_RECEIVE( pxQueue );
\r
959 /* We are actually removing data. */
\r
960 --( pxQueue->uxMessagesWaiting );
\r
962 #if ( configUSE_MUTEXES == 1 )
\r
964 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
966 /* Record the information required to implement
\r
967 priority inheritance should it become necessary. */
\r
968 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
973 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
975 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
983 traceQUEUE_PEEK( pxQueue );
\r
985 /* We are not removing the data, so reset our read
\r
987 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
989 /* The data is being left in the queue, so see if there are
\r
990 any other tasks waiting for the data. */
\r
991 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
993 /* Tasks that are removed from the event list will get added to
\r
994 the pending ready list as the scheduler is still suspended. */
\r
995 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
997 /* The task waiting has a higher priority than this task. */
\r
1008 xReturn = errQUEUE_EMPTY;
\r
1011 taskEXIT_CRITICAL();
\r
1013 if( xReturn == errQUEUE_EMPTY )
\r
1015 if( xTicksToWait > ( portTickType ) 0 )
\r
1017 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1019 xReturn = queueERRONEOUS_UNBLOCK;
\r
1023 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1028 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1032 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
1036 /*-----------------------------------------------------------*/
\r
1038 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1040 signed portBASE_TYPE xReturn;
\r
1041 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1043 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1045 /* We cannot block from an ISR, so check there is data available. */
\r
1046 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1048 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1050 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1051 --( pxQueue->uxMessagesWaiting );
\r
1053 /* If the queue is locked we will not modify the event list. Instead
\r
1054 we update the lock count so the task that unlocks the queue will know
\r
1055 that an ISR has removed data while the queue was locked. */
\r
1056 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1058 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1060 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1062 /* The task waiting has a higher priority than us so
\r
1063 force a context switch. */
\r
1064 *pxTaskWoken = pdTRUE;
\r
1070 /* Increment the lock count so the task that unlocks the queue
\r
1071 knows that data was removed while it was locked. */
\r
1072 ++( pxQueue->xRxLock );
\r
1080 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1083 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1087 /*-----------------------------------------------------------*/
\r
1089 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1091 unsigned portBASE_TYPE uxReturn;
\r
1093 taskENTER_CRITICAL();
\r
1094 uxReturn = pxQueue->uxMessagesWaiting;
\r
1095 taskEXIT_CRITICAL();
\r
1099 /*-----------------------------------------------------------*/
\r
1101 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1103 unsigned portBASE_TYPE uxReturn;
\r
1105 uxReturn = pxQueue->uxMessagesWaiting;
\r
1109 /*-----------------------------------------------------------*/
\r
1111 void vQueueDelete( xQueueHandle pxQueue )
\r
1113 traceQUEUE_DELETE( pxQueue );
\r
1114 vQueueUnregisterQueue( pxQueue );
\r
1115 vPortFree( pxQueue->pcHead );
\r
1116 vPortFree( pxQueue );
\r
1118 /*-----------------------------------------------------------*/
\r
1120 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1122 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1124 #if ( configUSE_MUTEXES == 1 )
\r
1126 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1128 /* The mutex is no longer being held. */
\r
1129 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1130 pxQueue->pxMutexHolder = NULL;
\r
1135 else if( xPosition == queueSEND_TO_BACK )
\r
1137 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1138 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1139 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1141 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1146 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1147 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1148 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1150 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1154 ++( pxQueue->uxMessagesWaiting );
\r
1156 /*-----------------------------------------------------------*/
\r
1158 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1160 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1162 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1163 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1165 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1167 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1170 /*-----------------------------------------------------------*/
\r
1172 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1174 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1176 /* The lock counts contains the number of extra data items placed or
\r
1177 removed from the queue while the queue was locked. When a queue is
\r
1178 locked items can be added or removed, but the event lists cannot be
\r
1180 taskENTER_CRITICAL();
\r
1182 /* See if data was added to the queue while it was locked. */
\r
1183 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1185 /* Data was posted while the queue was locked. Are any tasks
\r
1186 blocked waiting for data to become available? */
\r
1187 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1189 /* Tasks that are removed from the event list will get added to
\r
1190 the pending ready list as the scheduler is still suspended. */
\r
1191 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1193 /* The task waiting has a higher priority so record that a
\r
1194 context switch is required. */
\r
1195 vTaskMissedYield();
\r
1198 --( pxQueue->xTxLock );
\r
1206 pxQueue->xTxLock = queueUNLOCKED;
\r
1208 taskEXIT_CRITICAL();
\r
1210 /* Do the same for the Rx lock. */
\r
1211 taskENTER_CRITICAL();
\r
1213 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1215 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1217 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1219 vTaskMissedYield();
\r
1222 --( pxQueue->xRxLock );
\r
1230 pxQueue->xRxLock = queueUNLOCKED;
\r
1232 taskEXIT_CRITICAL();
\r
1234 /*-----------------------------------------------------------*/
\r
1236 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1238 signed portBASE_TYPE xReturn;
\r
1240 taskENTER_CRITICAL();
\r
1241 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1242 taskEXIT_CRITICAL();
\r
1246 /*-----------------------------------------------------------*/
\r
1248 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1250 signed portBASE_TYPE xReturn;
\r
1252 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1256 /*-----------------------------------------------------------*/
\r
1258 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1260 signed portBASE_TYPE xReturn;
\r
1262 taskENTER_CRITICAL();
\r
1263 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1264 taskEXIT_CRITICAL();
\r
1268 /*-----------------------------------------------------------*/
\r
1270 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1272 signed portBASE_TYPE xReturn;
\r
1274 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1278 /*-----------------------------------------------------------*/
\r
1280 #if configUSE_CO_ROUTINES == 1
\r
1281 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1283 signed portBASE_TYPE xReturn;
\r
1285 /* If the queue is already full we may have to block. A critical section
\r
1286 is required to prevent an interrupt removing something from the queue
\r
1287 between the check to see if the queue is full and blocking on the queue. */
\r
1288 portDISABLE_INTERRUPTS();
\r
1290 if( prvIsQueueFull( pxQueue ) )
\r
1292 /* The queue is full - do we want to block or just leave without
\r
1294 if( xTicksToWait > ( portTickType ) 0 )
\r
1296 /* As this is called from a coroutine we cannot block directly, but
\r
1297 return indicating that we need to block. */
\r
1298 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1299 portENABLE_INTERRUPTS();
\r
1300 return errQUEUE_BLOCKED;
\r
1304 portENABLE_INTERRUPTS();
\r
1305 return errQUEUE_FULL;
\r
1309 portENABLE_INTERRUPTS();
\r
1313 portDISABLE_INTERRUPTS();
\r
1315 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1317 /* There is room in the queue, copy the data into the queue. */
\r
1318 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1321 /* Were any co-routines waiting for data to become available? */
\r
1322 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1324 /* In this instance the co-routine could be placed directly
\r
1325 into the ready list as we are within a critical section.
\r
1326 Instead the same pending ready list mechanism is used as if
\r
1327 the event were caused from within an interrupt. */
\r
1328 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1330 /* The co-routine waiting has a higher priority so record
\r
1331 that a yield might be appropriate. */
\r
1332 xReturn = errQUEUE_YIELD;
\r
1338 xReturn = errQUEUE_FULL;
\r
1341 portENABLE_INTERRUPTS();
\r
1346 /*-----------------------------------------------------------*/
\r
1348 #if configUSE_CO_ROUTINES == 1
\r
1349 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1351 signed portBASE_TYPE xReturn;
\r
1353 /* If the queue is already empty we may have to block. A critical section
\r
1354 is required to prevent an interrupt adding something to the queue
\r
1355 between the check to see if the queue is empty and blocking on the queue. */
\r
1356 portDISABLE_INTERRUPTS();
\r
1358 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1360 /* There are no messages in the queue, do we want to block or just
\r
1361 leave with nothing? */
\r
1362 if( xTicksToWait > ( portTickType ) 0 )
\r
1364 /* As this is a co-routine we cannot block directly, but return
\r
1365 indicating that we need to block. */
\r
1366 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1367 portENABLE_INTERRUPTS();
\r
1368 return errQUEUE_BLOCKED;
\r
1372 portENABLE_INTERRUPTS();
\r
1373 return errQUEUE_FULL;
\r
1377 portENABLE_INTERRUPTS();
\r
1381 portDISABLE_INTERRUPTS();
\r
1383 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1385 /* Data is available from the queue. */
\r
1386 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1387 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1389 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1391 --( pxQueue->uxMessagesWaiting );
\r
1392 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1396 /* Were any co-routines waiting for space to become available? */
\r
1397 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1399 /* In this instance the co-routine could be placed directly
\r
1400 into the ready list as we are within a critical section.
\r
1401 Instead the same pending ready list mechanism is used as if
\r
1402 the event were caused from within an interrupt. */
\r
1403 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1405 xReturn = errQUEUE_YIELD;
\r
1414 portENABLE_INTERRUPTS();
\r
1419 /*-----------------------------------------------------------*/
\r
1423 #if configUSE_CO_ROUTINES == 1
\r
1424 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1426 /* Cannot block within an ISR so if there is no space on the queue then
\r
1427 exit without doing anything. */
\r
1428 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1430 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1432 /* We only want to wake one co-routine per ISR, so check that a
\r
1433 co-routine has not already been woken. */
\r
1434 if( !xCoRoutinePreviouslyWoken )
\r
1436 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1438 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1446 return xCoRoutinePreviouslyWoken;
\r
1449 /*-----------------------------------------------------------*/
\r
1451 #if configUSE_CO_ROUTINES == 1
\r
1452 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1454 signed portBASE_TYPE xReturn;
\r
1456 /* We cannot block from an ISR, so check there is data available. If
\r
1457 not then just leave without doing anything. */
\r
1458 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1460 /* Copy the data from the queue. */
\r
1461 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1462 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1464 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1466 --( pxQueue->uxMessagesWaiting );
\r
1467 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1469 if( !( *pxCoRoutineWoken ) )
\r
1471 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1473 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1475 *pxCoRoutineWoken = pdTRUE;
\r
1490 /*-----------------------------------------------------------*/
\r
1492 #if configQUEUE_REGISTRY_SIZE > 0
\r
1494 void vQueueAddToRegistry( xQueueHandle xQueue, signed portCHAR *pcQueueName )
\r
1496 unsigned portBASE_TYPE ux;
\r
1498 /* See if there is an empty space in the registry. A NULL name denotes
\r
1500 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1502 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1504 /* Store the information on this queue. */
\r
1505 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1506 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1513 /*-----------------------------------------------------------*/
\r
1515 #if configQUEUE_REGISTRY_SIZE > 0
\r
1517 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1519 unsigned portBASE_TYPE ux;
\r
1521 /* See if the handle of the queue being unregistered in actually in the
\r
1523 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1525 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1527 /* Set the name to NULL to show that this slot if free again. */
\r
1528 xQueueRegistry[ ux ].pcQueueName = NULL;
\r