2 FreeRTOS.org V5.0.3 - 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 /* The two tasks are blocked on the queue, the low priority task is polling/running. */
\r
948 /* An interrupt occurs here - which unblocks the HP tasks, but they do not run. */
\r
949 taskENTER_CRITICAL();
\r
951 /* Because the interrupt occurred the LP task manages to grab the data as the other two tasks are not yet running. */
\r
952 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
954 /* Remember our read position in case we are just peeking. */
\r
955 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
957 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
959 if( xJustPeeking == pdFALSE )
\r
961 traceQUEUE_RECEIVE( pxQueue );
\r
963 /* We are actually removing data. */
\r
964 --( pxQueue->uxMessagesWaiting );
\r
966 #if ( configUSE_MUTEXES == 1 )
\r
968 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
970 /* Record the information required to implement
\r
971 priority inheritance should it become necessary. */
\r
972 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
977 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
979 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
987 traceQUEUE_PEEK( pxQueue );
\r
989 /* We are not removing the data, so reset our read
\r
991 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
993 /* The data is being left in the queue, so see if there are
\r
994 any other tasks waiting for the data. */
\r
995 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
997 /* Tasks that are removed from the event list will get added to
\r
998 the pending ready list as the scheduler is still suspended. */
\r
999 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1001 /* The task waiting has a higher priority than this task. */
\r
1012 xReturn = errQUEUE_EMPTY;
\r
1015 taskEXIT_CRITICAL();
\r
1017 if( xReturn == errQUEUE_EMPTY )
\r
1019 if( xTicksToWait > ( portTickType ) 0 )
\r
1021 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1023 xReturn = queueERRONEOUS_UNBLOCK;
\r
1027 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1032 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1036 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
1040 /*-----------------------------------------------------------*/
\r
1042 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1044 signed portBASE_TYPE xReturn;
\r
1045 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1047 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1049 /* We cannot block from an ISR, so check there is data available. */
\r
1050 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1052 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1054 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1055 --( pxQueue->uxMessagesWaiting );
\r
1057 /* If the queue is locked we will not modify the event list. Instead
\r
1058 we update the lock count so the task that unlocks the queue will know
\r
1059 that an ISR has removed data while the queue was locked. */
\r
1060 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1062 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1064 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1066 /* The task waiting has a higher priority than us so
\r
1067 force a context switch. */
\r
1068 *pxTaskWoken = pdTRUE;
\r
1074 /* Increment the lock count so the task that unlocks the queue
\r
1075 knows that data was removed while it was locked. */
\r
1076 ++( pxQueue->xRxLock );
\r
1084 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1087 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1091 /*-----------------------------------------------------------*/
\r
1093 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1095 unsigned portBASE_TYPE uxReturn;
\r
1097 taskENTER_CRITICAL();
\r
1098 uxReturn = pxQueue->uxMessagesWaiting;
\r
1099 taskEXIT_CRITICAL();
\r
1103 /*-----------------------------------------------------------*/
\r
1105 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1107 unsigned portBASE_TYPE uxReturn;
\r
1109 uxReturn = pxQueue->uxMessagesWaiting;
\r
1113 /*-----------------------------------------------------------*/
\r
1115 void vQueueDelete( xQueueHandle pxQueue )
\r
1117 traceQUEUE_DELETE( pxQueue );
\r
1118 vQueueUnregisterQueue( pxQueue );
\r
1119 vPortFree( pxQueue->pcHead );
\r
1120 vPortFree( pxQueue );
\r
1122 /*-----------------------------------------------------------*/
\r
1124 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1126 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1128 #if ( configUSE_MUTEXES == 1 )
\r
1130 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1132 /* The mutex is no longer being held. */
\r
1133 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1134 pxQueue->pxMutexHolder = NULL;
\r
1139 else if( xPosition == queueSEND_TO_BACK )
\r
1141 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1142 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1143 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1145 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1150 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1151 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1152 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1154 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1158 ++( pxQueue->uxMessagesWaiting );
\r
1160 /*-----------------------------------------------------------*/
\r
1162 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1164 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1166 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1167 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1169 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1171 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1174 /*-----------------------------------------------------------*/
\r
1176 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1178 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1180 /* The lock counts contains the number of extra data items placed or
\r
1181 removed from the queue while the queue was locked. When a queue is
\r
1182 locked items can be added or removed, but the event lists cannot be
\r
1184 taskENTER_CRITICAL();
\r
1186 /* See if data was added to the queue while it was locked. */
\r
1187 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1189 /* Data was posted while the queue was locked. Are any tasks
\r
1190 blocked waiting for data to become available? */
\r
1191 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1193 /* Tasks that are removed from the event list will get added to
\r
1194 the pending ready list as the scheduler is still suspended. */
\r
1195 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1197 /* The task waiting has a higher priority so record that a
\r
1198 context switch is required. */
\r
1199 vTaskMissedYield();
\r
1202 --( pxQueue->xTxLock );
\r
1210 pxQueue->xTxLock = queueUNLOCKED;
\r
1212 taskEXIT_CRITICAL();
\r
1214 /* Do the same for the Rx lock. */
\r
1215 taskENTER_CRITICAL();
\r
1217 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1219 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1221 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1223 vTaskMissedYield();
\r
1226 --( pxQueue->xRxLock );
\r
1234 pxQueue->xRxLock = queueUNLOCKED;
\r
1236 taskEXIT_CRITICAL();
\r
1238 /*-----------------------------------------------------------*/
\r
1240 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1242 signed portBASE_TYPE xReturn;
\r
1244 taskENTER_CRITICAL();
\r
1245 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1246 taskEXIT_CRITICAL();
\r
1250 /*-----------------------------------------------------------*/
\r
1252 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1254 signed portBASE_TYPE xReturn;
\r
1256 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1260 /*-----------------------------------------------------------*/
\r
1262 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1264 signed portBASE_TYPE xReturn;
\r
1266 taskENTER_CRITICAL();
\r
1267 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1268 taskEXIT_CRITICAL();
\r
1272 /*-----------------------------------------------------------*/
\r
1274 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1276 signed portBASE_TYPE xReturn;
\r
1278 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1282 /*-----------------------------------------------------------*/
\r
1284 #if configUSE_CO_ROUTINES == 1
\r
1285 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1287 signed portBASE_TYPE xReturn;
\r
1289 /* If the queue is already full we may have to block. A critical section
\r
1290 is required to prevent an interrupt removing something from the queue
\r
1291 between the check to see if the queue is full and blocking on the queue. */
\r
1292 portDISABLE_INTERRUPTS();
\r
1294 if( prvIsQueueFull( pxQueue ) )
\r
1296 /* The queue is full - do we want to block or just leave without
\r
1298 if( xTicksToWait > ( portTickType ) 0 )
\r
1300 /* As this is called from a coroutine we cannot block directly, but
\r
1301 return indicating that we need to block. */
\r
1302 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1303 portENABLE_INTERRUPTS();
\r
1304 return errQUEUE_BLOCKED;
\r
1308 portENABLE_INTERRUPTS();
\r
1309 return errQUEUE_FULL;
\r
1313 portENABLE_INTERRUPTS();
\r
1317 portDISABLE_INTERRUPTS();
\r
1319 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1321 /* There is room in the queue, copy the data into the queue. */
\r
1322 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1325 /* Were any co-routines waiting for data to become available? */
\r
1326 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1328 /* In this instance the co-routine could be placed directly
\r
1329 into the ready list as we are within a critical section.
\r
1330 Instead the same pending ready list mechanism is used as if
\r
1331 the event were caused from within an interrupt. */
\r
1332 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1334 /* The co-routine waiting has a higher priority so record
\r
1335 that a yield might be appropriate. */
\r
1336 xReturn = errQUEUE_YIELD;
\r
1342 xReturn = errQUEUE_FULL;
\r
1345 portENABLE_INTERRUPTS();
\r
1350 /*-----------------------------------------------------------*/
\r
1352 #if configUSE_CO_ROUTINES == 1
\r
1353 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1355 signed portBASE_TYPE xReturn;
\r
1357 /* If the queue is already empty we may have to block. A critical section
\r
1358 is required to prevent an interrupt adding something to the queue
\r
1359 between the check to see if the queue is empty and blocking on the queue. */
\r
1360 portDISABLE_INTERRUPTS();
\r
1362 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1364 /* There are no messages in the queue, do we want to block or just
\r
1365 leave with nothing? */
\r
1366 if( xTicksToWait > ( portTickType ) 0 )
\r
1368 /* As this is a co-routine we cannot block directly, but return
\r
1369 indicating that we need to block. */
\r
1370 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1371 portENABLE_INTERRUPTS();
\r
1372 return errQUEUE_BLOCKED;
\r
1376 portENABLE_INTERRUPTS();
\r
1377 return errQUEUE_FULL;
\r
1381 portENABLE_INTERRUPTS();
\r
1385 portDISABLE_INTERRUPTS();
\r
1387 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1389 /* Data is available from the queue. */
\r
1390 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1391 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1393 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1395 --( pxQueue->uxMessagesWaiting );
\r
1396 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1400 /* Were any co-routines waiting for space to become available? */
\r
1401 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1403 /* In this instance the co-routine could be placed directly
\r
1404 into the ready list as we are within a critical section.
\r
1405 Instead the same pending ready list mechanism is used as if
\r
1406 the event were caused from within an interrupt. */
\r
1407 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1409 xReturn = errQUEUE_YIELD;
\r
1418 portENABLE_INTERRUPTS();
\r
1423 /*-----------------------------------------------------------*/
\r
1427 #if configUSE_CO_ROUTINES == 1
\r
1428 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1430 /* Cannot block within an ISR so if there is no space on the queue then
\r
1431 exit without doing anything. */
\r
1432 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1434 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1436 /* We only want to wake one co-routine per ISR, so check that a
\r
1437 co-routine has not already been woken. */
\r
1438 if( !xCoRoutinePreviouslyWoken )
\r
1440 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1442 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1450 return xCoRoutinePreviouslyWoken;
\r
1453 /*-----------------------------------------------------------*/
\r
1455 #if configUSE_CO_ROUTINES == 1
\r
1456 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1458 signed portBASE_TYPE xReturn;
\r
1460 /* We cannot block from an ISR, so check there is data available. If
\r
1461 not then just leave without doing anything. */
\r
1462 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1464 /* Copy the data from the queue. */
\r
1465 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1466 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1468 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1470 --( pxQueue->uxMessagesWaiting );
\r
1471 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1473 if( !( *pxCoRoutineWoken ) )
\r
1475 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1477 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1479 *pxCoRoutineWoken = pdTRUE;
\r
1494 /*-----------------------------------------------------------*/
\r
1496 #if configQUEUE_REGISTRY_SIZE > 0
\r
1498 void vQueueAddToRegistry( xQueueHandle xQueue, signed portCHAR *pcQueueName )
\r
1500 unsigned portBASE_TYPE ux;
\r
1502 /* See if there is an empty space in the registry. A NULL name denotes
\r
1504 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1506 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1508 /* Store the information on this queue. */
\r
1509 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1510 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1517 /*-----------------------------------------------------------*/
\r
1519 #if configQUEUE_REGISTRY_SIZE > 0
\r
1521 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1523 unsigned portBASE_TYPE ux;
\r
1525 /* See if the handle of the queue being unregistered in actually in the
\r
1527 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1529 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1531 /* Set the name to NULL to show that this slot if free again. */
\r
1532 xQueueRegistry[ ux ].pcQueueName = NULL;
\r