2 FreeRTOS.org V4.4.0 - 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 for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 + More use of 8bit data types.
\r
40 + Function name prefixes changed where the data type returned has changed.
\r
44 + Added the queue locking mechanism and make more use of the scheduler
\r
45 suspension feature to minimise the time interrupts have to be disabled
\r
46 when accessing a queue.
\r
50 + Explicit use of 'signed' qualifier on portCHAR types added.
\r
54 + API changes as described on the FreeRTOS.org WEB site.
\r
58 + Added the queue functions that can be used from co-routines.
\r
62 + Added a loop within xQueueSend() and xQueueReceive() to prevent the
\r
63 functions exiting when a block time remains and the function has
\r
66 Changes from V4.1.2:
\r
68 + BUG FIX: Removed the call to prvIsQueueEmpty from within xQueueCRReceive
\r
69 as it exited with interrupts enabled. Thanks Paul Katz.
\r
71 Changes from V4.1.3:
\r
73 + Modified xQueueSend() and xQueueReceive() to handle the (very unlikely)
\r
74 case whereby a task unblocking due to a temporal event can remove/send an
\r
75 item from/to a queue when a higher priority task is still blocked on the
\r
76 queue. This modification is a result of the SafeRTOS testing.
\r
81 #include "FreeRTOS.h"
\r
83 #include "croutine.h"
\r
85 /*-----------------------------------------------------------
\r
86 * PUBLIC LIST API documented in list.h
\r
87 *----------------------------------------------------------*/
\r
89 /* Constants used with the cRxLock and cTxLock structure members. */
\r
90 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
91 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
93 /* For internal use only. */
\r
94 #define queueSEND_TO_BACK ( 0 )
\r
95 #define queueSEND_TO_FRONT ( 1 )
\r
97 /* Effectively make a union out of the xQUEUE structure. */
\r
98 #define pxMutexHolder pcTail
\r
99 #define uxQueueType pcHead
\r
100 #define queueQUEUE_IS_MUTEX NULL
\r
103 * Definition of the queue used by the scheduler.
\r
104 * Items are queued by copy, not reference.
\r
106 typedef struct QueueDefinition
\r
108 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
109 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
111 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
112 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
114 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
115 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
117 unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
118 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
119 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
121 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
122 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
124 /*-----------------------------------------------------------*/
\r
127 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
128 * To keep the definition private the API header file defines it as a
\r
131 typedef xQUEUE * xQueueHandle;
\r
134 * Prototypes for public functions are included here so we don't have to
\r
135 * include the API header file (as it defines xQueueHandle differently). These
\r
136 * functions are documented in the API header file.
\r
138 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
139 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
140 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue );
\r
141 void vQueueDelete( xQueueHandle xQueue );
\r
142 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition );
\r
143 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
144 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
145 xQueueHandle xQueueCreateMutex( void );
\r
147 #if configUSE_CO_ROUTINES == 1
\r
148 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
149 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
150 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
151 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
155 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
156 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
157 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
158 * queue is locked it will instead increment the appropriate queue lock count
\r
159 * to indicate that a task may require unblocking. When the queue in unlocked
\r
160 * these lock counts are inspected, and the appropriate action taken.
\r
162 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
165 * Uses a critical section to determine if there is any data in a queue.
\r
167 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
169 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
172 * Uses a critical section to determine if there is any space in a queue.
\r
174 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
176 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
179 * Copies an item into the queue, either at the front of the queue or the
\r
180 * back of the queue.
\r
182 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
185 * Copies an item out of a queue.
\r
187 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
188 /*-----------------------------------------------------------*/
\r
191 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
192 * accessing the queue event lists.
\r
194 #define prvLockQueue( pxQueue ) \
\r
196 taskENTER_CRITICAL(); \
\r
197 ++( pxQueue->xRxLock ); \
\r
198 ++( pxQueue->xTxLock ); \
\r
199 taskEXIT_CRITICAL(); \
\r
201 /*-----------------------------------------------------------*/
\r
204 /*-----------------------------------------------------------
\r
205 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
206 *----------------------------------------------------------*/
\r
208 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
210 xQUEUE *pxNewQueue;
\r
211 size_t xQueueSizeInBytes;
\r
213 /* Allocate the new queue structure. */
\r
214 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
216 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
217 if( pxNewQueue != NULL )
\r
219 /* Create the list of pointers to queue items. The queue is one byte
\r
220 longer than asked for to make wrap checking easier/faster. */
\r
221 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
223 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
224 if( pxNewQueue->pcHead != NULL )
\r
226 /* Initialise the queue members as described above where the
\r
227 queue type is defined. */
\r
228 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
229 pxNewQueue->uxMessagesWaiting = 0;
\r
230 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
231 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
232 pxNewQueue->uxLength = uxQueueLength;
\r
233 pxNewQueue->uxItemSize = uxItemSize;
\r
234 pxNewQueue->xRxLock = queueUNLOCKED;
\r
235 pxNewQueue->xTxLock = queueUNLOCKED;
\r
237 /* Likewise ensure the event queues start with the correct state. */
\r
238 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
239 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
245 vPortFree( pxNewQueue );
\r
250 /* Will only reach here if we could not allocate enough memory or no memory
\r
254 /*-----------------------------------------------------------*/
\r
256 #if ( configUSE_MUTEXES == 1 )
\r
258 xQueueHandle xQueueCreateMutex( void )
\r
260 xQUEUE *pxNewQueue;
\r
262 /* Allocate the new queue structure. */
\r
263 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
264 if( pxNewQueue != NULL )
\r
266 /* Information required for priority inheritance. */
\r
267 pxNewQueue->pxMutexHolder = NULL;
\r
268 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
270 /* Queues used as a mutex no data is actually copied into or out
\r
272 pxNewQueue->pcWriteTo = NULL;
\r
273 pxNewQueue->pcReadFrom = NULL;
\r
275 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
276 an item size of 0 as nothing is actually copied into or out
\r
278 pxNewQueue->uxMessagesWaiting = 0;
\r
279 pxNewQueue->uxLength = 1;
\r
280 pxNewQueue->uxItemSize = 0;
\r
281 pxNewQueue->xRxLock = queueUNLOCKED;
\r
282 pxNewQueue->xTxLock = queueUNLOCKED;
\r
284 /* Ensure the event queues start with the correct state. */
\r
285 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
286 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
288 /* Start with the semaphore in the expected state. */
\r
289 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
295 #endif /* configUSE_MUTEXES */
\r
296 /*-----------------------------------------------------------*/
\r
298 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
300 signed portBASE_TYPE xReturn = pdPASS;
\r
301 xTimeOutType xTimeOut;
\r
303 /* Make sure other tasks do not access the queue. */
\r
306 /* Capture the current time status for future reference. */
\r
307 vTaskSetTimeOutState( &xTimeOut );
\r
309 /* It is important that this is the only thread/ISR that modifies the
\r
310 ready or delayed lists until xTaskResumeAll() is called. Places where
\r
311 the ready/delayed lists are modified include:
\r
313 + vTaskDelay() - Nothing can call vTaskDelay as the scheduler is
\r
314 suspended, vTaskDelay() cannot be called from an ISR.
\r
315 + vTaskPrioritySet() - Has a critical section around the access.
\r
316 + vTaskSwitchContext() - This will not get executed while the scheduler
\r
318 + prvCheckDelayedTasks() - This will not get executed while the
\r
319 scheduler is suspended.
\r
320 + xTaskCreate() - Has a critical section around the access.
\r
321 + vTaskResume() - Has a critical section around the access.
\r
322 + xTaskResumeAll() - Has a critical section around the access.
\r
323 + xTaskRemoveFromEventList - Checks to see if the scheduler is
\r
324 suspended. If so then the TCB being removed from the event is
\r
325 removed from the event and added to the xPendingReadyList.
\r
328 /* Make sure interrupts do not access the queue event list. */
\r
329 prvLockQueue( pxQueue );
\r
331 /* It is important that interrupts to not access the event list of the
\r
332 queue being modified here. Places where the event list is modified
\r
335 + xQueueGenericSendFromISR(). This checks the lock on the queue to see
\r
336 if it has access. If the queue is locked then the Tx lock count is
\r
337 incremented to signify that a task waiting for data can be made ready
\r
338 once the queue lock is removed. If the queue is not locked then
\r
339 a task can be moved from the event list, but will not be removed
\r
340 from the delayed list or placed in the ready list until the scheduler
\r
343 + xQueueReceiveFromISR(). As per xQueueGenericSendFromISR().
\r
346 /* If the queue is already full we may have to block. */
\r
349 if( prvIsQueueFull( pxQueue ) )
\r
351 /* The queue is full - do we want to block or just leave without
\r
353 if( xTicksToWait > ( portTickType ) 0 )
\r
355 /* We are going to place ourselves on the xTasksWaitingToSend event
\r
356 list, and will get woken should the delay expire, or space become
\r
357 available on the queue.
\r
359 As detailed above we do not require mutual exclusion on the event
\r
360 list as nothing else can modify it or the ready lists while we
\r
361 have the scheduler suspended and queue locked.
\r
363 It is possible that an ISR has removed data from the queue since we
\r
364 checked if any was available. If this is the case then the data
\r
365 will have been copied from the queue, and the queue variables
\r
366 updated, but the event list will not yet have been checked to see if
\r
367 anything is waiting as the queue is locked. */
\r
368 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
370 /* Force a context switch now as we are blocked. We can do
\r
371 this from within a critical section as the task we are
\r
372 switching to has its own context. When we return here (i.e. we
\r
373 unblock) we will leave the critical section as normal.
\r
375 It is possible that an ISR has caused an event on an unrelated and
\r
376 unlocked queue. If this was the case then the event list for that
\r
377 queue will have been updated but the ready lists left unchanged -
\r
378 instead the readied task will have been added to the pending ready
\r
380 taskENTER_CRITICAL();
\r
382 /* We can safely unlock the queue and scheduler here as
\r
383 interrupts are disabled. We must not yield with anything
\r
384 locked, but we can yield from within a critical section.
\r
386 Tasks that have been placed on the pending ready list cannot
\r
387 be tasks that are waiting for events on this queue. See
\r
388 in comment xTaskRemoveFromEventList(). */
\r
389 prvUnlockQueue( pxQueue );
\r
391 /* Resuming the scheduler may cause a yield. If so then there
\r
392 is no point yielding again here. */
\r
393 if( !xTaskResumeAll() )
\r
398 /* We want to check to see if the queue is still full
\r
399 before leaving the critical section. This is to prevent
\r
400 this task placing an item into the queue due to an
\r
401 interrupt making space on the queue between critical
\r
402 sections (when there might be a higher priority task
\r
403 blocked on the queue that cannot run yet because the
\r
404 scheduler gets suspended). */
\r
405 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
407 /* We unblocked but there is no space in the queue,
\r
408 we probably timed out. */
\r
409 xReturn = errQUEUE_FULL;
\r
412 /* Before leaving the critical section we have to ensure
\r
413 exclusive access again. */
\r
415 prvLockQueue( pxQueue );
\r
417 taskEXIT_CRITICAL();
\r
421 /* If xReturn is errQUEUE_FULL then we unblocked when the queue
\r
422 was still full. Don't check it again now as it is possible that
\r
423 an interrupt has removed an item from the queue since we left the
\r
424 critical section and we don't want to write to the queue in case
\r
425 there is a task of higher priority blocked waiting for space to
\r
426 be available on the queue. If this is the case the higher priority
\r
427 task will execute when the scheduler is unsupended. */
\r
428 if( xReturn != errQUEUE_FULL )
\r
430 /* When we are here it is possible that we unblocked as space became
\r
431 available on the queue. It is also possible that an ISR posted to the
\r
432 queue since we left the critical section, so it may be that again there
\r
433 is no space. This would only happen if a task and ISR post onto the
\r
435 taskENTER_CRITICAL();
\r
437 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
439 /* There is room in the queue, copy the data into the queue. */
\r
440 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
443 /* Update the TxLock count so prvUnlockQueue knows to check for
\r
444 tasks waiting for data to become available in the queue. */
\r
445 ++( pxQueue->xTxLock );
\r
449 xReturn = errQUEUE_FULL;
\r
452 taskEXIT_CRITICAL();
\r
455 if( xReturn == errQUEUE_FULL )
\r
457 if( xTicksToWait > 0 )
\r
459 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
461 xReturn = queueERRONEOUS_UNBLOCK;
\r
466 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
468 prvUnlockQueue( pxQueue );
\r
473 /*-----------------------------------------------------------*/
\r
475 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )
\r
477 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
478 in the queue. Also we don't directly wake a task that was blocked on a
\r
479 queue read, instead we return a flag to say whether a context switch is
\r
480 required or not (i.e. has a task with a higher priority than us been woken
\r
482 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
484 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
486 /* If the queue is locked we do not alter the event list. This will
\r
487 be done when the queue is unlocked later. */
\r
488 if( pxQueue->xTxLock == queueUNLOCKED )
\r
490 /* We only want to wake one task per ISR, so check that a task has
\r
491 not already been woken. */
\r
492 if( !xTaskPreviouslyWoken )
\r
494 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
496 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
498 /* The task waiting has a higher priority so record that a
\r
499 context switch is required. */
\r
507 /* Increment the lock count so the task that unlocks the queue
\r
508 knows that data was posted while it was locked. */
\r
509 ++( pxQueue->xTxLock );
\r
513 return xTaskPreviouslyWoken;
\r
515 /*-----------------------------------------------------------*/
\r
517 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
519 signed portBASE_TYPE xReturn = pdTRUE;
\r
520 xTimeOutType xTimeOut;
\r
521 signed portCHAR *pcOriginalReadPosition;
\r
523 /* This function is very similar to xQueueGenericSend(). See comments
\r
524 within xQueueGenericSend() for a more detailed explanation.
\r
526 Make sure other tasks do not access the queue. */
\r
529 /* Capture the current time status for future reference. */
\r
530 vTaskSetTimeOutState( &xTimeOut );
\r
532 /* Make sure interrupts do not access the queue. */
\r
533 prvLockQueue( pxQueue );
\r
537 /* If there are no messages in the queue we may have to block. */
\r
538 if( prvIsQueueEmpty( pxQueue ) )
\r
540 /* There are no messages in the queue, do we want to block or just
\r
541 leave with nothing? */
\r
542 if( xTicksToWait > ( portTickType ) 0 )
\r
544 #if ( configUSE_MUTEXES == 1 )
\r
546 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
548 portENTER_CRITICAL();
\r
549 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );
\r
550 portEXIT_CRITICAL();
\r
555 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
556 taskENTER_CRITICAL();
\r
558 prvUnlockQueue( pxQueue );
\r
559 if( !xTaskResumeAll() )
\r
564 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
566 /* We unblocked but the queue is empty. We probably
\r
568 xReturn = errQUEUE_EMPTY;
\r
572 prvLockQueue( pxQueue );
\r
574 taskEXIT_CRITICAL();
\r
578 if( xReturn != errQUEUE_EMPTY )
\r
580 taskENTER_CRITICAL();
\r
582 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
584 /* Remember our read position in case we are just peeking. */
\r
585 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
587 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
589 if( xJustPeeking == pdFALSE )
\r
591 /* We are actually removing data. */
\r
592 --( pxQueue->uxMessagesWaiting );
\r
594 /* Increment the lock count so prvUnlockQueue knows to check for
\r
595 tasks waiting for space to become available on the queue. */
\r
596 ++( pxQueue->xRxLock );
\r
598 #if ( configUSE_MUTEXES == 1 )
\r
600 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
602 /* Record the information required to implement
\r
603 priority inheritance should it become necessary. */
\r
604 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
611 /* We are not removing the data, so reset our read
\r
613 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
620 xReturn = errQUEUE_EMPTY;
\r
623 taskEXIT_CRITICAL();
\r
626 if( xReturn == errQUEUE_EMPTY )
\r
628 if( xTicksToWait > 0 )
\r
630 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
632 xReturn = queueERRONEOUS_UNBLOCK;
\r
636 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
638 /* We no longer require exclusive access to the queue. */
\r
639 prvUnlockQueue( pxQueue );
\r
644 /*-----------------------------------------------------------*/
\r
646 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
648 signed portBASE_TYPE xReturn;
\r
650 /* We cannot block from an ISR, so check there is data available. */
\r
651 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
653 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
654 --( pxQueue->uxMessagesWaiting );
\r
656 /* If the queue is locked we will not modify the event list. Instead
\r
657 we update the lock count so the task that unlocks the queue will know
\r
658 that an ISR has removed data while the queue was locked. */
\r
659 if( pxQueue->xRxLock == queueUNLOCKED )
\r
661 /* We only want to wake one task per ISR, so check that a task has
\r
662 not already been woken. */
\r
663 if( !( *pxTaskWoken ) )
\r
665 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
667 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
669 /* The task waiting has a higher priority than us so
\r
670 force a context switch. */
\r
671 *pxTaskWoken = pdTRUE;
\r
678 /* Increment the lock count so the task that unlocks the queue
\r
679 knows that data was removed while it was locked. */
\r
680 ++( pxQueue->xRxLock );
\r
692 /*-----------------------------------------------------------*/
\r
694 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue )
\r
696 unsigned portBASE_TYPE uxReturn;
\r
698 taskENTER_CRITICAL();
\r
699 uxReturn = pxQueue->uxMessagesWaiting;
\r
700 taskEXIT_CRITICAL();
\r
704 /*-----------------------------------------------------------*/
\r
706 void vQueueDelete( xQueueHandle pxQueue )
\r
708 vPortFree( pxQueue->pcHead );
\r
709 vPortFree( pxQueue );
\r
711 /*-----------------------------------------------------------*/
\r
713 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
715 if( pxQueue->uxItemSize == 0 )
\r
717 #if ( configUSE_MUTEXES == 1 )
\r
719 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
721 /* The mutex is no longer being held. */
\r
722 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );
\r
727 else if( xPosition == queueSEND_TO_BACK )
\r
729 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
730 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
731 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
733 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
738 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
739 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
740 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
742 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
746 ++( pxQueue->uxMessagesWaiting );
\r
748 /*-----------------------------------------------------------*/
\r
750 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
752 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
754 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
755 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
757 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
759 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
762 /*-----------------------------------------------------------*/
\r
764 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
766 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
768 /* The lock counts contains the number of extra data items placed or
\r
769 removed from the queue while the queue was locked. When a queue is
\r
770 locked items can be added or removed, but the event lists cannot be
\r
772 taskENTER_CRITICAL();
\r
774 --( pxQueue->xTxLock );
\r
776 /* See if data was added to the queue while it was locked. */
\r
777 if( pxQueue->xTxLock > queueUNLOCKED )
\r
779 pxQueue->xTxLock = queueUNLOCKED;
\r
781 /* Data was posted while the queue was locked. Are any tasks
\r
782 blocked waiting for data to become available? */
\r
783 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
785 /* Tasks that are removed from the event list will get added to
\r
786 the pending ready list as the scheduler is still suspended. */
\r
787 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
789 /* The task waiting has a higher priority so record that a
\r
790 context switch is required. */
\r
791 vTaskMissedYield();
\r
796 taskEXIT_CRITICAL();
\r
798 /* Do the same for the Rx lock. */
\r
799 taskENTER_CRITICAL();
\r
801 --( pxQueue->xRxLock );
\r
803 if( pxQueue->xRxLock > queueUNLOCKED )
\r
805 pxQueue->xRxLock = queueUNLOCKED;
\r
807 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
809 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
811 vTaskMissedYield();
\r
816 taskEXIT_CRITICAL();
\r
818 /*-----------------------------------------------------------*/
\r
820 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
822 signed portBASE_TYPE xReturn;
\r
824 taskENTER_CRITICAL();
\r
825 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
826 taskEXIT_CRITICAL();
\r
830 /*-----------------------------------------------------------*/
\r
832 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
834 signed portBASE_TYPE xReturn;
\r
836 taskENTER_CRITICAL();
\r
837 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
838 taskEXIT_CRITICAL();
\r
842 /*-----------------------------------------------------------*/
\r
844 #if configUSE_CO_ROUTINES == 1
\r
845 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
847 signed portBASE_TYPE xReturn;
\r
849 /* If the queue is already full we may have to block. A critical section
\r
850 is required to prevent an interrupt removing something from the queue
\r
851 between the check to see if the queue is full and blocking on the queue. */
\r
852 portDISABLE_INTERRUPTS();
\r
854 if( prvIsQueueFull( pxQueue ) )
\r
856 /* The queue is full - do we want to block or just leave without
\r
858 if( xTicksToWait > ( portTickType ) 0 )
\r
860 /* As this is called from a coroutine we cannot block directly, but
\r
861 return indicating that we need to block. */
\r
862 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
863 portENABLE_INTERRUPTS();
\r
864 return errQUEUE_BLOCKED;
\r
868 portENABLE_INTERRUPTS();
\r
869 return errQUEUE_FULL;
\r
873 portENABLE_INTERRUPTS();
\r
877 portDISABLE_INTERRUPTS();
\r
879 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
881 /* There is room in the queue, copy the data into the queue. */
\r
882 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
885 /* Were any co-routines waiting for data to become available? */
\r
886 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
888 /* In this instance the co-routine could be placed directly
\r
889 into the ready list as we are within a critical section.
\r
890 Instead the same pending ready list mechanism is used as if
\r
891 the event were caused from within an interrupt. */
\r
892 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
894 /* The co-routine waiting has a higher priority so record
\r
895 that a yield might be appropriate. */
\r
896 xReturn = errQUEUE_YIELD;
\r
902 xReturn = errQUEUE_FULL;
\r
905 portENABLE_INTERRUPTS();
\r
910 /*-----------------------------------------------------------*/
\r
912 #if configUSE_CO_ROUTINES == 1
\r
913 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
915 signed portBASE_TYPE xReturn;
\r
917 /* If the queue is already empty we may have to block. A critical section
\r
918 is required to prevent an interrupt adding something to the queue
\r
919 between the check to see if the queue is empty and blocking on the queue. */
\r
920 portDISABLE_INTERRUPTS();
\r
922 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
924 /* There are no messages in the queue, do we want to block or just
\r
925 leave with nothing? */
\r
926 if( xTicksToWait > ( portTickType ) 0 )
\r
928 /* As this is a co-routine we cannot block directly, but return
\r
929 indicating that we need to block. */
\r
930 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
931 portENABLE_INTERRUPTS();
\r
932 return errQUEUE_BLOCKED;
\r
936 portENABLE_INTERRUPTS();
\r
937 return errQUEUE_FULL;
\r
941 portENABLE_INTERRUPTS();
\r
945 portDISABLE_INTERRUPTS();
\r
947 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
949 /* Data is available from the queue. */
\r
950 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
951 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
953 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
955 --( pxQueue->uxMessagesWaiting );
\r
956 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
960 /* Were any co-routines waiting for space to become available? */
\r
961 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
963 /* In this instance the co-routine could be placed directly
\r
964 into the ready list as we are within a critical section.
\r
965 Instead the same pending ready list mechanism is used as if
\r
966 the event were caused from within an interrupt. */
\r
967 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
969 xReturn = errQUEUE_YIELD;
\r
978 portENABLE_INTERRUPTS();
\r
983 /*-----------------------------------------------------------*/
\r
987 #if configUSE_CO_ROUTINES == 1
\r
988 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
990 /* Cannot block within an ISR so if there is no space on the queue then
\r
991 exit without doing anything. */
\r
992 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
994 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
996 /* We only want to wake one co-routine per ISR, so check that a
\r
997 co-routine has not already been woken. */
\r
998 if( !xCoRoutinePreviouslyWoken )
\r
1000 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1002 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1010 return xCoRoutinePreviouslyWoken;
\r
1013 /*-----------------------------------------------------------*/
\r
1015 #if configUSE_CO_ROUTINES == 1
\r
1016 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1018 signed portBASE_TYPE xReturn;
\r
1020 /* We cannot block from an ISR, so check there is data available. If
\r
1021 not then just leave without doing anything. */
\r
1022 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1024 /* Copy the data from the queue. */
\r
1025 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1026 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1028 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1030 --( pxQueue->uxMessagesWaiting );
\r
1031 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1033 if( !( *pxCoRoutineWoken ) )
\r
1035 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1037 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1039 *pxCoRoutineWoken = pdTRUE;
\r
1054 /*-----------------------------------------------------------*/
\r