2 FreeRTOS.org V4.5.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( const 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, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
144 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const 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, const 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
615 /* The data is being left in the queue, so increment the
\r
616 lock count so prvUnlockQueue knows to check for other
\r
617 tasks waiting for the data to be available. */
\r
618 ++( pxQueue->xTxLock );
\r
625 xReturn = errQUEUE_EMPTY;
\r
628 taskEXIT_CRITICAL();
\r
631 if( xReturn == errQUEUE_EMPTY )
\r
633 if( xTicksToWait > 0 )
\r
635 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
637 xReturn = queueERRONEOUS_UNBLOCK;
\r
641 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
643 /* We no longer require exclusive access to the queue. */
\r
644 prvUnlockQueue( pxQueue );
\r
649 /*-----------------------------------------------------------*/
\r
651 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
653 signed portBASE_TYPE xReturn;
\r
655 /* We cannot block from an ISR, so check there is data available. */
\r
656 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
658 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
659 --( pxQueue->uxMessagesWaiting );
\r
661 /* If the queue is locked we will not modify the event list. Instead
\r
662 we update the lock count so the task that unlocks the queue will know
\r
663 that an ISR has removed data while the queue was locked. */
\r
664 if( pxQueue->xRxLock == queueUNLOCKED )
\r
666 /* We only want to wake one task per ISR, so check that a task has
\r
667 not already been woken. */
\r
668 if( !( *pxTaskWoken ) )
\r
670 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
672 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
674 /* The task waiting has a higher priority than us so
\r
675 force a context switch. */
\r
676 *pxTaskWoken = pdTRUE;
\r
683 /* Increment the lock count so the task that unlocks the queue
\r
684 knows that data was removed while it was locked. */
\r
685 ++( pxQueue->xRxLock );
\r
697 /*-----------------------------------------------------------*/
\r
699 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
701 unsigned portBASE_TYPE uxReturn;
\r
703 taskENTER_CRITICAL();
\r
704 uxReturn = pxQueue->uxMessagesWaiting;
\r
705 taskEXIT_CRITICAL();
\r
709 /*-----------------------------------------------------------*/
\r
711 void vQueueDelete( xQueueHandle pxQueue )
\r
713 vPortFree( pxQueue->pcHead );
\r
714 vPortFree( pxQueue );
\r
716 /*-----------------------------------------------------------*/
\r
718 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
720 if( pxQueue->uxItemSize == 0 )
\r
722 #if ( configUSE_MUTEXES == 1 )
\r
724 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
726 /* The mutex is no longer being held. */
\r
727 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );
\r
732 else if( xPosition == queueSEND_TO_BACK )
\r
734 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
735 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
736 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
738 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
743 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
744 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
745 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
747 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
751 ++( pxQueue->uxMessagesWaiting );
\r
753 /*-----------------------------------------------------------*/
\r
755 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
757 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
759 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
760 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
762 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
764 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
767 /*-----------------------------------------------------------*/
\r
769 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
771 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
773 /* The lock counts contains the number of extra data items placed or
\r
774 removed from the queue while the queue was locked. When a queue is
\r
775 locked items can be added or removed, but the event lists cannot be
\r
777 taskENTER_CRITICAL();
\r
779 --( pxQueue->xTxLock );
\r
781 /* See if data was added to the queue while it was locked. */
\r
782 if( pxQueue->xTxLock > queueUNLOCKED )
\r
784 pxQueue->xTxLock = queueUNLOCKED;
\r
786 /* Data was posted while the queue was locked. Are any tasks
\r
787 blocked waiting for data to become available? */
\r
788 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
790 /* Tasks that are removed from the event list will get added to
\r
791 the pending ready list as the scheduler is still suspended. */
\r
792 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
794 /* The task waiting has a higher priority so record that a
\r
795 context switch is required. */
\r
796 vTaskMissedYield();
\r
801 taskEXIT_CRITICAL();
\r
803 /* Do the same for the Rx lock. */
\r
804 taskENTER_CRITICAL();
\r
806 --( pxQueue->xRxLock );
\r
808 if( pxQueue->xRxLock > queueUNLOCKED )
\r
810 pxQueue->xRxLock = queueUNLOCKED;
\r
812 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
814 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
816 vTaskMissedYield();
\r
821 taskEXIT_CRITICAL();
\r
823 /*-----------------------------------------------------------*/
\r
825 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
827 signed portBASE_TYPE xReturn;
\r
829 taskENTER_CRITICAL();
\r
830 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
831 taskEXIT_CRITICAL();
\r
835 /*-----------------------------------------------------------*/
\r
837 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
839 signed portBASE_TYPE xReturn;
\r
841 taskENTER_CRITICAL();
\r
842 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
843 taskEXIT_CRITICAL();
\r
847 /*-----------------------------------------------------------*/
\r
849 #if configUSE_CO_ROUTINES == 1
\r
850 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
852 signed portBASE_TYPE xReturn;
\r
854 /* If the queue is already full we may have to block. A critical section
\r
855 is required to prevent an interrupt removing something from the queue
\r
856 between the check to see if the queue is full and blocking on the queue. */
\r
857 portDISABLE_INTERRUPTS();
\r
859 if( prvIsQueueFull( pxQueue ) )
\r
861 /* The queue is full - do we want to block or just leave without
\r
863 if( xTicksToWait > ( portTickType ) 0 )
\r
865 /* As this is called from a coroutine we cannot block directly, but
\r
866 return indicating that we need to block. */
\r
867 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
868 portENABLE_INTERRUPTS();
\r
869 return errQUEUE_BLOCKED;
\r
873 portENABLE_INTERRUPTS();
\r
874 return errQUEUE_FULL;
\r
878 portENABLE_INTERRUPTS();
\r
882 portDISABLE_INTERRUPTS();
\r
884 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
886 /* There is room in the queue, copy the data into the queue. */
\r
887 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
890 /* Were any co-routines waiting for data to become available? */
\r
891 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
893 /* In this instance the co-routine could be placed directly
\r
894 into the ready list as we are within a critical section.
\r
895 Instead the same pending ready list mechanism is used as if
\r
896 the event were caused from within an interrupt. */
\r
897 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
899 /* The co-routine waiting has a higher priority so record
\r
900 that a yield might be appropriate. */
\r
901 xReturn = errQUEUE_YIELD;
\r
907 xReturn = errQUEUE_FULL;
\r
910 portENABLE_INTERRUPTS();
\r
915 /*-----------------------------------------------------------*/
\r
917 #if configUSE_CO_ROUTINES == 1
\r
918 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
920 signed portBASE_TYPE xReturn;
\r
922 /* If the queue is already empty we may have to block. A critical section
\r
923 is required to prevent an interrupt adding something to the queue
\r
924 between the check to see if the queue is empty and blocking on the queue. */
\r
925 portDISABLE_INTERRUPTS();
\r
927 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
929 /* There are no messages in the queue, do we want to block or just
\r
930 leave with nothing? */
\r
931 if( xTicksToWait > ( portTickType ) 0 )
\r
933 /* As this is a co-routine we cannot block directly, but return
\r
934 indicating that we need to block. */
\r
935 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
936 portENABLE_INTERRUPTS();
\r
937 return errQUEUE_BLOCKED;
\r
941 portENABLE_INTERRUPTS();
\r
942 return errQUEUE_FULL;
\r
946 portENABLE_INTERRUPTS();
\r
950 portDISABLE_INTERRUPTS();
\r
952 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
954 /* Data is available from the queue. */
\r
955 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
956 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
958 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
960 --( pxQueue->uxMessagesWaiting );
\r
961 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
965 /* Were any co-routines waiting for space to become available? */
\r
966 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
968 /* In this instance the co-routine could be placed directly
\r
969 into the ready list as we are within a critical section.
\r
970 Instead the same pending ready list mechanism is used as if
\r
971 the event were caused from within an interrupt. */
\r
972 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
974 xReturn = errQUEUE_YIELD;
\r
983 portENABLE_INTERRUPTS();
\r
988 /*-----------------------------------------------------------*/
\r
992 #if configUSE_CO_ROUTINES == 1
\r
993 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
995 /* Cannot block within an ISR so if there is no space on the queue then
\r
996 exit without doing anything. */
\r
997 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
999 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1001 /* We only want to wake one co-routine per ISR, so check that a
\r
1002 co-routine has not already been woken. */
\r
1003 if( !xCoRoutinePreviouslyWoken )
\r
1005 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1007 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1015 return xCoRoutinePreviouslyWoken;
\r
1018 /*-----------------------------------------------------------*/
\r
1020 #if configUSE_CO_ROUTINES == 1
\r
1021 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1023 signed portBASE_TYPE xReturn;
\r
1025 /* We cannot block from an ISR, so check there is data available. If
\r
1026 not then just leave without doing anything. */
\r
1027 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1029 /* Copy the data from the queue. */
\r
1030 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1031 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1033 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1035 --( pxQueue->uxMessagesWaiting );
\r
1036 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1038 if( !( *pxCoRoutineWoken ) )
\r
1040 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1042 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1044 *pxCoRoutineWoken = pdTRUE;
\r
1059 /*-----------------------------------------------------------*/
\r