2 FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
52 #include "FreeRTOS.h"
\r
54 #include "croutine.h"
\r
56 /*-----------------------------------------------------------
\r
57 * PUBLIC LIST API documented in list.h
\r
58 *----------------------------------------------------------*/
\r
60 /* Constants used with the cRxLock and cTxLock structure members. */
\r
61 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
62 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
64 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
66 /* For internal use only. */
\r
67 #define queueSEND_TO_BACK ( 0 )
\r
68 #define queueSEND_TO_FRONT ( 1 )
\r
70 /* Effectively make a union out of the xQUEUE structure. */
\r
71 #define pxMutexHolder pcTail
\r
72 #define uxQueueType pcHead
\r
73 #define uxRecursiveCallCount pcReadFrom
\r
74 #define queueQUEUE_IS_MUTEX NULL
\r
76 /* Semaphores do not actually store or copy data, so have an items size of
\r
78 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
79 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
80 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
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 static 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
176 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
177 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
178 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
179 * queue is locked it will instead increment the appropriate queue lock count
\r
180 * to indicate that a task may require unblocking. When the queue in unlocked
\r
181 * these lock counts are inspected, and the appropriate action taken.
\r
183 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
186 * Uses a critical section to determine if there is any data in a queue.
\r
188 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
190 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
193 * Uses a critical section to determine if there is any space in a queue.
\r
195 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
197 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
200 * Copies an item into the queue, either at the front of the queue or the
\r
201 * back of the queue.
\r
203 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
206 * Copies an item out of a queue.
\r
208 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
209 /*-----------------------------------------------------------*/
\r
212 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
213 * accessing the queue event lists.
\r
215 #define prvLockQueue( pxQueue ) \
\r
217 taskENTER_CRITICAL(); \
\r
219 if( pxQueue->xRxLock == queueUNLOCKED ) \
\r
221 pxQueue->xRxLock = queueLOCKED_UNMODIFIED; \
\r
223 if( pxQueue->xTxLock == queueUNLOCKED ) \
\r
225 pxQueue->xTxLock = queueLOCKED_UNMODIFIED; \
\r
228 taskEXIT_CRITICAL(); \
\r
230 /*-----------------------------------------------------------*/
\r
233 /*-----------------------------------------------------------
\r
234 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
235 *----------------------------------------------------------*/
\r
237 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
239 xQUEUE *pxNewQueue;
\r
240 size_t xQueueSizeInBytes;
\r
242 /* Allocate the new queue structure. */
\r
243 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
245 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
246 if( pxNewQueue != NULL )
\r
248 /* Create the list of pointers to queue items. The queue is one byte
\r
249 longer than asked for to make wrap checking easier/faster. */
\r
250 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
252 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
253 if( pxNewQueue->pcHead != NULL )
\r
255 /* Initialise the queue members as described above where the
\r
256 queue type is defined. */
\r
257 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
258 pxNewQueue->uxMessagesWaiting = 0;
\r
259 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
260 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
261 pxNewQueue->uxLength = uxQueueLength;
\r
262 pxNewQueue->uxItemSize = uxItemSize;
\r
263 pxNewQueue->xRxLock = queueUNLOCKED;
\r
264 pxNewQueue->xTxLock = queueUNLOCKED;
\r
266 /* Likewise ensure the event queues start with the correct state. */
\r
267 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
268 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
270 traceQUEUE_CREATE( pxNewQueue );
\r
276 traceQUEUE_CREATE_FAILED();
\r
277 vPortFree( pxNewQueue );
\r
282 /* Will only reach here if we could not allocate enough memory or no memory
\r
286 /*-----------------------------------------------------------*/
\r
288 #if ( configUSE_MUTEXES == 1 )
\r
290 xQueueHandle xQueueCreateMutex( void )
\r
292 xQUEUE *pxNewQueue;
\r
294 /* Allocate the new queue structure. */
\r
295 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
296 if( pxNewQueue != NULL )
\r
298 /* Information required for priority inheritance. */
\r
299 pxNewQueue->pxMutexHolder = NULL;
\r
300 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
302 /* Queues used as a mutex no data is actually copied into or out
\r
304 pxNewQueue->pcWriteTo = NULL;
\r
305 pxNewQueue->pcReadFrom = NULL;
\r
307 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
308 an item size of 0 as nothing is actually copied into or out
\r
310 pxNewQueue->uxMessagesWaiting = 0;
\r
311 pxNewQueue->uxLength = 1;
\r
312 pxNewQueue->uxItemSize = 0;
\r
313 pxNewQueue->xRxLock = queueUNLOCKED;
\r
314 pxNewQueue->xTxLock = queueUNLOCKED;
\r
316 /* Ensure the event queues start with the correct state. */
\r
317 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
318 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
320 /* Start with the semaphore in the expected state. */
\r
321 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
323 traceCREATE_MUTEX( pxNewQueue );
\r
327 traceCREATE_MUTEX_FAILED();
\r
333 #endif /* configUSE_MUTEXES */
\r
334 /*-----------------------------------------------------------*/
\r
336 #if configUSE_RECURSIVE_MUTEXES == 1
\r
338 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
340 portBASE_TYPE xReturn;
\r
342 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
343 change outside of this task. If this task does not hold the mutex then
\r
344 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
345 this is the only condition we are interested in it does not matter if
\r
346 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
347 mutual exclusion is required to test the pxMutexHolder variable. */
\r
348 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
350 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
352 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
353 the task handle, therefore no underflow check is required. Also,
\r
354 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
355 there can only be one, no mutual exclusion is required to modify the
\r
356 uxRecursiveCallCount member. */
\r
357 ( pxMutex->uxRecursiveCallCount )--;
\r
359 /* Have we unwound the call count? */
\r
360 if( pxMutex->uxRecursiveCallCount == 0 )
\r
362 /* Return the mutex. This will automatically unblock any other
\r
363 task that might be waiting to access the mutex. */
\r
364 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
371 /* We cannot give the mutex because we are not the holder. */
\r
374 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
380 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
381 /*-----------------------------------------------------------*/
\r
383 #if configUSE_RECURSIVE_MUTEXES == 1
\r
385 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
387 portBASE_TYPE xReturn;
\r
389 /* Comments regarding mutual exclusion as per those within
\r
390 xQueueGiveMutexRecursive(). */
\r
392 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
394 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
396 ( pxMutex->uxRecursiveCallCount )++;
\r
401 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
403 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
404 we may have blocked to reach here. */
\r
405 if( xReturn == pdPASS )
\r
407 ( pxMutex->uxRecursiveCallCount )++;
\r
414 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
415 /*-----------------------------------------------------------*/
\r
417 #if configUSE_COUNTING_SEMAPHORES == 1
\r
419 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
421 xQueueHandle pxHandle;
\r
423 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
425 if( pxHandle != NULL )
\r
427 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
429 traceCREATE_COUNTING_SEMAPHORE();
\r
433 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
439 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
440 /*-----------------------------------------------------------*/
\r
442 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
444 signed portBASE_TYPE xReturn = pdTRUE;
\r
445 xTimeOutType xTimeOut;
\r
449 /* If xTicksToWait is zero then we are not going to block even
\r
450 if there is no room in the queue to post. */
\r
451 if( xTicksToWait > ( portTickType ) 0 )
\r
454 prvLockQueue( pxQueue );
\r
456 if( xReturn == pdTRUE )
\r
458 /* This is the first time through - we need to capture the
\r
459 time while the scheduler is locked to ensure we attempt to
\r
460 block at least once. */
\r
461 vTaskSetTimeOutState( &xTimeOut );
\r
464 if( prvIsQueueFull( pxQueue ) )
\r
466 /* Need to call xTaskCheckForTimeout again as time could
\r
467 have passed since it was last called if this is not the
\r
468 first time around this loop. */
\r
469 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
471 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
472 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
474 /* Unlocking the queue means queue events can effect the
\r
475 event list. It is possible that interrupts occurring now
\r
476 remove this task from the event list again - but as the
\r
477 scheduler is suspended the task will go onto the pending
\r
478 ready last instead of the actual ready list. */
\r
479 prvUnlockQueue( pxQueue );
\r
481 /* Resuming the scheduler will move tasks from the pending
\r
482 ready list into the ready list - so it is feasible that this
\r
483 task is already in a ready list before it yields - in which
\r
484 case the yield will not cause a context switch unless there
\r
485 is also a higher priority task in the pending ready list. */
\r
486 if( !xTaskResumeAll() )
\r
493 prvUnlockQueue( pxQueue );
\r
494 ( void ) xTaskResumeAll();
\r
499 /* The queue was not full so we can just unlock the
\r
500 scheduler and queue again before carrying on. */
\r
501 prvUnlockQueue( pxQueue );
\r
502 ( void ) xTaskResumeAll();
\r
506 /* Higher priority tasks and interrupts can execute during
\r
507 this time and could possible refill the queue - even if we
\r
508 unblocked because space became available. */
\r
510 taskENTER_CRITICAL();
\r
512 /* Is there room on the queue now? To be running we must be
\r
513 the highest priority task wanting to access the queue. */
\r
514 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
516 traceQUEUE_SEND( pxQueue );
\r
517 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
520 /* If there was a task waiting for data to arrive on the
\r
521 queue then unblock it now. */
\r
522 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
524 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
526 /* The unblocked task has a priority higher than
\r
527 our own so yield immediately. */
\r
534 /* Setting xReturn to errQUEUE_FULL will force its timeout
\r
535 to be re-evaluated. This is necessary in case interrupts
\r
536 and higher priority tasks accessed the queue between this
\r
537 task being unblocked and subsequently attempting to write
\r
539 xReturn = errQUEUE_FULL;
\r
542 taskEXIT_CRITICAL();
\r
544 if( xReturn == errQUEUE_FULL )
\r
546 if( xTicksToWait > ( portTickType ) 0 )
\r
548 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
550 xReturn = queueERRONEOUS_UNBLOCK;
\r
554 traceQUEUE_SEND_FAILED( pxQueue );
\r
559 traceQUEUE_SEND_FAILED( pxQueue );
\r
563 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
567 /*-----------------------------------------------------------*/
\r
569 #if configUSE_ALTERNATIVE_API == 1
\r
571 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
573 signed portBASE_TYPE xReturn = pdPASS;
\r
574 xTimeOutType xTimeOut;
\r
576 /* The source code that implements the alternative (Alt) API is
\r
577 simpler because it makes more use of critical sections. This is
\r
578 the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
579 preferred fully featured API too. The fully featured API has more
\r
580 complex code that takes longer to execute, but makes less use of
\r
581 critical sections. */
\r
585 /* If xTicksToWait is zero then we are not going to block even
\r
586 if there is no room in the queue to post. */
\r
587 if( xTicksToWait > ( portTickType ) 0 )
\r
589 portENTER_CRITICAL();
\r
591 if( xReturn == pdPASS )
\r
593 /* This is the first time through - capture the time
\r
594 inside the critical section to ensure we attempt to
\r
595 block at least once. */
\r
596 vTaskSetTimeOutState( &xTimeOut );
\r
599 if( prvIsQueueFull( pxQueue ) )
\r
601 /* Need to call xTaskCheckForTimeout again as time could
\r
602 have passed since it was last called if this is not the
\r
603 first time around this loop. */
\r
604 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
606 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
607 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
609 /* This will exit the critical section, then re-enter when
\r
610 the task next runs. */
\r
615 portEXIT_CRITICAL();
\r
618 /* Higher priority tasks and interrupts can execute during
\r
619 this time and could possible refill the queue - even if we
\r
620 unblocked because space became available. */
\r
622 taskENTER_CRITICAL();
\r
624 /* Is there room on the queue now? To be running we must be
\r
625 the highest priority task wanting to access the queue. */
\r
626 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
628 traceQUEUE_SEND( pxQueue );
\r
629 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
632 /* If there was a task waiting for data to arrive on the
\r
633 queue then unblock it now. */
\r
634 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
636 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
638 /* The unblocked task has a priority higher than
\r
639 our own so yield immediately. */
\r
646 /* Setting xReturn to errQUEUE_FULL will force its timeout
\r
647 to be re-evaluated. This is necessary in case interrupts
\r
648 and higher priority tasks accessed the queue between this
\r
649 task being unblocked and subsequently attempting to write
\r
651 xReturn = errQUEUE_FULL;
\r
654 taskEXIT_CRITICAL();
\r
656 if( xReturn == errQUEUE_FULL )
\r
658 if( xTicksToWait > ( portTickType ) 0 )
\r
660 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
662 xReturn = queueERRONEOUS_UNBLOCK;
\r
666 traceQUEUE_SEND_FAILED( pxQueue );
\r
671 traceQUEUE_SEND_FAILED( pxQueue );
\r
675 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
680 #endif /* configUSE_ALTERNATIVE_API */
\r
681 /*-----------------------------------------------------------*/
\r
683 #if configUSE_ALTERNATIVE_API == 1
\r
685 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
687 signed portBASE_TYPE xReturn = pdTRUE;
\r
688 xTimeOutType xTimeOut;
\r
689 signed portCHAR *pcOriginalReadPosition;
\r
691 /* The source code that implements the alternative (Alt) API is
\r
692 simpler because it makes more use of critical sections. This is
\r
693 the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
694 preferred fully featured API too. The fully featured API has more
\r
695 complex code that takes longer to execute, but makes less use of
\r
696 critical sections. */
\r
700 /* If there are no messages in the queue we may have to block. */
\r
701 if( xTicksToWait > ( portTickType ) 0 )
\r
703 portENTER_CRITICAL();
\r
705 if( xReturn == pdPASS )
\r
707 /* This is the first time through - capture the time
\r
708 inside the critical section to ensure we attempt to
\r
709 block at least once. */
\r
710 vTaskSetTimeOutState( &xTimeOut );
\r
713 if( prvIsQueueEmpty( pxQueue ) )
\r
715 /* Need to call xTaskCheckForTimeout again as time could
\r
716 have passed since it was last called if this is not the
\r
717 first time around this loop. */
\r
718 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
720 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
722 #if ( configUSE_MUTEXES == 1 )
\r
724 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
726 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
731 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
736 portEXIT_CRITICAL();
\r
739 taskENTER_CRITICAL();
\r
741 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
743 /* Remember our read position in case we are just peeking. */
\r
744 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
746 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
748 if( xJustPeeking == pdFALSE )
\r
750 traceQUEUE_RECEIVE( pxQueue );
\r
752 /* We are actually removing data. */
\r
753 --( pxQueue->uxMessagesWaiting );
\r
755 #if ( configUSE_MUTEXES == 1 )
\r
757 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
759 /* Record the information required to implement
\r
760 priority inheritance should it become necessary. */
\r
761 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
766 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
768 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
776 traceQUEUE_PEEK( pxQueue );
\r
778 /* We are not removing the data, so reset our read
\r
780 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
782 /* The data is being left in the queue, so see if there are
\r
783 any other tasks waiting for the data. */
\r
784 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
786 /* Tasks that are removed from the event list will get added to
\r
787 the pending ready list as the scheduler is still suspended. */
\r
788 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
790 /* The task waiting has a higher priority that this task. */
\r
801 xReturn = errQUEUE_EMPTY;
\r
804 taskEXIT_CRITICAL();
\r
806 if( xReturn == errQUEUE_EMPTY )
\r
808 if( xTicksToWait > ( portTickType ) 0 )
\r
810 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
812 xReturn = queueERRONEOUS_UNBLOCK;
\r
816 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
821 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
824 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
830 #endif /* configUSE_ALTERNATIVE_API */
\r
831 /*-----------------------------------------------------------*/
\r
833 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
835 signed portBASE_TYPE xReturn;
\r
836 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
838 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
839 in the queue. Also we don't directly wake a task that was blocked on a
\r
840 queue read, instead we return a flag to say whether a context switch is
\r
841 required or not (i.e. has a task with a higher priority than us been woken
\r
843 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
845 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
847 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
849 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
851 /* If the queue is locked we do not alter the event list. This will
\r
852 be done when the queue is unlocked later. */
\r
853 if( pxQueue->xTxLock == queueUNLOCKED )
\r
855 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
857 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
859 /* The task waiting has a higher priority so record that a
\r
860 context switch is required. */
\r
861 *pxHigherPriorityTaskWoken = pdTRUE;
\r
867 /* Increment the lock count so the task that unlocks the queue
\r
868 knows that data was posted while it was locked. */
\r
869 ++( pxQueue->xTxLock );
\r
876 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
877 xReturn = errQUEUE_FULL;
\r
880 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
884 /*-----------------------------------------------------------*/
\r
886 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
888 signed portBASE_TYPE xReturn = pdTRUE;
\r
889 xTimeOutType xTimeOut;
\r
890 signed portCHAR *pcOriginalReadPosition;
\r
894 /* If there are no messages in the queue we may have to block. */
\r
895 if( xTicksToWait > ( portTickType ) 0 )
\r
898 prvLockQueue( pxQueue );
\r
900 if( xReturn == pdTRUE )
\r
902 /* This is the first time through - we need to capture the
\r
903 time while the scheduler is locked to ensure we attempt to
\r
904 block at least once. */
\r
905 vTaskSetTimeOutState( &xTimeOut );
\r
908 if( prvIsQueueEmpty( pxQueue ) )
\r
910 /* Need to call xTaskCheckForTimeout again as time could
\r
911 have passed since it was last called if this is not the
\r
912 first time around this loop. */
\r
913 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
915 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
917 #if ( configUSE_MUTEXES == 1 )
\r
919 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
921 portENTER_CRITICAL();
\r
922 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
923 portEXIT_CRITICAL();
\r
928 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
929 prvUnlockQueue( pxQueue );
\r
930 if( !xTaskResumeAll() )
\r
937 prvUnlockQueue( pxQueue );
\r
938 ( void ) xTaskResumeAll();
\r
943 prvUnlockQueue( pxQueue );
\r
944 ( void ) xTaskResumeAll();
\r
948 taskENTER_CRITICAL();
\r
950 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
952 /* Remember our read position in case we are just peeking. */
\r
953 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
955 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
957 if( xJustPeeking == pdFALSE )
\r
959 traceQUEUE_RECEIVE( pxQueue );
\r
961 /* We are actually removing data. */
\r
962 --( pxQueue->uxMessagesWaiting );
\r
964 #if ( configUSE_MUTEXES == 1 )
\r
966 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
968 /* Record the information required to implement
\r
969 priority inheritance should it become necessary. */
\r
970 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
975 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
977 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
985 traceQUEUE_PEEK( pxQueue );
\r
987 /* We are not removing the data, so reset our read
\r
989 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
991 /* The data is being left in the queue, so see if there are
\r
992 any other tasks waiting for the data. */
\r
993 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
995 /* Tasks that are removed from the event list will get added to
\r
996 the pending ready list as the scheduler is still suspended. */
\r
997 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
999 /* The task waiting has a higher priority that this task. */
\r
1006 xReturn = pdPASS;
\r
1010 xReturn = errQUEUE_EMPTY;
\r
1013 taskEXIT_CRITICAL();
\r
1015 if( xReturn == errQUEUE_EMPTY )
\r
1017 if( xTicksToWait > ( portTickType ) 0 )
\r
1019 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1021 xReturn = queueERRONEOUS_UNBLOCK;
\r
1025 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1030 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1033 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
1037 /*-----------------------------------------------------------*/
\r
1039 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1041 signed portBASE_TYPE xReturn;
\r
1042 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1044 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1046 /* We cannot block from an ISR, so check there is data available. */
\r
1047 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1049 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1051 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1052 --( pxQueue->uxMessagesWaiting );
\r
1054 /* If the queue is locked we will not modify the event list. Instead
\r
1055 we update the lock count so the task that unlocks the queue will know
\r
1056 that an ISR has removed data while the queue was locked. */
\r
1057 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1059 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1061 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1063 /* The task waiting has a higher priority than us so
\r
1064 force a context switch. */
\r
1065 *pxTaskWoken = pdTRUE;
\r
1071 /* Increment the lock count so the task that unlocks the queue
\r
1072 knows that data was removed while it was locked. */
\r
1073 ++( pxQueue->xRxLock );
\r
1081 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1084 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1088 /*-----------------------------------------------------------*/
\r
1090 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1092 unsigned portBASE_TYPE uxReturn;
\r
1094 taskENTER_CRITICAL();
\r
1095 uxReturn = pxQueue->uxMessagesWaiting;
\r
1096 taskEXIT_CRITICAL();
\r
1100 /*-----------------------------------------------------------*/
\r
1102 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1104 unsigned portBASE_TYPE uxReturn;
\r
1106 uxReturn = pxQueue->uxMessagesWaiting;
\r
1110 /*-----------------------------------------------------------*/
\r
1112 void vQueueDelete( xQueueHandle pxQueue )
\r
1114 traceQUEUE_DELETE( pxQueue );
\r
1115 vQueueUnregisterQueue( pxQueue );
\r
1116 vPortFree( pxQueue->pcHead );
\r
1117 vPortFree( pxQueue );
\r
1119 /*-----------------------------------------------------------*/
\r
1121 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1123 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1125 #if ( configUSE_MUTEXES == 1 )
\r
1127 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1129 /* The mutex is no longer being held. */
\r
1130 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1131 pxQueue->pxMutexHolder = NULL;
\r
1136 else if( xPosition == queueSEND_TO_BACK )
\r
1138 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1139 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1140 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1142 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1147 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1148 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1149 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1151 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1155 ++( pxQueue->uxMessagesWaiting );
\r
1157 /*-----------------------------------------------------------*/
\r
1159 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1161 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1163 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1164 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1166 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1168 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1171 /*-----------------------------------------------------------*/
\r
1173 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1175 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1177 /* The lock counts contains the number of extra data items placed or
\r
1178 removed from the queue while the queue was locked. When a queue is
\r
1179 locked items can be added or removed, but the event lists cannot be
\r
1181 taskENTER_CRITICAL();
\r
1183 /* See if data was added to the queue while it was locked. */
\r
1184 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1186 /* Data was posted while the queue was locked. Are any tasks
\r
1187 blocked waiting for data to become available? */
\r
1188 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1190 /* Tasks that are removed from the event list will get added to
\r
1191 the pending ready list as the scheduler is still suspended. */
\r
1192 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1194 /* The task waiting has a higher priority so record that a
\r
1195 context switch is required. */
\r
1196 vTaskMissedYield();
\r
1199 --( pxQueue->xTxLock );
\r
1207 pxQueue->xTxLock = queueUNLOCKED;
\r
1209 taskEXIT_CRITICAL();
\r
1211 /* Do the same for the Rx lock. */
\r
1212 taskENTER_CRITICAL();
\r
1214 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1216 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1218 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1220 vTaskMissedYield();
\r
1223 --( pxQueue->xRxLock );
\r
1231 pxQueue->xRxLock = queueUNLOCKED;
\r
1233 taskEXIT_CRITICAL();
\r
1235 /*-----------------------------------------------------------*/
\r
1237 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1239 signed portBASE_TYPE xReturn;
\r
1241 taskENTER_CRITICAL();
\r
1242 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1243 taskEXIT_CRITICAL();
\r
1247 /*-----------------------------------------------------------*/
\r
1249 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1251 signed portBASE_TYPE xReturn;
\r
1253 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1257 /*-----------------------------------------------------------*/
\r
1259 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1261 signed portBASE_TYPE xReturn;
\r
1263 taskENTER_CRITICAL();
\r
1264 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1265 taskEXIT_CRITICAL();
\r
1269 /*-----------------------------------------------------------*/
\r
1271 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1273 signed portBASE_TYPE xReturn;
\r
1275 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1279 /*-----------------------------------------------------------*/
\r
1281 #if configUSE_CO_ROUTINES == 1
\r
1282 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1284 signed portBASE_TYPE xReturn;
\r
1286 /* If the queue is already full we may have to block. A critical section
\r
1287 is required to prevent an interrupt removing something from the queue
\r
1288 between the check to see if the queue is full and blocking on the queue. */
\r
1289 portDISABLE_INTERRUPTS();
\r
1291 if( prvIsQueueFull( pxQueue ) )
\r
1293 /* The queue is full - do we want to block or just leave without
\r
1295 if( xTicksToWait > ( portTickType ) 0 )
\r
1297 /* As this is called from a coroutine we cannot block directly, but
\r
1298 return indicating that we need to block. */
\r
1299 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1300 portENABLE_INTERRUPTS();
\r
1301 return errQUEUE_BLOCKED;
\r
1305 portENABLE_INTERRUPTS();
\r
1306 return errQUEUE_FULL;
\r
1310 portENABLE_INTERRUPTS();
\r
1314 portDISABLE_INTERRUPTS();
\r
1316 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1318 /* There is room in the queue, copy the data into the queue. */
\r
1319 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1322 /* Were any co-routines waiting for data to become available? */
\r
1323 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1325 /* In this instance the co-routine could be placed directly
\r
1326 into the ready list as we are within a critical section.
\r
1327 Instead the same pending ready list mechanism is used as if
\r
1328 the event were caused from within an interrupt. */
\r
1329 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1331 /* The co-routine waiting has a higher priority so record
\r
1332 that a yield might be appropriate. */
\r
1333 xReturn = errQUEUE_YIELD;
\r
1339 xReturn = errQUEUE_FULL;
\r
1342 portENABLE_INTERRUPTS();
\r
1347 /*-----------------------------------------------------------*/
\r
1349 #if configUSE_CO_ROUTINES == 1
\r
1350 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1352 signed portBASE_TYPE xReturn;
\r
1354 /* If the queue is already empty we may have to block. A critical section
\r
1355 is required to prevent an interrupt adding something to the queue
\r
1356 between the check to see if the queue is empty and blocking on the queue. */
\r
1357 portDISABLE_INTERRUPTS();
\r
1359 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1361 /* There are no messages in the queue, do we want to block or just
\r
1362 leave with nothing? */
\r
1363 if( xTicksToWait > ( portTickType ) 0 )
\r
1365 /* As this is a co-routine we cannot block directly, but return
\r
1366 indicating that we need to block. */
\r
1367 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1368 portENABLE_INTERRUPTS();
\r
1369 return errQUEUE_BLOCKED;
\r
1373 portENABLE_INTERRUPTS();
\r
1374 return errQUEUE_FULL;
\r
1378 portENABLE_INTERRUPTS();
\r
1382 portDISABLE_INTERRUPTS();
\r
1384 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1386 /* Data is available from the queue. */
\r
1387 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1388 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1390 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1392 --( pxQueue->uxMessagesWaiting );
\r
1393 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1397 /* Were any co-routines waiting for space to become available? */
\r
1398 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1400 /* In this instance the co-routine could be placed directly
\r
1401 into the ready list as we are within a critical section.
\r
1402 Instead the same pending ready list mechanism is used as if
\r
1403 the event were caused from within an interrupt. */
\r
1404 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1406 xReturn = errQUEUE_YIELD;
\r
1415 portENABLE_INTERRUPTS();
\r
1420 /*-----------------------------------------------------------*/
\r
1424 #if configUSE_CO_ROUTINES == 1
\r
1425 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1427 /* Cannot block within an ISR so if there is no space on the queue then
\r
1428 exit without doing anything. */
\r
1429 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1431 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1433 /* We only want to wake one co-routine per ISR, so check that a
\r
1434 co-routine has not already been woken. */
\r
1435 if( !xCoRoutinePreviouslyWoken )
\r
1437 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1439 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1447 return xCoRoutinePreviouslyWoken;
\r
1450 /*-----------------------------------------------------------*/
\r
1452 #if configUSE_CO_ROUTINES == 1
\r
1453 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1455 signed portBASE_TYPE xReturn;
\r
1457 /* We cannot block from an ISR, so check there is data available. If
\r
1458 not then just leave without doing anything. */
\r
1459 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1461 /* Copy the data from the queue. */
\r
1462 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1463 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1465 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1467 --( pxQueue->uxMessagesWaiting );
\r
1468 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1470 if( !( *pxCoRoutineWoken ) )
\r
1472 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1474 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1476 *pxCoRoutineWoken = pdTRUE;
\r
1491 /*-----------------------------------------------------------*/
\r
1493 #if configQUEUE_REGISTRY_SIZE > 0
\r
1495 void vQueueAddToRegistry( xQueueHandle xQueue, signed portCHAR *pcQueueName )
\r
1497 unsigned portBASE_TYPE ux;
\r
1499 /* See if there is an empty space in the registry. A NULL name denotes
\r
1501 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1503 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1505 /* Store the information on this queue. */
\r
1506 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1507 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1514 /*-----------------------------------------------------------*/
\r
1516 #if configQUEUE_REGISTRY_SIZE > 0
\r
1518 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1520 unsigned portBASE_TYPE ux;
\r
1522 /* See if the handle of the queue being unregistered in actually in the
\r
1524 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1526 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1528 /* Set the name to NULL to show that this slot if free again. */
\r
1529 xQueueRegistry[ ux ].pcQueueName = NULL;
\r