2 FreeRTOS.org V4.7.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 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 volatile 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
110 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
\r
111 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
\r
113 #if configUSE_CO_ROUTINES == 1
\r
114 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
115 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
116 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
117 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
121 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
122 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
123 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
124 * queue is locked it will instead increment the appropriate queue lock count
\r
125 * to indicate that a task may require unblocking. When the queue in unlocked
\r
126 * these lock counts are inspected, and the appropriate action taken.
\r
128 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
131 * Uses a critical section to determine if there is any data in a queue.
\r
133 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
135 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
138 * Uses a critical section to determine if there is any space in a queue.
\r
140 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
142 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
145 * Copies an item into the queue, either at the front of the queue or the
\r
146 * back of the queue.
\r
148 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );
\r
151 * Copies an item out of a queue.
\r
153 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );
\r
154 /*-----------------------------------------------------------*/
\r
157 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
158 * accessing the queue event lists.
\r
160 #define prvLockQueue( pxQueue ) \
\r
162 taskENTER_CRITICAL(); \
\r
163 ++( pxQueue->xRxLock ); \
\r
164 ++( pxQueue->xTxLock ); \
\r
165 taskEXIT_CRITICAL(); \
\r
167 /*-----------------------------------------------------------*/
\r
170 /*-----------------------------------------------------------
\r
171 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
172 *----------------------------------------------------------*/
\r
174 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
176 xQUEUE *pxNewQueue;
\r
177 size_t xQueueSizeInBytes;
\r
179 /* Allocate the new queue structure. */
\r
180 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
182 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
183 if( pxNewQueue != NULL )
\r
185 /* Create the list of pointers to queue items. The queue is one byte
\r
186 longer than asked for to make wrap checking easier/faster. */
\r
187 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
189 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
190 if( pxNewQueue->pcHead != NULL )
\r
192 /* Initialise the queue members as described above where the
\r
193 queue type is defined. */
\r
194 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
195 pxNewQueue->uxMessagesWaiting = 0;
\r
196 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
197 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
198 pxNewQueue->uxLength = uxQueueLength;
\r
199 pxNewQueue->uxItemSize = uxItemSize;
\r
200 pxNewQueue->xRxLock = queueUNLOCKED;
\r
201 pxNewQueue->xTxLock = queueUNLOCKED;
\r
203 /* Likewise ensure the event queues start with the correct state. */
\r
204 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
205 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
211 vPortFree( pxNewQueue );
\r
216 /* Will only reach here if we could not allocate enough memory or no memory
\r
220 /*-----------------------------------------------------------*/
\r
222 #if ( configUSE_MUTEXES == 1 )
\r
224 xQueueHandle xQueueCreateMutex( void )
\r
226 xQUEUE *pxNewQueue;
\r
228 /* Allocate the new queue structure. */
\r
229 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
230 if( pxNewQueue != NULL )
\r
232 /* Information required for priority inheritance. */
\r
233 pxNewQueue->pxMutexHolder = NULL;
\r
234 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
236 /* Queues used as a mutex no data is actually copied into or out
\r
238 pxNewQueue->pcWriteTo = NULL;
\r
239 pxNewQueue->pcReadFrom = NULL;
\r
241 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
242 an item size of 0 as nothing is actually copied into or out
\r
244 pxNewQueue->uxMessagesWaiting = 0;
\r
245 pxNewQueue->uxLength = 1;
\r
246 pxNewQueue->uxItemSize = 0;
\r
247 pxNewQueue->xRxLock = queueUNLOCKED;
\r
248 pxNewQueue->xTxLock = queueUNLOCKED;
\r
250 /* Ensure the event queues start with the correct state. */
\r
251 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
252 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
254 /* Start with the semaphore in the expected state. */
\r
255 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
261 #endif /* configUSE_MUTEXES */
\r
262 /*-----------------------------------------------------------*/
\r
264 #if configUSE_COUNTING_SEMAPHORES == 1
\r
266 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
268 xQueueHandle pxHandle;
\r
270 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
272 if( pxHandle != NULL )
\r
274 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
280 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
281 /*-----------------------------------------------------------*/
\r
283 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
285 signed portBASE_TYPE xReturn = pdPASS;
\r
286 xTimeOutType xTimeOut;
\r
288 /* Make sure other tasks do not access the queue. */
\r
291 /* Capture the current time status for future reference. */
\r
292 vTaskSetTimeOutState( &xTimeOut );
\r
294 /* It is important that this is the only thread/ISR that modifies the
\r
295 ready or delayed lists until xTaskResumeAll() is called. Places where
\r
296 the ready/delayed lists are modified include:
\r
298 + vTaskDelay() - Nothing can call vTaskDelay as the scheduler is
\r
299 suspended, vTaskDelay() cannot be called from an ISR.
\r
300 + vTaskPrioritySet() - Has a critical section around the access.
\r
301 + vTaskSwitchContext() - This will not get executed while the scheduler
\r
303 + prvCheckDelayedTasks() - This will not get executed while the
\r
304 scheduler is suspended.
\r
305 + xTaskCreate() - Has a critical section around the access.
\r
306 + vTaskResume() - Has a critical section around the access.
\r
307 + xTaskResumeAll() - Has a critical section around the access.
\r
308 + xTaskRemoveFromEventList - Checks to see if the scheduler is
\r
309 suspended. If so then the TCB being removed from the event is
\r
310 removed from the event and added to the xPendingReadyList.
\r
313 /* Make sure interrupts do not access the queue event list. */
\r
314 prvLockQueue( pxQueue );
\r
316 /* It is important that interrupts to not access the event list of the
\r
317 queue being modified here. Places where the event list is modified
\r
320 + xQueueGenericSendFromISR(). This checks the lock on the queue to see
\r
321 if it has access. If the queue is locked then the Tx lock count is
\r
322 incremented to signify that a task waiting for data can be made ready
\r
323 once the queue lock is removed. If the queue is not locked then
\r
324 a task can be moved from the event list, but will not be removed
\r
325 from the delayed list or placed in the ready list until the scheduler
\r
328 + xQueueReceiveFromISR(). As per xQueueGenericSendFromISR().
\r
331 /* If the queue is already full we may have to block. */
\r
334 if( prvIsQueueFull( pxQueue ) )
\r
336 /* The queue is full - do we want to block or just leave without
\r
338 if( xTicksToWait > ( portTickType ) 0 )
\r
340 /* We are going to place ourselves on the xTasksWaitingToSend event
\r
341 list, and will get woken should the delay expire, or space become
\r
342 available on the queue.
\r
344 As detailed above we do not require mutual exclusion on the event
\r
345 list as nothing else can modify it or the ready lists while we
\r
346 have the scheduler suspended and queue locked.
\r
348 It is possible that an ISR has removed data from the queue since we
\r
349 checked if any was available. If this is the case then the data
\r
350 will have been copied from the queue, and the queue variables
\r
351 updated, but the event list will not yet have been checked to see if
\r
352 anything is waiting as the queue is locked. */
\r
353 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
355 /* Force a context switch now as we are blocked. We can do
\r
356 this from within a critical section as the task we are
\r
357 switching to has its own context. When we return here (i.e. we
\r
358 unblock) we will leave the critical section as normal.
\r
360 It is possible that an ISR has caused an event on an unrelated and
\r
361 unlocked queue. If this was the case then the event list for that
\r
362 queue will have been updated but the ready lists left unchanged -
\r
363 instead the readied task will have been added to the pending ready
\r
365 taskENTER_CRITICAL();
\r
367 /* We can safely unlock the queue and scheduler here as
\r
368 interrupts are disabled. We must not yield with anything
\r
369 locked, but we can yield from within a critical section.
\r
371 Tasks that have been placed on the pending ready list cannot
\r
372 be tasks that are waiting for events on this queue. See
\r
373 in comment xTaskRemoveFromEventList(). */
\r
374 prvUnlockQueue( pxQueue );
\r
376 /* Resuming the scheduler may cause a yield. If so then there
\r
377 is no point yielding again here. */
\r
378 if( !xTaskResumeAll() )
\r
383 /* We want to check to see if the queue is still full
\r
384 before leaving the critical section. This is to prevent
\r
385 this task placing an item into the queue due to an
\r
386 interrupt making space on the queue between critical
\r
387 sections (when there might be a higher priority task
\r
388 blocked on the queue that cannot run yet because the
\r
389 scheduler gets suspended). */
\r
390 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
392 /* We unblocked but there is no space in the queue,
\r
393 we probably timed out. */
\r
394 xReturn = errQUEUE_FULL;
\r
397 /* Before leaving the critical section we have to ensure
\r
398 exclusive access again. */
\r
400 prvLockQueue( pxQueue );
\r
402 taskEXIT_CRITICAL();
\r
406 /* If xReturn is errQUEUE_FULL then we unblocked when the queue
\r
407 was still full. Don't check it again now as it is possible that
\r
408 an interrupt has removed an item from the queue since we left the
\r
409 critical section and we don't want to write to the queue in case
\r
410 there is a task of higher priority blocked waiting for space to
\r
411 be available on the queue. If this is the case the higher priority
\r
412 task will execute when the scheduler is unsupended. */
\r
413 if( xReturn != errQUEUE_FULL )
\r
415 /* When we are here it is possible that we unblocked as space became
\r
416 available on the queue. It is also possible that an ISR posted to the
\r
417 queue since we left the critical section, so it may be that again there
\r
418 is no space. This would only happen if a task and ISR post onto the
\r
420 taskENTER_CRITICAL();
\r
422 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
424 /* There is room in the queue, copy the data into the queue. */
\r
425 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
428 /* Update the TxLock count so prvUnlockQueue knows to check for
\r
429 tasks waiting for data to become available in the queue. */
\r
430 ++( pxQueue->xTxLock );
\r
434 xReturn = errQUEUE_FULL;
\r
437 taskEXIT_CRITICAL();
\r
440 if( xReturn == errQUEUE_FULL )
\r
442 if( xTicksToWait > 0 )
\r
444 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
446 xReturn = queueERRONEOUS_UNBLOCK;
\r
451 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
453 prvUnlockQueue( pxQueue );
\r
458 /*-----------------------------------------------------------*/
\r
460 #if configUSE_ALTERNATIVE_API == 1
\r
462 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
464 signed portBASE_TYPE xReturn;
\r
465 xTimeOutType xTimeOut;
\r
467 /* The source code that implements the alternative (Alt) API is much
\r
468 simpler because it executes everything from within a critical section.
\r
469 This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
470 preferred fully featured API too. The fully featured API has more
\r
471 complex code that takes longer to execute, but makes much less use of
\r
472 critical sections. Therefore the alternative API sacrifices interrupt
\r
473 responsiveness to gain execution speed, whereas the fully featured API
\r
474 sacrifices execution speed to ensure better interrupt responsiveness. */
\r
476 taskENTER_CRITICAL();
\r
478 /* Capture the current time status for future reference. */
\r
479 vTaskSetTimeOutState( &xTimeOut );
\r
481 /* If the queue is already full we may have to block. */
\r
484 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
486 /* The queue is full - do we want to block or just leave without
\r
488 if( xTicksToWait > ( portTickType ) 0 )
\r
490 /* We are going to place ourselves on the xTasksWaitingToSend
\r
491 event list, and will get woken should the delay expire, or
\r
492 space become available on the queue. */
\r
493 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
495 /* Force a context switch now as we are blocked. We can do
\r
496 this from within a critical section as the task we are
\r
497 switching to has its own context. When we return here (i.e.
\r
498 we unblock) we will leave the critical section as normal. */
\r
503 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
505 /* There is room in the queue, copy the data into the queue. */
\r
506 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
509 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
511 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
513 /* The task waiting has a higher priority. */
\r
520 xReturn = errQUEUE_FULL;
\r
522 if( xTicksToWait > 0 )
\r
524 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
526 /* Another task must have accessed the queue between
\r
527 this task unblocking and actually executing. */
\r
528 xReturn = queueERRONEOUS_UNBLOCK;
\r
533 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
535 taskEXIT_CRITICAL();
\r
540 #endif /* configUSE_ALTERNATIVE_API */
\r
541 /*-----------------------------------------------------------*/
\r
543 #if configUSE_ALTERNATIVE_API == 1
\r
545 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
547 signed portBASE_TYPE xReturn = pdTRUE;
\r
548 xTimeOutType xTimeOut;
\r
549 signed portCHAR *pcOriginalReadPosition;
\r
551 /* The source code that implements the alternative (Alt) API is much
\r
552 simpler because it executes everything from within a critical section.
\r
553 This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
554 preferred fully featured API too. The fully featured API has more
\r
555 complex code that takes longer to execute, but makes much less use of
\r
556 critical sections. Therefore the alternative API sacrifices interrupt
\r
557 responsiveness to gain execution speed, whereas the fully featured API
\r
558 sacrifices execution speed to ensure better interrupt responsiveness. */
\r
560 taskENTER_CRITICAL();
\r
562 /* Capture the current time status for future reference. */
\r
563 vTaskSetTimeOutState( &xTimeOut );
\r
567 /* If there are no messages in the queue we may have to block. */
\r
568 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
570 /* There are no messages in the queue, do we want to block or just
\r
571 leave with nothing? */
\r
572 if( xTicksToWait > ( portTickType ) 0 )
\r
574 #if ( configUSE_MUTEXES == 1 )
\r
576 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
578 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );
\r
583 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
588 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
590 /* Remember our read position in case we are just peeking. */
\r
591 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
593 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
595 if( xJustPeeking == pdFALSE )
\r
597 /* We are actually removing data. */
\r
598 --( pxQueue->uxMessagesWaiting );
\r
600 #if ( configUSE_MUTEXES == 1 )
\r
602 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
604 /* Record the information required to implement
\r
605 priority inheritance should it become necessary. */
\r
606 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
611 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
613 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
615 /* The task waiting has a higher priority. */
\r
622 /* We are not removing the data, so reset our read
\r
624 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
631 xReturn = errQUEUE_EMPTY;
\r
633 if( xTicksToWait > 0 )
\r
635 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
637 xReturn = queueERRONEOUS_UNBLOCK;
\r
642 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
644 taskEXIT_CRITICAL();
\r
649 #endif /* configUSE_ALTERNATIVE_API */
\r
650 /*-----------------------------------------------------------*/
\r
652 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )
\r
654 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
655 in the queue. Also we don't directly wake a task that was blocked on a
\r
656 queue read, instead we return a flag to say whether a context switch is
\r
657 required or not (i.e. has a task with a higher priority than us been woken
\r
659 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
661 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
663 /* If the queue is locked we do not alter the event list. This will
\r
664 be done when the queue is unlocked later. */
\r
665 if( pxQueue->xTxLock == queueUNLOCKED )
\r
667 /* We only want to wake one task per ISR, so check that a task has
\r
668 not already been woken. */
\r
669 if( !xTaskPreviouslyWoken )
\r
671 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
673 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
675 /* The task waiting has a higher priority so record that a
\r
676 context switch is required. */
\r
684 /* Increment the lock count so the task that unlocks the queue
\r
685 knows that data was posted while it was locked. */
\r
686 ++( pxQueue->xTxLock );
\r
690 return xTaskPreviouslyWoken;
\r
692 /*-----------------------------------------------------------*/
\r
694 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
696 signed portBASE_TYPE xReturn = pdTRUE;
\r
697 xTimeOutType xTimeOut;
\r
698 signed portCHAR *pcOriginalReadPosition;
\r
700 /* This function is very similar to xQueueGenericSend(). See comments
\r
701 within xQueueGenericSend() for a more detailed explanation.
\r
703 Make sure other tasks do not access the queue. */
\r
706 /* Capture the current time status for future reference. */
\r
707 vTaskSetTimeOutState( &xTimeOut );
\r
709 /* Make sure interrupts do not access the queue. */
\r
710 prvLockQueue( pxQueue );
\r
714 /* If there are no messages in the queue we may have to block. */
\r
715 if( prvIsQueueEmpty( pxQueue ) )
\r
717 /* There are no messages in the queue, do we want to block or just
\r
718 leave with nothing? */
\r
719 if( xTicksToWait > ( portTickType ) 0 )
\r
721 #if ( configUSE_MUTEXES == 1 )
\r
723 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
725 portENTER_CRITICAL();
\r
726 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );
\r
727 portEXIT_CRITICAL();
\r
732 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
733 taskENTER_CRITICAL();
\r
735 prvUnlockQueue( pxQueue );
\r
736 if( !xTaskResumeAll() )
\r
741 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
743 /* We unblocked but the queue is empty. We probably
\r
745 xReturn = errQUEUE_EMPTY;
\r
749 prvLockQueue( pxQueue );
\r
751 taskEXIT_CRITICAL();
\r
755 if( xReturn != errQUEUE_EMPTY )
\r
757 taskENTER_CRITICAL();
\r
759 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
761 /* Remember our read position in case we are just peeking. */
\r
762 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
764 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
766 if( xJustPeeking == pdFALSE )
\r
768 /* We are actually removing data. */
\r
769 --( pxQueue->uxMessagesWaiting );
\r
771 /* Increment the lock count so prvUnlockQueue knows to check for
\r
772 tasks waiting for space to become available on the queue. */
\r
773 ++( pxQueue->xRxLock );
\r
775 #if ( configUSE_MUTEXES == 1 )
\r
777 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
779 /* Record the information required to implement
\r
780 priority inheritance should it become necessary. */
\r
781 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
788 /* We are not removing the data, so reset our read
\r
790 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
792 /* The data is being left in the queue, so increment the
\r
793 lock count so prvUnlockQueue knows to check for other
\r
794 tasks waiting for the data to be available. */
\r
795 ++( pxQueue->xTxLock );
\r
802 xReturn = errQUEUE_EMPTY;
\r
805 taskEXIT_CRITICAL();
\r
808 if( xReturn == errQUEUE_EMPTY )
\r
810 if( xTicksToWait > 0 )
\r
812 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
814 xReturn = queueERRONEOUS_UNBLOCK;
\r
818 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
820 /* We no longer require exclusive access to the queue. */
\r
821 prvUnlockQueue( pxQueue );
\r
826 /*-----------------------------------------------------------*/
\r
828 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
830 signed portBASE_TYPE xReturn;
\r
832 /* We cannot block from an ISR, so check there is data available. */
\r
833 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
835 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
836 --( pxQueue->uxMessagesWaiting );
\r
838 /* If the queue is locked we will not modify the event list. Instead
\r
839 we update the lock count so the task that unlocks the queue will know
\r
840 that an ISR has removed data while the queue was locked. */
\r
841 if( pxQueue->xRxLock == queueUNLOCKED )
\r
843 /* We only want to wake one task per ISR, so check that a task has
\r
844 not already been woken. */
\r
845 if( !( *pxTaskWoken ) )
\r
847 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
849 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
851 /* The task waiting has a higher priority than us so
\r
852 force a context switch. */
\r
853 *pxTaskWoken = pdTRUE;
\r
860 /* Increment the lock count so the task that unlocks the queue
\r
861 knows that data was removed while it was locked. */
\r
862 ++( pxQueue->xRxLock );
\r
874 /*-----------------------------------------------------------*/
\r
876 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
878 unsigned portBASE_TYPE uxReturn;
\r
880 taskENTER_CRITICAL();
\r
881 uxReturn = pxQueue->uxMessagesWaiting;
\r
882 taskEXIT_CRITICAL();
\r
886 /*-----------------------------------------------------------*/
\r
888 void vQueueDelete( xQueueHandle pxQueue )
\r
890 vPortFree( pxQueue->pcHead );
\r
891 vPortFree( pxQueue );
\r
893 /*-----------------------------------------------------------*/
\r
895 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
897 if( pxQueue->uxItemSize == 0 )
\r
899 #if ( configUSE_MUTEXES == 1 )
\r
901 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
903 /* The mutex is no longer being held. */
\r
904 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );
\r
909 else if( xPosition == queueSEND_TO_BACK )
\r
911 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
912 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
913 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
915 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
920 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
921 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
922 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
924 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
928 ++( pxQueue->uxMessagesWaiting );
\r
930 /*-----------------------------------------------------------*/
\r
932 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
934 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
936 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
937 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
939 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
941 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
944 /*-----------------------------------------------------------*/
\r
946 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
948 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
950 /* The lock counts contains the number of extra data items placed or
\r
951 removed from the queue while the queue was locked. When a queue is
\r
952 locked items can be added or removed, but the event lists cannot be
\r
954 taskENTER_CRITICAL();
\r
956 --( pxQueue->xTxLock );
\r
958 /* See if data was added to the queue while it was locked. */
\r
959 if( pxQueue->xTxLock > queueUNLOCKED )
\r
961 pxQueue->xTxLock = queueUNLOCKED;
\r
963 /* Data was posted while the queue was locked. Are any tasks
\r
964 blocked waiting for data to become available? */
\r
965 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
967 /* Tasks that are removed from the event list will get added to
\r
968 the pending ready list as the scheduler is still suspended. */
\r
969 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
971 /* The task waiting has a higher priority so record that a
\r
972 context switch is required. */
\r
973 vTaskMissedYield();
\r
978 taskEXIT_CRITICAL();
\r
980 /* Do the same for the Rx lock. */
\r
981 taskENTER_CRITICAL();
\r
983 --( pxQueue->xRxLock );
\r
985 if( pxQueue->xRxLock > queueUNLOCKED )
\r
987 pxQueue->xRxLock = queueUNLOCKED;
\r
989 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
991 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
993 vTaskMissedYield();
\r
998 taskEXIT_CRITICAL();
\r
1000 /*-----------------------------------------------------------*/
\r
1002 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1004 signed portBASE_TYPE xReturn;
\r
1006 taskENTER_CRITICAL();
\r
1007 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1008 taskEXIT_CRITICAL();
\r
1012 /*-----------------------------------------------------------*/
\r
1014 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1016 signed portBASE_TYPE xReturn;
\r
1018 taskENTER_CRITICAL();
\r
1019 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1020 taskEXIT_CRITICAL();
\r
1024 /*-----------------------------------------------------------*/
\r
1026 #if configUSE_CO_ROUTINES == 1
\r
1027 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1029 signed portBASE_TYPE xReturn;
\r
1031 /* If the queue is already full we may have to block. A critical section
\r
1032 is required to prevent an interrupt removing something from the queue
\r
1033 between the check to see if the queue is full and blocking on the queue. */
\r
1034 portDISABLE_INTERRUPTS();
\r
1036 if( prvIsQueueFull( pxQueue ) )
\r
1038 /* The queue is full - do we want to block or just leave without
\r
1040 if( xTicksToWait > ( portTickType ) 0 )
\r
1042 /* As this is called from a coroutine we cannot block directly, but
\r
1043 return indicating that we need to block. */
\r
1044 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1045 portENABLE_INTERRUPTS();
\r
1046 return errQUEUE_BLOCKED;
\r
1050 portENABLE_INTERRUPTS();
\r
1051 return errQUEUE_FULL;
\r
1055 portENABLE_INTERRUPTS();
\r
1059 portDISABLE_INTERRUPTS();
\r
1061 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1063 /* There is room in the queue, copy the data into the queue. */
\r
1064 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1067 /* Were any co-routines waiting for data to become available? */
\r
1068 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1070 /* In this instance the co-routine could be placed directly
\r
1071 into the ready list as we are within a critical section.
\r
1072 Instead the same pending ready list mechanism is used as if
\r
1073 the event were caused from within an interrupt. */
\r
1074 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1076 /* The co-routine waiting has a higher priority so record
\r
1077 that a yield might be appropriate. */
\r
1078 xReturn = errQUEUE_YIELD;
\r
1084 xReturn = errQUEUE_FULL;
\r
1087 portENABLE_INTERRUPTS();
\r
1092 /*-----------------------------------------------------------*/
\r
1094 #if configUSE_CO_ROUTINES == 1
\r
1095 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1097 signed portBASE_TYPE xReturn;
\r
1099 /* If the queue is already empty we may have to block. A critical section
\r
1100 is required to prevent an interrupt adding something to the queue
\r
1101 between the check to see if the queue is empty and blocking on the queue. */
\r
1102 portDISABLE_INTERRUPTS();
\r
1104 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1106 /* There are no messages in the queue, do we want to block or just
\r
1107 leave with nothing? */
\r
1108 if( xTicksToWait > ( portTickType ) 0 )
\r
1110 /* As this is a co-routine we cannot block directly, but return
\r
1111 indicating that we need to block. */
\r
1112 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1113 portENABLE_INTERRUPTS();
\r
1114 return errQUEUE_BLOCKED;
\r
1118 portENABLE_INTERRUPTS();
\r
1119 return errQUEUE_FULL;
\r
1123 portENABLE_INTERRUPTS();
\r
1127 portDISABLE_INTERRUPTS();
\r
1129 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1131 /* Data is available from the queue. */
\r
1132 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1133 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1135 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1137 --( pxQueue->uxMessagesWaiting );
\r
1138 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1142 /* Were any co-routines waiting for space to become available? */
\r
1143 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1145 /* In this instance the co-routine could be placed directly
\r
1146 into the ready list as we are within a critical section.
\r
1147 Instead the same pending ready list mechanism is used as if
\r
1148 the event were caused from within an interrupt. */
\r
1149 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1151 xReturn = errQUEUE_YIELD;
\r
1160 portENABLE_INTERRUPTS();
\r
1165 /*-----------------------------------------------------------*/
\r
1169 #if configUSE_CO_ROUTINES == 1
\r
1170 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1172 /* Cannot block within an ISR so if there is no space on the queue then
\r
1173 exit without doing anything. */
\r
1174 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1176 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1178 /* We only want to wake one co-routine per ISR, so check that a
\r
1179 co-routine has not already been woken. */
\r
1180 if( !xCoRoutinePreviouslyWoken )
\r
1182 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1184 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1192 return xCoRoutinePreviouslyWoken;
\r
1195 /*-----------------------------------------------------------*/
\r
1197 #if configUSE_CO_ROUTINES == 1
\r
1198 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1200 signed portBASE_TYPE xReturn;
\r
1202 /* We cannot block from an ISR, so check there is data available. If
\r
1203 not then just leave without doing anything. */
\r
1204 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1206 /* Copy the data from the queue. */
\r
1207 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1208 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1210 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1212 --( pxQueue->uxMessagesWaiting );
\r
1213 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1215 if( !( *pxCoRoutineWoken ) )
\r
1217 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1219 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1221 *pxCoRoutineWoken = pdTRUE;
\r
1236 /*-----------------------------------------------------------*/
\r