2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
39 #include "FreeRTOS.h"
\r
41 #include "croutine.h"
\r
43 /*-----------------------------------------------------------
\r
44 * PUBLIC LIST API documented in list.h
\r
45 *----------------------------------------------------------*/
\r
47 /* Constants used with the cRxLock and cTxLock structure members. */
\r
48 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
49 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
51 /* For internal use only. */
\r
52 #define queueSEND_TO_BACK ( 0 )
\r
53 #define queueSEND_TO_FRONT ( 1 )
\r
55 /* Effectively make a union out of the xQUEUE structure. */
\r
56 #define pxMutexHolder pcTail
\r
57 #define uxQueueType pcHead
\r
58 #define queueQUEUE_IS_MUTEX NULL
\r
60 /* Semaphores do not actually store or copy data, so have an items size of
\r
62 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
63 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
66 * Definition of the queue used by the scheduler.
\r
67 * Items are queued by copy, not reference.
\r
69 typedef struct QueueDefinition
\r
71 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
72 signed portCHAR *pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
\r
74 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
75 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
77 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
78 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
80 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 light weight (fast) 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 fully featured API as an alternative. 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 light weight 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
537 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
539 taskEXIT_CRITICAL();
\r
544 #endif /* configUSE_ALTERNATIVE_API */
\r
545 /*-----------------------------------------------------------*/
\r
547 #if configUSE_ALTERNATIVE_API == 1
\r
549 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
551 signed portBASE_TYPE xReturn = pdTRUE;
\r
552 xTimeOutType xTimeOut;
\r
553 signed portCHAR *pcOriginalReadPosition;
\r
555 /* The source code that implements the light weight (fast) API is much
\r
556 simpler because it executes everything from within a critical section.
\r
557 This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
558 fully featured API as an alternative. The fully featured API has more
\r
559 complex code that takes longer to execute, but makes much less use of
\r
560 critical sections. Therefore the light weight API sacrifices interrupt
\r
561 responsiveness to gain execution speed, whereas the fully featured API
\r
562 sacrifices execution speed to ensure better interrupt responsiveness. */
\r
564 taskENTER_CRITICAL();
\r
566 /* Capture the current time status for future reference. */
\r
567 vTaskSetTimeOutState( &xTimeOut );
\r
571 /* If there are no messages in the queue we may have to block. */
\r
572 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
574 /* There are no messages in the queue, do we want to block or just
\r
575 leave with nothing? */
\r
576 if( xTicksToWait > ( portTickType ) 0 )
\r
578 #if ( configUSE_MUTEXES == 1 )
\r
580 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
582 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );
\r
587 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
592 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
594 /* Remember our read position in case we are just peeking. */
\r
595 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
597 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
599 if( xJustPeeking == pdFALSE )
\r
601 /* We are actually removing data. */
\r
602 --( pxQueue->uxMessagesWaiting );
\r
604 #if ( configUSE_MUTEXES == 1 )
\r
606 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
608 /* Record the information required to implement
\r
609 priority inheritance should it become necessary. */
\r
610 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
615 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
617 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
619 /* The task waiting has a higher priority. */
\r
626 /* We are not removing the data, so reset our read
\r
628 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
635 xReturn = errQUEUE_EMPTY;
\r
637 if( xTicksToWait > 0 )
\r
639 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
641 xReturn = queueERRONEOUS_UNBLOCK;
\r
646 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
648 taskEXIT_CRITICAL();
\r
653 #endif /* configUSE_ALTERNATIVE_API */
\r
654 /*-----------------------------------------------------------*/
\r
656 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )
\r
658 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
659 in the queue. Also we don't directly wake a task that was blocked on a
\r
660 queue read, instead we return a flag to say whether a context switch is
\r
661 required or not (i.e. has a task with a higher priority than us been woken
\r
663 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
665 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
667 /* If the queue is locked we do not alter the event list. This will
\r
668 be done when the queue is unlocked later. */
\r
669 if( pxQueue->xTxLock == queueUNLOCKED )
\r
671 /* We only want to wake one task per ISR, so check that a task has
\r
672 not already been woken. */
\r
673 if( !xTaskPreviouslyWoken )
\r
675 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
677 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
679 /* The task waiting has a higher priority so record that a
\r
680 context switch is required. */
\r
688 /* Increment the lock count so the task that unlocks the queue
\r
689 knows that data was posted while it was locked. */
\r
690 ++( pxQueue->xTxLock );
\r
694 return xTaskPreviouslyWoken;
\r
696 /*-----------------------------------------------------------*/
\r
698 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
700 signed portBASE_TYPE xReturn = pdTRUE;
\r
701 xTimeOutType xTimeOut;
\r
702 signed portCHAR *pcOriginalReadPosition;
\r
704 /* This function is very similar to xQueueGenericSend(). See comments
\r
705 within xQueueGenericSend() for a more detailed explanation.
\r
707 Make sure other tasks do not access the queue. */
\r
710 /* Capture the current time status for future reference. */
\r
711 vTaskSetTimeOutState( &xTimeOut );
\r
713 /* Make sure interrupts do not access the queue. */
\r
714 prvLockQueue( pxQueue );
\r
718 /* If there are no messages in the queue we may have to block. */
\r
719 if( prvIsQueueEmpty( pxQueue ) )
\r
721 /* There are no messages in the queue, do we want to block or just
\r
722 leave with nothing? */
\r
723 if( xTicksToWait > ( portTickType ) 0 )
\r
725 #if ( configUSE_MUTEXES == 1 )
\r
727 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
729 portENTER_CRITICAL();
\r
730 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );
\r
731 portEXIT_CRITICAL();
\r
736 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
737 taskENTER_CRITICAL();
\r
739 prvUnlockQueue( pxQueue );
\r
740 if( !xTaskResumeAll() )
\r
745 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
747 /* We unblocked but the queue is empty. We probably
\r
749 xReturn = errQUEUE_EMPTY;
\r
753 prvLockQueue( pxQueue );
\r
755 taskEXIT_CRITICAL();
\r
759 if( xReturn != errQUEUE_EMPTY )
\r
761 taskENTER_CRITICAL();
\r
763 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
765 /* Remember our read position in case we are just peeking. */
\r
766 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
768 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
770 if( xJustPeeking == pdFALSE )
\r
772 /* We are actually removing data. */
\r
773 --( pxQueue->uxMessagesWaiting );
\r
775 /* Increment the lock count so prvUnlockQueue knows to check for
\r
776 tasks waiting for space to become available on the queue. */
\r
777 ++( pxQueue->xRxLock );
\r
779 #if ( configUSE_MUTEXES == 1 )
\r
781 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
783 /* Record the information required to implement
\r
784 priority inheritance should it become necessary. */
\r
785 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
792 /* We are not removing the data, so reset our read
\r
794 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
796 /* The data is being left in the queue, so increment the
\r
797 lock count so prvUnlockQueue knows to check for other
\r
798 tasks waiting for the data to be available. */
\r
799 ++( pxQueue->xTxLock );
\r
806 xReturn = errQUEUE_EMPTY;
\r
809 taskEXIT_CRITICAL();
\r
812 if( xReturn == errQUEUE_EMPTY )
\r
814 if( xTicksToWait > 0 )
\r
816 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
818 xReturn = queueERRONEOUS_UNBLOCK;
\r
822 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
824 /* We no longer require exclusive access to the queue. */
\r
825 prvUnlockQueue( pxQueue );
\r
830 /*-----------------------------------------------------------*/
\r
832 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
834 signed portBASE_TYPE xReturn;
\r
836 /* We cannot block from an ISR, so check there is data available. */
\r
837 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
839 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
840 --( pxQueue->uxMessagesWaiting );
\r
842 /* If the queue is locked we will not modify the event list. Instead
\r
843 we update the lock count so the task that unlocks the queue will know
\r
844 that an ISR has removed data while the queue was locked. */
\r
845 if( pxQueue->xRxLock == queueUNLOCKED )
\r
847 /* We only want to wake one task per ISR, so check that a task has
\r
848 not already been woken. */
\r
849 if( !( *pxTaskWoken ) )
\r
851 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
853 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
855 /* The task waiting has a higher priority than us so
\r
856 force a context switch. */
\r
857 *pxTaskWoken = pdTRUE;
\r
864 /* Increment the lock count so the task that unlocks the queue
\r
865 knows that data was removed while it was locked. */
\r
866 ++( pxQueue->xRxLock );
\r
878 /*-----------------------------------------------------------*/
\r
880 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
882 unsigned portBASE_TYPE uxReturn;
\r
884 taskENTER_CRITICAL();
\r
885 uxReturn = pxQueue->uxMessagesWaiting;
\r
886 taskEXIT_CRITICAL();
\r
890 /*-----------------------------------------------------------*/
\r
892 void vQueueDelete( xQueueHandle pxQueue )
\r
894 vPortFree( pxQueue->pcHead );
\r
895 vPortFree( pxQueue );
\r
897 /*-----------------------------------------------------------*/
\r
899 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
901 if( pxQueue->uxItemSize == 0 )
\r
903 #if ( configUSE_MUTEXES == 1 )
\r
905 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
907 /* The mutex is no longer being held. */
\r
908 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );
\r
913 else if( xPosition == queueSEND_TO_BACK )
\r
915 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
916 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
917 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
919 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
924 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
925 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
926 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
928 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
932 ++( pxQueue->uxMessagesWaiting );
\r
934 /*-----------------------------------------------------------*/
\r
936 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
938 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
940 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
941 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
943 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
945 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
948 /*-----------------------------------------------------------*/
\r
950 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
952 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
954 /* The lock counts contains the number of extra data items placed or
\r
955 removed from the queue while the queue was locked. When a queue is
\r
956 locked items can be added or removed, but the event lists cannot be
\r
958 taskENTER_CRITICAL();
\r
960 --( pxQueue->xTxLock );
\r
962 /* See if data was added to the queue while it was locked. */
\r
963 if( pxQueue->xTxLock > queueUNLOCKED )
\r
965 pxQueue->xTxLock = queueUNLOCKED;
\r
967 /* Data was posted while the queue was locked. Are any tasks
\r
968 blocked waiting for data to become available? */
\r
969 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
971 /* Tasks that are removed from the event list will get added to
\r
972 the pending ready list as the scheduler is still suspended. */
\r
973 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
975 /* The task waiting has a higher priority so record that a
\r
976 context switch is required. */
\r
977 vTaskMissedYield();
\r
982 taskEXIT_CRITICAL();
\r
984 /* Do the same for the Rx lock. */
\r
985 taskENTER_CRITICAL();
\r
987 --( pxQueue->xRxLock );
\r
989 if( pxQueue->xRxLock > queueUNLOCKED )
\r
991 pxQueue->xRxLock = queueUNLOCKED;
\r
993 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
995 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
997 vTaskMissedYield();
\r
1002 taskEXIT_CRITICAL();
\r
1004 /*-----------------------------------------------------------*/
\r
1006 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1008 signed portBASE_TYPE xReturn;
\r
1010 taskENTER_CRITICAL();
\r
1011 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1012 taskEXIT_CRITICAL();
\r
1016 /*-----------------------------------------------------------*/
\r
1018 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1020 signed portBASE_TYPE xReturn;
\r
1022 taskENTER_CRITICAL();
\r
1023 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1024 taskEXIT_CRITICAL();
\r
1028 /*-----------------------------------------------------------*/
\r
1030 #if configUSE_CO_ROUTINES == 1
\r
1031 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1033 signed portBASE_TYPE xReturn;
\r
1035 /* If the queue is already full we may have to block. A critical section
\r
1036 is required to prevent an interrupt removing something from the queue
\r
1037 between the check to see if the queue is full and blocking on the queue. */
\r
1038 portDISABLE_INTERRUPTS();
\r
1040 if( prvIsQueueFull( pxQueue ) )
\r
1042 /* The queue is full - do we want to block or just leave without
\r
1044 if( xTicksToWait > ( portTickType ) 0 )
\r
1046 /* As this is called from a coroutine we cannot block directly, but
\r
1047 return indicating that we need to block. */
\r
1048 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1049 portENABLE_INTERRUPTS();
\r
1050 return errQUEUE_BLOCKED;
\r
1054 portENABLE_INTERRUPTS();
\r
1055 return errQUEUE_FULL;
\r
1059 portENABLE_INTERRUPTS();
\r
1063 portDISABLE_INTERRUPTS();
\r
1065 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1067 /* There is room in the queue, copy the data into the queue. */
\r
1068 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1071 /* Were any co-routines waiting for data to become available? */
\r
1072 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1074 /* In this instance the co-routine could be placed directly
\r
1075 into the ready list as we are within a critical section.
\r
1076 Instead the same pending ready list mechanism is used as if
\r
1077 the event were caused from within an interrupt. */
\r
1078 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1080 /* The co-routine waiting has a higher priority so record
\r
1081 that a yield might be appropriate. */
\r
1082 xReturn = errQUEUE_YIELD;
\r
1088 xReturn = errQUEUE_FULL;
\r
1091 portENABLE_INTERRUPTS();
\r
1096 /*-----------------------------------------------------------*/
\r
1098 #if configUSE_CO_ROUTINES == 1
\r
1099 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1101 signed portBASE_TYPE xReturn;
\r
1103 /* If the queue is already empty we may have to block. A critical section
\r
1104 is required to prevent an interrupt adding something to the queue
\r
1105 between the check to see if the queue is empty and blocking on the queue. */
\r
1106 portDISABLE_INTERRUPTS();
\r
1108 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1110 /* There are no messages in the queue, do we want to block or just
\r
1111 leave with nothing? */
\r
1112 if( xTicksToWait > ( portTickType ) 0 )
\r
1114 /* As this is a co-routine we cannot block directly, but return
\r
1115 indicating that we need to block. */
\r
1116 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1117 portENABLE_INTERRUPTS();
\r
1118 return errQUEUE_BLOCKED;
\r
1122 portENABLE_INTERRUPTS();
\r
1123 return errQUEUE_FULL;
\r
1127 portENABLE_INTERRUPTS();
\r
1131 portDISABLE_INTERRUPTS();
\r
1133 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1135 /* Data is available from the queue. */
\r
1136 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1137 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1139 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1141 --( pxQueue->uxMessagesWaiting );
\r
1142 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1146 /* Were any co-routines waiting for space to become available? */
\r
1147 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1149 /* In this instance the co-routine could be placed directly
\r
1150 into the ready list as we are within a critical section.
\r
1151 Instead the same pending ready list mechanism is used as if
\r
1152 the event were caused from within an interrupt. */
\r
1153 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1155 xReturn = errQUEUE_YIELD;
\r
1164 portENABLE_INTERRUPTS();
\r
1169 /*-----------------------------------------------------------*/
\r
1173 #if configUSE_CO_ROUTINES == 1
\r
1174 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1176 /* Cannot block within an ISR so if there is no space on the queue then
\r
1177 exit without doing anything. */
\r
1178 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1180 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1182 /* We only want to wake one co-routine per ISR, so check that a
\r
1183 co-routine has not already been woken. */
\r
1184 if( !xCoRoutinePreviouslyWoken )
\r
1186 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1188 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1196 return xCoRoutinePreviouslyWoken;
\r
1199 /*-----------------------------------------------------------*/
\r
1201 #if configUSE_CO_ROUTINES == 1
\r
1202 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1204 signed portBASE_TYPE xReturn;
\r
1206 /* We cannot block from an ISR, so check there is data available. If
\r
1207 not then just leave without doing anything. */
\r
1208 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1210 /* Copy the data from the queue. */
\r
1211 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1212 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1214 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1216 --( pxQueue->uxMessagesWaiting );
\r
1217 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1219 if( !( *pxCoRoutineWoken ) )
\r
1221 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1223 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1225 *pxCoRoutineWoken = pdTRUE;
\r
1240 /*-----------------------------------------------------------*/
\r