2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS 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 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; 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, 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
30 ***************************************************************************
\r
36 + More use of 8bit data types.
\r
37 + Function name prefixes changed where the data type returned has changed.
\r
41 + Added the queue locking mechanism and make more use of the scheduler
\r
42 suspension feature to minimise the time interrupts have to be disabled
\r
43 when accessing a queue.
\r
47 + Explicit use of 'signed' qualifier on portCHAR types added.
\r
51 + API changes as described on the FreeRTOS.org WEB site.
\r
55 + Added the queue functions that can be used from co-routines.
\r
61 #include "FreeRTOS.h"
\r
63 #include "croutine.h"
\r
65 /*-----------------------------------------------------------
\r
66 * PUBLIC LIST API documented in list.h
\r
67 *----------------------------------------------------------*/
\r
69 /* Constants used with the cRxLock and cTxLock structure members. */
\r
70 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
73 * Definition of the queue used by the scheduler.
\r
74 * Items are queued by copy, not reference.
\r
76 typedef struct QueueDefinition
\r
78 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
79 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
81 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
82 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
84 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
85 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
87 unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
88 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
89 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
91 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
92 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
94 /*-----------------------------------------------------------*/
\r
97 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
98 * To keep the definition private the API header file defines it as a
\r
101 typedef xQUEUE * xQueueHandle;
\r
104 * Prototypes for public functions are included here so we don't have to
\r
105 * include the API header file (as it defines xQueueHandle differently). These
\r
106 * functions are documented in the API header file.
\r
108 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
109 signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );
\r
110 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue );
\r
111 void vQueueDelete( xQueueHandle xQueue );
\r
112 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );
\r
113 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
114 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
116 #if configUSE_CO_ROUTINES == 1
\r
117 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
118 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
119 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
120 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
124 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
125 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
126 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
127 * queue is locked it will instead increment the appropriate queue lock count
\r
128 * to indicate that a task may require unblocking. When the queue in unlocked
\r
129 * these lock counts are inspected, and the appropriate action taken.
\r
131 static signed portBASE_TYPE prvUnlockQueue( xQueueHandle pxQueue );
\r
134 * Uses a critical section to determine if there is any data in a queue.
\r
136 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
138 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
141 * Uses a critical section to determine if there is any space in a queue.
\r
143 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
145 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
148 * Macro that copies an item into the queue. This is done by copying the item
\r
149 * byte for byte, not by reference. Updates the queue state to ensure it's
\r
150 * integrity after the copy.
\r
152 #define prvCopyQueueData( pxQueue, pvItemToQueue ) \
\r
154 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize ); \
\r
155 ++( pxQueue->uxMessagesWaiting ); \
\r
156 pxQueue->pcWriteTo += pxQueue->uxItemSize; \
\r
157 if( pxQueue->pcWriteTo >= pxQueue->pcTail ) \
\r
159 pxQueue->pcWriteTo = pxQueue->pcHead; \
\r
162 /*-----------------------------------------------------------*/
\r
165 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
166 * accessing the queue event lists.
\r
168 #define prvLockQueue( pxQueue ) \
\r
170 taskENTER_CRITICAL(); \
\r
171 ++( pxQueue->xRxLock ); \
\r
172 ++( pxQueue->xTxLock ); \
\r
173 taskEXIT_CRITICAL(); \
\r
175 /*-----------------------------------------------------------*/
\r
178 /*-----------------------------------------------------------
\r
179 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
180 *----------------------------------------------------------*/
\r
182 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
184 xQUEUE *pxNewQueue;
\r
185 size_t xQueueSizeInBytes;
\r
187 /* Allocate the new queue structure. */
\r
188 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
190 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
191 if( pxNewQueue != NULL )
\r
193 /* Create the list of pointers to queue items. The queue is one byte
\r
194 longer than asked for to make wrap checking easier/faster. */
\r
195 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
197 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
198 if( pxNewQueue->pcHead != NULL )
\r
200 /* Initialise the queue members as described above where the
\r
201 queue type is defined. */
\r
202 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
203 pxNewQueue->uxMessagesWaiting = 0;
\r
204 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
205 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
206 pxNewQueue->uxLength = uxQueueLength;
\r
207 pxNewQueue->uxItemSize = uxItemSize;
\r
208 pxNewQueue->xRxLock = queueUNLOCKED;
\r
209 pxNewQueue->xTxLock = queueUNLOCKED;
\r
211 /* Likewise ensure the event queues start with the correct state. */
\r
212 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
213 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
219 vPortFree( pxNewQueue );
\r
224 /* Will only reach here if we could not allocate enough memory or no memory
\r
228 /*-----------------------------------------------------------*/
\r
230 signed portBASE_TYPE xQueueSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
232 signed portBASE_TYPE xReturn;
\r
234 /* Make sure other tasks do not access the queue. */
\r
237 /* It is important that this is the only thread/ISR that modifies the
\r
238 ready or delayed lists until xTaskResumeAll() is called. Places where
\r
239 the ready/delayed lists are modified include:
\r
241 + vTaskDelay() - Nothing can call vTaskDelay as the scheduler is
\r
242 suspended, vTaskDelay() cannot be called from an ISR.
\r
243 + vTaskPrioritySet() - Has a critical section around the access.
\r
244 + vTaskSwitchContext() - This will not get executed while the scheduler
\r
246 + prvCheckDelayedTasks() - This will not get executed while the
\r
247 scheduler is suspended.
\r
248 + xTaskCreate() - Has a critical section around the access.
\r
249 + vTaskResume() - Has a critical section around the access.
\r
250 + xTaskResumeAll() - Has a critical section around the access.
\r
251 + xTaskRemoveFromEventList - Checks to see if the scheduler is
\r
252 suspended. If so then the TCB being removed from the event is
\r
253 removed from the event and added to the xPendingReadyList.
\r
256 /* Make sure interrupts do not access the queue event list. */
\r
257 prvLockQueue( pxQueue );
\r
259 /* It is important that interrupts to not access the event list of the
\r
260 queue being modified here. Places where the event list is modified
\r
263 + xQueueSendFromISR(). This checks the lock on the queue to see if
\r
264 it has access. If the queue is locked then the Tx lock count is
\r
265 incremented to signify that a task waiting for data can be made ready
\r
266 once the queue lock is removed. If the queue is not locked then
\r
267 a task can be moved from the event list, but will not be removed
\r
268 from the delayed list or placed in the ready list until the scheduler
\r
271 + xQueueReceiveFromISR(). As per xQueueSendFromISR().
\r
274 /* If the queue is already full we may have to block. */
\r
275 if( prvIsQueueFull( pxQueue ) )
\r
277 /* The queue is full - do we want to block or just leave without
\r
279 if( xTicksToWait > ( portTickType ) 0 )
\r
281 /* We are going to place ourselves on the xTasksWaitingToSend event
\r
282 list, and will get woken should the delay expire, or space become
\r
283 available on the queue.
\r
285 As detailed above we do not require mutual exclusion on the event
\r
286 list as nothing else can modify it or the ready lists while we
\r
287 have the scheduler suspended and queue locked.
\r
289 It is possible that an ISR has removed data from the queue since we
\r
290 checked if any was available. If this is the case then the data
\r
291 will have been copied from the queue, and the queue variables
\r
292 updated, but the event list will not yet have been checked to see if
\r
293 anything is waiting as the queue is locked. */
\r
294 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
296 /* Force a context switch now as we are blocked. We can do
\r
297 this from within a critical section as the task we are
\r
298 switching to has its own context. When we return here (i.e. we
\r
299 unblock) we will leave the critical section as normal.
\r
301 It is possible that an ISR has caused an event on an unrelated and
\r
302 unlocked queue. If this was the case then the event list for that
\r
303 queue will have been updated but the ready lists left unchanged -
\r
304 instead the readied task will have been added to the pending ready
\r
306 taskENTER_CRITICAL();
\r
308 /* We can safely unlock the queue and scheduler here as
\r
309 interrupts are disabled. We must not yield with anything
\r
310 locked, but we can yield from within a critical section.
\r
312 Tasks that have been placed on the pending ready list cannot
\r
313 be tasks that are waiting for events on this queue. See
\r
314 in comment xTaskRemoveFromEventList(). */
\r
315 prvUnlockQueue( pxQueue );
\r
317 /* Resuming the scheduler may cause a yield. If so then there
\r
318 is no point yielding again here. */
\r
319 if( !xTaskResumeAll() )
\r
324 /* Before leaving the critical section we have to ensure
\r
325 exclusive access again. */
\r
327 prvLockQueue( pxQueue );
\r
329 taskEXIT_CRITICAL();
\r
333 /* When we are here it is possible that we unblocked as space became
\r
334 available on the queue. It is also possible that an ISR posted to the
\r
335 queue since we left the critical section, so it may be that again there
\r
336 is no space. This would only happen if a task and ISR post onto the
\r
338 taskENTER_CRITICAL();
\r
340 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
342 /* There is room in the queue, copy the data into the queue. */
\r
343 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
346 /* Update the TxLock count so prvUnlockQueue knows to check for
\r
347 tasks waiting for data to become available in the queue. */
\r
348 ++( pxQueue->xTxLock );
\r
352 xReturn = errQUEUE_FULL;
\r
355 taskEXIT_CRITICAL();
\r
357 /* We no longer require exclusive access to the queue. prvUnlockQueue
\r
358 will remove any tasks suspended on a receive if either this function
\r
359 or an ISR has posted onto the queue. */
\r
360 if( prvUnlockQueue( pxQueue ) )
\r
362 /* Resume the scheduler - making ready any tasks that were woken
\r
363 by an event while the scheduler was locked. Resuming the
\r
364 scheduler may cause a yield, in which case there is no point
\r
365 yielding again here. */
\r
366 if( !xTaskResumeAll() )
\r
373 /* Resume the scheduler - making ready any tasks that were woken
\r
374 by an event while the scheduler was locked. */
\r
380 /*-----------------------------------------------------------*/
\r
382 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken )
\r
384 /* Similar to xQueueSend, except we don't block if there is no room in the
\r
385 queue. Also we don't directly wake a task that was blocked on a queue
\r
386 read, instead we return a flag to say whether a context switch is required
\r
387 or not (i.e. has a task with a higher priority than us been woken by this
\r
389 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
391 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
393 /* If the queue is locked we do not alter the event list. This will
\r
394 be done when the queue is unlocked later. */
\r
395 if( pxQueue->xTxLock == queueUNLOCKED )
\r
397 /* We only want to wake one task per ISR, so check that a task has
\r
398 not already been woken. */
\r
399 if( !xTaskPreviouslyWoken )
\r
401 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
403 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
405 /* The task waiting has a higher priority so record that a
\r
406 context switch is required. */
\r
414 /* Increment the lock count so the task that unlocks the queue
\r
415 knows that data was posted while it was locked. */
\r
416 ++( pxQueue->xTxLock );
\r
420 return xTaskPreviouslyWoken;
\r
422 /*-----------------------------------------------------------*/
\r
424 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
426 signed portBASE_TYPE xReturn;
\r
428 /* This function is very similar to xQueueSend(). See comments within
\r
429 xQueueSend() for a more detailed explanation.
\r
431 Make sure other tasks do not access the queue. */
\r
434 /* Make sure interrupts do not access the queue. */
\r
435 prvLockQueue( pxQueue );
\r
437 /* If there are no messages in the queue we may have to block. */
\r
438 if( prvIsQueueEmpty( pxQueue ) )
\r
440 /* There are no messages in the queue, do we want to block or just
\r
441 leave with nothing? */
\r
442 if( xTicksToWait > ( portTickType ) 0 )
\r
444 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
445 taskENTER_CRITICAL();
\r
447 prvUnlockQueue( pxQueue );
\r
448 if( !xTaskResumeAll() )
\r
454 prvLockQueue( pxQueue );
\r
456 taskEXIT_CRITICAL();
\r
460 taskENTER_CRITICAL();
\r
462 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
464 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
465 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
467 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
469 --( pxQueue->uxMessagesWaiting );
\r
470 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
472 /* Increment the lock count so prvUnlockQueue knows to check for
\r
473 tasks waiting for space to become available on the queue. */
\r
474 ++( pxQueue->xRxLock );
\r
482 taskEXIT_CRITICAL();
\r
484 /* We no longer require exclusive access to the queue. */
\r
485 if( prvUnlockQueue( pxQueue ) )
\r
487 if( !xTaskResumeAll() )
\r
499 /*-----------------------------------------------------------*/
\r
501 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
503 signed portBASE_TYPE xReturn;
\r
505 /* We cannot block from an ISR, so check there is data available. */
\r
506 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
508 /* Copy the data from the queue. */
\r
509 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
510 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
512 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
514 --( pxQueue->uxMessagesWaiting );
\r
515 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
517 /* If the queue is locked we will not modify the event list. Instead
\r
518 we update the lock count so the task that unlocks the queue will know
\r
519 that an ISR has removed data while the queue was locked. */
\r
520 if( pxQueue->xRxLock == queueUNLOCKED )
\r
522 /* We only want to wake one task per ISR, so check that a task has
\r
523 not already been woken. */
\r
524 if( !( *pxTaskWoken ) )
\r
526 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
528 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
530 /* The task waiting has a higher priority than us so
\r
531 force a context switch. */
\r
532 *pxTaskWoken = pdTRUE;
\r
539 /* Increment the lock count so the task that unlocks the queue
\r
540 knows that data was removed while it was locked. */
\r
541 ++( pxQueue->xRxLock );
\r
553 /*-----------------------------------------------------------*/
\r
555 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue )
\r
557 unsigned portBASE_TYPE uxReturn;
\r
559 taskENTER_CRITICAL();
\r
560 uxReturn = pxQueue->uxMessagesWaiting;
\r
561 taskEXIT_CRITICAL();
\r
565 /*-----------------------------------------------------------*/
\r
567 void vQueueDelete( xQueueHandle pxQueue )
\r
569 vPortFree( pxQueue->pcHead );
\r
570 vPortFree( pxQueue );
\r
572 /*-----------------------------------------------------------*/
\r
574 static signed portBASE_TYPE prvUnlockQueue( xQueueHandle pxQueue )
\r
576 signed portBASE_TYPE xYieldRequired = pdFALSE;
\r
578 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
580 /* The lock counts contains the number of extra data items placed or
\r
581 removed from the queue while the queue was locked. When a queue is
\r
582 locked items can be added or removed, but the event lists cannot be
\r
584 taskENTER_CRITICAL();
\r
586 --( pxQueue->xTxLock );
\r
588 /* See if data was added to the queue while it was locked. */
\r
589 if( pxQueue->xTxLock > queueUNLOCKED )
\r
591 pxQueue->xTxLock = queueUNLOCKED;
\r
593 /* Data was posted while the queue was locked. Are any tasks
\r
594 blocked waiting for data to become available? */
\r
595 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
597 /* Tasks that are removed from the event list will get added to
\r
598 the pending ready list as the scheduler is still suspended. */
\r
599 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
601 /* The task waiting has a higher priority so record that a
\r
602 context switch is required. */
\r
603 xYieldRequired = pdTRUE;
\r
608 taskEXIT_CRITICAL();
\r
610 /* Do the same for the Rx lock. */
\r
611 taskENTER_CRITICAL();
\r
613 --( pxQueue->xRxLock );
\r
615 if( pxQueue->xRxLock > queueUNLOCKED )
\r
617 pxQueue->xRxLock = queueUNLOCKED;
\r
619 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
621 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
623 xYieldRequired = pdTRUE;
\r
628 taskEXIT_CRITICAL();
\r
630 return xYieldRequired;
\r
632 /*-----------------------------------------------------------*/
\r
634 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
636 signed portBASE_TYPE xReturn;
\r
638 taskENTER_CRITICAL();
\r
639 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
640 taskEXIT_CRITICAL();
\r
644 /*-----------------------------------------------------------*/
\r
646 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
648 signed portBASE_TYPE xReturn;
\r
650 taskENTER_CRITICAL();
\r
651 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
652 taskEXIT_CRITICAL();
\r
656 /*-----------------------------------------------------------*/
\r
658 #if configUSE_CO_ROUTINES == 1
\r
659 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
661 signed portBASE_TYPE xReturn;
\r
663 /* If the queue is already full we may have to block. A critical section
\r
664 is required to prevent an interrupt removing something from the queue
\r
665 between the check to see if the queue is full and blocking on the queue. */
\r
666 portDISABLE_INTERRUPTS();
\r
668 if( prvIsQueueFull( pxQueue ) )
\r
670 /* The queue is full - do we want to block or just leave without
\r
672 if( xTicksToWait > ( portTickType ) 0 )
\r
674 /* As this is called from a coroutine we cannot block directly, but
\r
675 return indicating that we need to block. */
\r
676 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
677 portENABLE_INTERRUPTS();
\r
678 return errQUEUE_BLOCKED;
\r
682 portENABLE_INTERRUPTS();
\r
683 return errQUEUE_FULL;
\r
687 portENABLE_INTERRUPTS();
\r
691 portDISABLE_INTERRUPTS();
\r
693 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
695 /* There is room in the queue, copy the data into the queue. */
\r
696 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
699 /* Were any co-routines waiting for data to become available? */
\r
700 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
702 /* In this instance the co-routine could be placed directly
\r
703 into the ready list as we are within a critical section.
\r
704 Instead the same pending ready list mechansim is used as if
\r
705 the event were caused from within an interrupt. */
\r
706 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
708 /* The co-routine waiting has a higher priority so record
\r
709 that a yield might be appropriate. */
\r
710 xReturn = errQUEUE_YIELD;
\r
716 xReturn = errQUEUE_FULL;
\r
719 portENABLE_INTERRUPTS();
\r
724 /*-----------------------------------------------------------*/
\r
726 #if configUSE_CO_ROUTINES == 1
\r
727 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
729 signed portBASE_TYPE xReturn;
\r
731 /* If the queue is already empty we may have to block. A critical section
\r
732 is required to prevent an interrupt adding something to the queue
\r
733 between the check to see if the queue is empty and blocking on the queue. */
\r
734 portDISABLE_INTERRUPTS();
\r
736 if( prvIsQueueEmpty( pxQueue ) )
\r
738 /* There are no messages in the queue, do we want to block or just
\r
739 leave with nothing? */
\r
740 if( xTicksToWait > ( portTickType ) 0 )
\r
742 /* As this is a co-routine we cannot block directly, but return
\r
743 indicating that we need to block. */
\r
744 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
745 portENABLE_INTERRUPTS();
\r
746 return errQUEUE_BLOCKED;
\r
750 portENABLE_INTERRUPTS();
\r
751 return errQUEUE_FULL;
\r
755 portENABLE_INTERRUPTS();
\r
759 portDISABLE_INTERRUPTS();
\r
761 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
763 /* Data is available from the queue. */
\r
764 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
765 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
767 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
769 --( pxQueue->uxMessagesWaiting );
\r
770 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
774 /* Were any co-routines waiting for space to become available? */
\r
775 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
777 /* In this instance the co-routine could be placed directly
\r
778 into the ready list as we are within a critical section.
\r
779 Instead the same pending ready list mechansim is used as if
\r
780 the event were caused from within an interrupt. */
\r
781 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
783 xReturn = errQUEUE_YIELD;
\r
792 portENABLE_INTERRUPTS();
\r
797 /*-----------------------------------------------------------*/
\r
801 #if configUSE_CO_ROUTINES == 1
\r
802 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
804 /* Cannot block within an ISR so if there is no space on the queue then
\r
805 exit without doing anything. */
\r
806 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
808 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
810 /* We only want to wake one co-routine per ISR, so check that a
\r
811 co-routine has not already been woken. */
\r
812 if( !xCoRoutinePreviouslyWoken )
\r
814 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
816 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
824 return xCoRoutinePreviouslyWoken;
\r
827 /*-----------------------------------------------------------*/
\r
829 #if configUSE_CO_ROUTINES == 1
\r
830 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
832 signed portBASE_TYPE xReturn;
\r
834 /* We cannot block from an ISR, so check there is data available. If
\r
835 not then just leave without doing anything. */
\r
836 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
838 /* Copy the data from the queue. */
\r
839 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
840 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
842 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
844 --( pxQueue->uxMessagesWaiting );
\r
845 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
847 if( !( *pxCoRoutineWoken ) )
\r
849 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
851 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
853 *pxCoRoutineWoken = pdTRUE;
\r
868 /*-----------------------------------------------------------*/
\r