2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
39 #include "FreeRTOS.h"
\r
41 #include "croutine.h"
\r
43 /*-----------------------------------------------------------
\r
44 * PUBLIC LIST API documented in list.h
\r
45 *----------------------------------------------------------*/
\r
47 /* Constants used with the cRxLock and cTxLock structure members. */
\r
48 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
49 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
51 /* For internal use only. */
\r
52 #define queueSEND_TO_BACK ( 0 )
\r
53 #define queueSEND_TO_FRONT ( 1 )
\r
55 /* Effectively make a union out of the xQUEUE structure. */
\r
56 #define pxMutexHolder pcTail
\r
57 #define uxQueueType pcHead
\r
58 #define queueQUEUE_IS_MUTEX NULL
\r
60 /* Semaphores do not actually store or copy data, so have an items size of
\r
62 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
63 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
66 * Definition of the queue used by the scheduler.
\r
67 * Items are queued by copy, not reference.
\r
69 typedef struct QueueDefinition
\r
71 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
72 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
74 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
75 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
77 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
78 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
80 unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
81 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
82 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
84 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
85 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
87 /*-----------------------------------------------------------*/
\r
90 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
91 * To keep the definition private the API header file defines it as a
\r
94 typedef xQUEUE * xQueueHandle;
\r
97 * Prototypes for public functions are included here so we don't have to
\r
98 * include the API header file (as it defines xQueueHandle differently). These
\r
99 * functions are documented in the API header file.
\r
101 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
102 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
103 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue );
\r
104 void vQueueDelete( xQueueHandle xQueue );
\r
105 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition );
\r
106 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
107 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
108 xQueueHandle xQueueCreateMutex( void );
\r
109 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );
\r
111 #if configUSE_CO_ROUTINES == 1
\r
112 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
113 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
114 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
115 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
119 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
120 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
121 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
122 * queue is locked it will instead increment the appropriate queue lock count
\r
123 * to indicate that a task may require unblocking. When the queue in unlocked
\r
124 * these lock counts are inspected, and the appropriate action taken.
\r
126 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
129 * Uses a critical section to determine if there is any data in a queue.
\r
131 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
133 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
136 * Uses a critical section to determine if there is any space in a queue.
\r
138 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
140 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
143 * Copies an item into the queue, either at the front of the queue or the
\r
144 * back of the queue.
\r
146 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
149 * Copies an item out of a queue.
\r
151 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
152 /*-----------------------------------------------------------*/
\r
155 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
156 * accessing the queue event lists.
\r
158 #define prvLockQueue( pxQueue ) \
\r
160 taskENTER_CRITICAL(); \
\r
161 ++( pxQueue->xRxLock ); \
\r
162 ++( pxQueue->xTxLock ); \
\r
163 taskEXIT_CRITICAL(); \
\r
165 /*-----------------------------------------------------------*/
\r
168 /*-----------------------------------------------------------
\r
169 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
170 *----------------------------------------------------------*/
\r
172 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
174 xQUEUE *pxNewQueue;
\r
175 size_t xQueueSizeInBytes;
\r
177 /* Allocate the new queue structure. */
\r
178 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
180 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
181 if( pxNewQueue != NULL )
\r
183 /* Create the list of pointers to queue items. The queue is one byte
\r
184 longer than asked for to make wrap checking easier/faster. */
\r
185 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
187 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
188 if( pxNewQueue->pcHead != NULL )
\r
190 /* Initialise the queue members as described above where the
\r
191 queue type is defined. */
\r
192 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
193 pxNewQueue->uxMessagesWaiting = 0;
\r
194 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
195 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
196 pxNewQueue->uxLength = uxQueueLength;
\r
197 pxNewQueue->uxItemSize = uxItemSize;
\r
198 pxNewQueue->xRxLock = queueUNLOCKED;
\r
199 pxNewQueue->xTxLock = queueUNLOCKED;
\r
201 /* Likewise ensure the event queues start with the correct state. */
\r
202 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
203 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
209 vPortFree( pxNewQueue );
\r
214 /* Will only reach here if we could not allocate enough memory or no memory
\r
218 /*-----------------------------------------------------------*/
\r
220 #if ( configUSE_MUTEXES == 1 )
\r
222 xQueueHandle xQueueCreateMutex( void )
\r
224 xQUEUE *pxNewQueue;
\r
226 /* Allocate the new queue structure. */
\r
227 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
228 if( pxNewQueue != NULL )
\r
230 /* Information required for priority inheritance. */
\r
231 pxNewQueue->pxMutexHolder = NULL;
\r
232 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
234 /* Queues used as a mutex no data is actually copied into or out
\r
236 pxNewQueue->pcWriteTo = NULL;
\r
237 pxNewQueue->pcReadFrom = NULL;
\r
239 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
240 an item size of 0 as nothing is actually copied into or out
\r
242 pxNewQueue->uxMessagesWaiting = 0;
\r
243 pxNewQueue->uxLength = 1;
\r
244 pxNewQueue->uxItemSize = 0;
\r
245 pxNewQueue->xRxLock = queueUNLOCKED;
\r
246 pxNewQueue->xTxLock = queueUNLOCKED;
\r
248 /* Ensure the event queues start with the correct state. */
\r
249 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
250 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
252 /* Start with the semaphore in the expected state. */
\r
253 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
259 #endif /* configUSE_MUTEXES */
\r
260 /*-----------------------------------------------------------*/
\r
262 #if configUSE_COUNTING_SEMAPHORES == 1
\r
264 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
266 xQueueHandle pxHandle;
\r
268 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
270 if( pxHandle != NULL )
\r
272 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
278 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
279 /*-----------------------------------------------------------*/
\r
281 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
283 signed portBASE_TYPE xReturn = pdPASS;
\r
284 xTimeOutType xTimeOut;
\r
286 /* Make sure other tasks do not access the queue. */
\r
289 /* Capture the current time status for future reference. */
\r
290 vTaskSetTimeOutState( &xTimeOut );
\r
292 /* It is important that this is the only thread/ISR that modifies the
\r
293 ready or delayed lists until xTaskResumeAll() is called. Places where
\r
294 the ready/delayed lists are modified include:
\r
296 + vTaskDelay() - Nothing can call vTaskDelay as the scheduler is
\r
297 suspended, vTaskDelay() cannot be called from an ISR.
\r
298 + vTaskPrioritySet() - Has a critical section around the access.
\r
299 + vTaskSwitchContext() - This will not get executed while the scheduler
\r
301 + prvCheckDelayedTasks() - This will not get executed while the
\r
302 scheduler is suspended.
\r
303 + xTaskCreate() - Has a critical section around the access.
\r
304 + vTaskResume() - Has a critical section around the access.
\r
305 + xTaskResumeAll() - Has a critical section around the access.
\r
306 + xTaskRemoveFromEventList - Checks to see if the scheduler is
\r
307 suspended. If so then the TCB being removed from the event is
\r
308 removed from the event and added to the xPendingReadyList.
\r
311 /* Make sure interrupts do not access the queue event list. */
\r
312 prvLockQueue( pxQueue );
\r
314 /* It is important that interrupts to not access the event list of the
\r
315 queue being modified here. Places where the event list is modified
\r
318 + xQueueGenericSendFromISR(). This checks the lock on the queue to see
\r
319 if it has access. If the queue is locked then the Tx lock count is
\r
320 incremented to signify that a task waiting for data can be made ready
\r
321 once the queue lock is removed. If the queue is not locked then
\r
322 a task can be moved from the event list, but will not be removed
\r
323 from the delayed list or placed in the ready list until the scheduler
\r
326 + xQueueReceiveFromISR(). As per xQueueGenericSendFromISR().
\r
329 /* If the queue is already full we may have to block. */
\r
332 if( prvIsQueueFull( pxQueue ) )
\r
334 /* The queue is full - do we want to block or just leave without
\r
336 if( xTicksToWait > ( portTickType ) 0 )
\r
338 /* We are going to place ourselves on the xTasksWaitingToSend event
\r
339 list, and will get woken should the delay expire, or space become
\r
340 available on the queue.
\r
342 As detailed above we do not require mutual exclusion on the event
\r
343 list as nothing else can modify it or the ready lists while we
\r
344 have the scheduler suspended and queue locked.
\r
346 It is possible that an ISR has removed data from the queue since we
\r
347 checked if any was available. If this is the case then the data
\r
348 will have been copied from the queue, and the queue variables
\r
349 updated, but the event list will not yet have been checked to see if
\r
350 anything is waiting as the queue is locked. */
\r
351 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
353 /* Force a context switch now as we are blocked. We can do
\r
354 this from within a critical section as the task we are
\r
355 switching to has its own context. When we return here (i.e. we
\r
356 unblock) we will leave the critical section as normal.
\r
358 It is possible that an ISR has caused an event on an unrelated and
\r
359 unlocked queue. If this was the case then the event list for that
\r
360 queue will have been updated but the ready lists left unchanged -
\r
361 instead the readied task will have been added to the pending ready
\r
363 taskENTER_CRITICAL();
\r
365 /* We can safely unlock the queue and scheduler here as
\r
366 interrupts are disabled. We must not yield with anything
\r
367 locked, but we can yield from within a critical section.
\r
369 Tasks that have been placed on the pending ready list cannot
\r
370 be tasks that are waiting for events on this queue. See
\r
371 in comment xTaskRemoveFromEventList(). */
\r
372 prvUnlockQueue( pxQueue );
\r
374 /* Resuming the scheduler may cause a yield. If so then there
\r
375 is no point yielding again here. */
\r
376 if( !xTaskResumeAll() )
\r
381 /* We want to check to see if the queue is still full
\r
382 before leaving the critical section. This is to prevent
\r
383 this task placing an item into the queue due to an
\r
384 interrupt making space on the queue between critical
\r
385 sections (when there might be a higher priority task
\r
386 blocked on the queue that cannot run yet because the
\r
387 scheduler gets suspended). */
\r
388 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
390 /* We unblocked but there is no space in the queue,
\r
391 we probably timed out. */
\r
392 xReturn = errQUEUE_FULL;
\r
395 /* Before leaving the critical section we have to ensure
\r
396 exclusive access again. */
\r
398 prvLockQueue( pxQueue );
\r
400 taskEXIT_CRITICAL();
\r
404 /* If xReturn is errQUEUE_FULL then we unblocked when the queue
\r
405 was still full. Don't check it again now as it is possible that
\r
406 an interrupt has removed an item from the queue since we left the
\r
407 critical section and we don't want to write to the queue in case
\r
408 there is a task of higher priority blocked waiting for space to
\r
409 be available on the queue. If this is the case the higher priority
\r
410 task will execute when the scheduler is unsupended. */
\r
411 if( xReturn != errQUEUE_FULL )
\r
413 /* When we are here it is possible that we unblocked as space became
\r
414 available on the queue. It is also possible that an ISR posted to the
\r
415 queue since we left the critical section, so it may be that again there
\r
416 is no space. This would only happen if a task and ISR post onto the
\r
418 taskENTER_CRITICAL();
\r
420 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
422 /* There is room in the queue, copy the data into the queue. */
\r
423 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
426 /* Update the TxLock count so prvUnlockQueue knows to check for
\r
427 tasks waiting for data to become available in the queue. */
\r
428 ++( pxQueue->xTxLock );
\r
432 xReturn = errQUEUE_FULL;
\r
435 taskEXIT_CRITICAL();
\r
438 if( xReturn == errQUEUE_FULL )
\r
440 if( xTicksToWait > 0 )
\r
442 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
444 xReturn = queueERRONEOUS_UNBLOCK;
\r
449 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
451 prvUnlockQueue( pxQueue );
\r
456 /*-----------------------------------------------------------*/
\r
458 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )
\r
460 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
461 in the queue. Also we don't directly wake a task that was blocked on a
\r
462 queue read, instead we return a flag to say whether a context switch is
\r
463 required or not (i.e. has a task with a higher priority than us been woken
\r
465 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
467 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
469 /* If the queue is locked we do not alter the event list. This will
\r
470 be done when the queue is unlocked later. */
\r
471 if( pxQueue->xTxLock == queueUNLOCKED )
\r
473 /* We only want to wake one task per ISR, so check that a task has
\r
474 not already been woken. */
\r
475 if( !xTaskPreviouslyWoken )
\r
477 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
479 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
481 /* The task waiting has a higher priority so record that a
\r
482 context switch is required. */
\r
490 /* Increment the lock count so the task that unlocks the queue
\r
491 knows that data was posted while it was locked. */
\r
492 ++( pxQueue->xTxLock );
\r
496 return xTaskPreviouslyWoken;
\r
498 /*-----------------------------------------------------------*/
\r
500 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
502 signed portBASE_TYPE xReturn = pdTRUE;
\r
503 xTimeOutType xTimeOut;
\r
504 signed portCHAR *pcOriginalReadPosition;
\r
506 /* This function is very similar to xQueueGenericSend(). See comments
\r
507 within xQueueGenericSend() for a more detailed explanation.
\r
509 Make sure other tasks do not access the queue. */
\r
512 /* Capture the current time status for future reference. */
\r
513 vTaskSetTimeOutState( &xTimeOut );
\r
515 /* Make sure interrupts do not access the queue. */
\r
516 prvLockQueue( pxQueue );
\r
520 /* If there are no messages in the queue we may have to block. */
\r
521 if( prvIsQueueEmpty( pxQueue ) )
\r
523 /* There are no messages in the queue, do we want to block or just
\r
524 leave with nothing? */
\r
525 if( xTicksToWait > ( portTickType ) 0 )
\r
527 #if ( configUSE_MUTEXES == 1 )
\r
529 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
531 portENTER_CRITICAL();
\r
532 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );
\r
533 portEXIT_CRITICAL();
\r
538 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
539 taskENTER_CRITICAL();
\r
541 prvUnlockQueue( pxQueue );
\r
542 if( !xTaskResumeAll() )
\r
547 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
549 /* We unblocked but the queue is empty. We probably
\r
551 xReturn = errQUEUE_EMPTY;
\r
555 prvLockQueue( pxQueue );
\r
557 taskEXIT_CRITICAL();
\r
561 if( xReturn != errQUEUE_EMPTY )
\r
563 taskENTER_CRITICAL();
\r
565 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
567 /* Remember our read position in case we are just peeking. */
\r
568 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
570 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
572 if( xJustPeeking == pdFALSE )
\r
574 /* We are actually removing data. */
\r
575 --( pxQueue->uxMessagesWaiting );
\r
577 /* Increment the lock count so prvUnlockQueue knows to check for
\r
578 tasks waiting for space to become available on the queue. */
\r
579 ++( pxQueue->xRxLock );
\r
581 #if ( configUSE_MUTEXES == 1 )
\r
583 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
585 /* Record the information required to implement
\r
586 priority inheritance should it become necessary. */
\r
587 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
594 /* We are not removing the data, so reset our read
\r
596 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
598 /* The data is being left in the queue, so increment the
\r
599 lock count so prvUnlockQueue knows to check for other
\r
600 tasks waiting for the data to be available. */
\r
601 ++( pxQueue->xTxLock );
\r
608 xReturn = errQUEUE_EMPTY;
\r
611 taskEXIT_CRITICAL();
\r
614 if( xReturn == errQUEUE_EMPTY )
\r
616 if( xTicksToWait > 0 )
\r
618 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
620 xReturn = queueERRONEOUS_UNBLOCK;
\r
624 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
626 /* We no longer require exclusive access to the queue. */
\r
627 prvUnlockQueue( pxQueue );
\r
632 /*-----------------------------------------------------------*/
\r
634 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
636 signed portBASE_TYPE xReturn;
\r
638 /* We cannot block from an ISR, so check there is data available. */
\r
639 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
641 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
642 --( pxQueue->uxMessagesWaiting );
\r
644 /* If the queue is locked we will not modify the event list. Instead
\r
645 we update the lock count so the task that unlocks the queue will know
\r
646 that an ISR has removed data while the queue was locked. */
\r
647 if( pxQueue->xRxLock == queueUNLOCKED )
\r
649 /* We only want to wake one task per ISR, so check that a task has
\r
650 not already been woken. */
\r
651 if( !( *pxTaskWoken ) )
\r
653 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
655 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
657 /* The task waiting has a higher priority than us so
\r
658 force a context switch. */
\r
659 *pxTaskWoken = pdTRUE;
\r
666 /* Increment the lock count so the task that unlocks the queue
\r
667 knows that data was removed while it was locked. */
\r
668 ++( pxQueue->xRxLock );
\r
680 /*-----------------------------------------------------------*/
\r
682 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
684 unsigned portBASE_TYPE uxReturn;
\r
686 taskENTER_CRITICAL();
\r
687 uxReturn = pxQueue->uxMessagesWaiting;
\r
688 taskEXIT_CRITICAL();
\r
692 /*-----------------------------------------------------------*/
\r
694 void vQueueDelete( xQueueHandle pxQueue )
\r
696 vPortFree( pxQueue->pcHead );
\r
697 vPortFree( pxQueue );
\r
699 /*-----------------------------------------------------------*/
\r
701 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
703 if( pxQueue->uxItemSize == 0 )
\r
705 #if ( configUSE_MUTEXES == 1 )
\r
707 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
709 /* The mutex is no longer being held. */
\r
710 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );
\r
715 else if( xPosition == queueSEND_TO_BACK )
\r
717 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
718 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
719 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
721 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
726 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
727 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
728 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
730 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
734 ++( pxQueue->uxMessagesWaiting );
\r
736 /*-----------------------------------------------------------*/
\r
738 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
740 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
742 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
743 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
745 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
747 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
750 /*-----------------------------------------------------------*/
\r
752 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
754 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
756 /* The lock counts contains the number of extra data items placed or
\r
757 removed from the queue while the queue was locked. When a queue is
\r
758 locked items can be added or removed, but the event lists cannot be
\r
760 taskENTER_CRITICAL();
\r
762 --( pxQueue->xTxLock );
\r
764 /* See if data was added to the queue while it was locked. */
\r
765 if( pxQueue->xTxLock > queueUNLOCKED )
\r
767 pxQueue->xTxLock = queueUNLOCKED;
\r
769 /* Data was posted while the queue was locked. Are any tasks
\r
770 blocked waiting for data to become available? */
\r
771 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
773 /* Tasks that are removed from the event list will get added to
\r
774 the pending ready list as the scheduler is still suspended. */
\r
775 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
777 /* The task waiting has a higher priority so record that a
\r
778 context switch is required. */
\r
779 vTaskMissedYield();
\r
784 taskEXIT_CRITICAL();
\r
786 /* Do the same for the Rx lock. */
\r
787 taskENTER_CRITICAL();
\r
789 --( pxQueue->xRxLock );
\r
791 if( pxQueue->xRxLock > queueUNLOCKED )
\r
793 pxQueue->xRxLock = queueUNLOCKED;
\r
795 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
797 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
799 vTaskMissedYield();
\r
804 taskEXIT_CRITICAL();
\r
806 /*-----------------------------------------------------------*/
\r
808 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
810 signed portBASE_TYPE xReturn;
\r
812 taskENTER_CRITICAL();
\r
813 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
814 taskEXIT_CRITICAL();
\r
818 /*-----------------------------------------------------------*/
\r
820 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
822 signed portBASE_TYPE xReturn;
\r
824 taskENTER_CRITICAL();
\r
825 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
826 taskEXIT_CRITICAL();
\r
830 /*-----------------------------------------------------------*/
\r
832 #if configUSE_CO_ROUTINES == 1
\r
833 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
835 signed portBASE_TYPE xReturn;
\r
837 /* If the queue is already full we may have to block. A critical section
\r
838 is required to prevent an interrupt removing something from the queue
\r
839 between the check to see if the queue is full and blocking on the queue. */
\r
840 portDISABLE_INTERRUPTS();
\r
842 if( prvIsQueueFull( pxQueue ) )
\r
844 /* The queue is full - do we want to block or just leave without
\r
846 if( xTicksToWait > ( portTickType ) 0 )
\r
848 /* As this is called from a coroutine we cannot block directly, but
\r
849 return indicating that we need to block. */
\r
850 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
851 portENABLE_INTERRUPTS();
\r
852 return errQUEUE_BLOCKED;
\r
856 portENABLE_INTERRUPTS();
\r
857 return errQUEUE_FULL;
\r
861 portENABLE_INTERRUPTS();
\r
865 portDISABLE_INTERRUPTS();
\r
867 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
869 /* There is room in the queue, copy the data into the queue. */
\r
870 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
873 /* Were any co-routines waiting for data to become available? */
\r
874 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
876 /* In this instance the co-routine could be placed directly
\r
877 into the ready list as we are within a critical section.
\r
878 Instead the same pending ready list mechanism is used as if
\r
879 the event were caused from within an interrupt. */
\r
880 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
882 /* The co-routine waiting has a higher priority so record
\r
883 that a yield might be appropriate. */
\r
884 xReturn = errQUEUE_YIELD;
\r
890 xReturn = errQUEUE_FULL;
\r
893 portENABLE_INTERRUPTS();
\r
898 /*-----------------------------------------------------------*/
\r
900 #if configUSE_CO_ROUTINES == 1
\r
901 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
903 signed portBASE_TYPE xReturn;
\r
905 /* If the queue is already empty we may have to block. A critical section
\r
906 is required to prevent an interrupt adding something to the queue
\r
907 between the check to see if the queue is empty and blocking on the queue. */
\r
908 portDISABLE_INTERRUPTS();
\r
910 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
912 /* There are no messages in the queue, do we want to block or just
\r
913 leave with nothing? */
\r
914 if( xTicksToWait > ( portTickType ) 0 )
\r
916 /* As this is a co-routine we cannot block directly, but return
\r
917 indicating that we need to block. */
\r
918 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
919 portENABLE_INTERRUPTS();
\r
920 return errQUEUE_BLOCKED;
\r
924 portENABLE_INTERRUPTS();
\r
925 return errQUEUE_FULL;
\r
929 portENABLE_INTERRUPTS();
\r
933 portDISABLE_INTERRUPTS();
\r
935 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
937 /* Data is available from the queue. */
\r
938 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
939 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
941 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
943 --( pxQueue->uxMessagesWaiting );
\r
944 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
948 /* Were any co-routines waiting for space to become available? */
\r
949 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
951 /* In this instance the co-routine could be placed directly
\r
952 into the ready list as we are within a critical section.
\r
953 Instead the same pending ready list mechanism is used as if
\r
954 the event were caused from within an interrupt. */
\r
955 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
957 xReturn = errQUEUE_YIELD;
\r
966 portENABLE_INTERRUPTS();
\r
971 /*-----------------------------------------------------------*/
\r
975 #if configUSE_CO_ROUTINES == 1
\r
976 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
978 /* Cannot block within an ISR so if there is no space on the queue then
\r
979 exit without doing anything. */
\r
980 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
982 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
984 /* We only want to wake one co-routine per ISR, so check that a
\r
985 co-routine has not already been woken. */
\r
986 if( !xCoRoutinePreviouslyWoken )
\r
988 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
990 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
998 return xCoRoutinePreviouslyWoken;
\r
1001 /*-----------------------------------------------------------*/
\r
1003 #if configUSE_CO_ROUTINES == 1
\r
1004 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1006 signed portBASE_TYPE xReturn;
\r
1008 /* We cannot block from an ISR, so check there is data available. If
\r
1009 not then just leave without doing anything. */
\r
1010 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1012 /* Copy the data from the queue. */
\r
1013 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1014 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1016 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1018 --( pxQueue->uxMessagesWaiting );
\r
1019 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1021 if( !( *pxCoRoutineWoken ) )
\r
1023 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1025 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1027 *pxCoRoutineWoken = pdTRUE;
\r
1042 /*-----------------------------------------------------------*/
\r