2 FreeRTOS.org V4.1.2 - Copyright (C) 2003-2006 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
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
59 + Added a loop within xQueueSend() and xQueueReceive() to prevent the
\r
60 functions exiting when a block time remains and the function has
\r
63 Changes from V4.1.2:
\r
65 + BUG FIX: Removed the call to prvIsQueueEmpty from within xQueueCRReceive
\r
66 as it exited with interrupts enabled. Thanks Paul Katz.
\r
71 #include "FreeRTOS.h"
\r
73 #include "croutine.h"
\r
75 /*-----------------------------------------------------------
\r
76 * PUBLIC LIST API documented in list.h
\r
77 *----------------------------------------------------------*/
\r
79 /* Constants used with the cRxLock and cTxLock structure members. */
\r
80 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
81 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
84 * Definition of the queue used by the scheduler.
\r
85 * Items are queued by copy, not reference.
\r
87 typedef struct QueueDefinition
\r
89 signed portCHAR *pcHead; /*< Points to the beginning of the queue storage area. */
\r
90 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
92 signed portCHAR *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
93 signed portCHAR *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
95 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
96 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
98 unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
99 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
100 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
102 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
103 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
105 /*-----------------------------------------------------------*/
\r
108 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
109 * To keep the definition private the API header file defines it as a
\r
112 typedef xQUEUE * xQueueHandle;
\r
115 * Prototypes for public functions are included here so we don't have to
\r
116 * include the API header file (as it defines xQueueHandle differently). These
\r
117 * functions are documented in the API header file.
\r
119 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
120 signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );
\r
121 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue );
\r
122 void vQueueDelete( xQueueHandle xQueue );
\r
123 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );
\r
124 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
125 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
127 #if configUSE_CO_ROUTINES == 1
\r
128 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
129 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
130 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
131 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r
135 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
136 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
137 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
138 * queue is locked it will instead increment the appropriate queue lock count
\r
139 * to indicate that a task may require unblocking. When the queue in unlocked
\r
140 * these lock counts are inspected, and the appropriate action taken.
\r
142 static void prvUnlockQueue( xQueueHandle pxQueue );
\r
145 * Uses a critical section to determine if there is any data in a queue.
\r
147 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
149 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
\r
152 * Uses a critical section to determine if there is any space in a queue.
\r
154 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
156 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
\r
159 * Macro that copies an item into the queue. This is done by copying the item
\r
160 * byte for byte, not by reference. Updates the queue state to ensure it's
\r
161 * integrity after the copy.
\r
163 #define prvCopyQueueData( pxQueue, pvItemToQueue ) \
\r
165 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize ); \
\r
166 ++( pxQueue->uxMessagesWaiting ); \
\r
167 pxQueue->pcWriteTo += pxQueue->uxItemSize; \
\r
168 if( pxQueue->pcWriteTo >= pxQueue->pcTail ) \
\r
170 pxQueue->pcWriteTo = pxQueue->pcHead; \
\r
173 /*-----------------------------------------------------------*/
\r
176 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
177 * accessing the queue event lists.
\r
179 #define prvLockQueue( pxQueue ) \
\r
181 taskENTER_CRITICAL(); \
\r
182 ++( pxQueue->xRxLock ); \
\r
183 ++( pxQueue->xTxLock ); \
\r
184 taskEXIT_CRITICAL(); \
\r
186 /*-----------------------------------------------------------*/
\r
189 /*-----------------------------------------------------------
\r
190 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
191 *----------------------------------------------------------*/
\r
193 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
195 xQUEUE *pxNewQueue;
\r
196 size_t xQueueSizeInBytes;
\r
198 /* Allocate the new queue structure. */
\r
199 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
201 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
202 if( pxNewQueue != NULL )
\r
204 /* Create the list of pointers to queue items. The queue is one byte
\r
205 longer than asked for to make wrap checking easier/faster. */
\r
206 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
208 pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
\r
209 if( pxNewQueue->pcHead != NULL )
\r
211 /* Initialise the queue members as described above where the
\r
212 queue type is defined. */
\r
213 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
214 pxNewQueue->uxMessagesWaiting = 0;
\r
215 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
216 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
217 pxNewQueue->uxLength = uxQueueLength;
\r
218 pxNewQueue->uxItemSize = uxItemSize;
\r
219 pxNewQueue->xRxLock = queueUNLOCKED;
\r
220 pxNewQueue->xTxLock = queueUNLOCKED;
\r
222 /* Likewise ensure the event queues start with the correct state. */
\r
223 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
224 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
230 vPortFree( pxNewQueue );
\r
235 /* Will only reach here if we could not allocate enough memory or no memory
\r
239 /*-----------------------------------------------------------*/
\r
241 signed portBASE_TYPE xQueueSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
243 signed portBASE_TYPE xReturn;
\r
244 xTimeOutType xTimeOut;
\r
246 /* Make sure other tasks do not access the queue. */
\r
249 /* Capture the current time status for future reference. */
\r
250 vTaskSetTimeOutState( &xTimeOut );
\r
252 /* It is important that this is the only thread/ISR that modifies the
\r
253 ready or delayed lists until xTaskResumeAll() is called. Places where
\r
254 the ready/delayed lists are modified include:
\r
256 + vTaskDelay() - Nothing can call vTaskDelay as the scheduler is
\r
257 suspended, vTaskDelay() cannot be called from an ISR.
\r
258 + vTaskPrioritySet() - Has a critical section around the access.
\r
259 + vTaskSwitchContext() - This will not get executed while the scheduler
\r
261 + prvCheckDelayedTasks() - This will not get executed while the
\r
262 scheduler is suspended.
\r
263 + xTaskCreate() - Has a critical section around the access.
\r
264 + vTaskResume() - Has a critical section around the access.
\r
265 + xTaskResumeAll() - Has a critical section around the access.
\r
266 + xTaskRemoveFromEventList - Checks to see if the scheduler is
\r
267 suspended. If so then the TCB being removed from the event is
\r
268 removed from the event and added to the xPendingReadyList.
\r
271 /* Make sure interrupts do not access the queue event list. */
\r
272 prvLockQueue( pxQueue );
\r
274 /* It is important that interrupts to not access the event list of the
\r
275 queue being modified here. Places where the event list is modified
\r
278 + xQueueSendFromISR(). This checks the lock on the queue to see if
\r
279 it has access. If the queue is locked then the Tx lock count is
\r
280 incremented to signify that a task waiting for data can be made ready
\r
281 once the queue lock is removed. If the queue is not locked then
\r
282 a task can be moved from the event list, but will not be removed
\r
283 from the delayed list or placed in the ready list until the scheduler
\r
286 + xQueueReceiveFromISR(). As per xQueueSendFromISR().
\r
289 /* If the queue is already full we may have to block. */
\r
292 if( prvIsQueueFull( pxQueue ) )
\r
294 /* The queue is full - do we want to block or just leave without
\r
296 if( xTicksToWait > ( portTickType ) 0 )
\r
298 /* We are going to place ourselves on the xTasksWaitingToSend event
\r
299 list, and will get woken should the delay expire, or space become
\r
300 available on the queue.
\r
302 As detailed above we do not require mutual exclusion on the event
\r
303 list as nothing else can modify it or the ready lists while we
\r
304 have the scheduler suspended and queue locked.
\r
306 It is possible that an ISR has removed data from the queue since we
\r
307 checked if any was available. If this is the case then the data
\r
308 will have been copied from the queue, and the queue variables
\r
309 updated, but the event list will not yet have been checked to see if
\r
310 anything is waiting as the queue is locked. */
\r
311 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
313 /* Force a context switch now as we are blocked. We can do
\r
314 this from within a critical section as the task we are
\r
315 switching to has its own context. When we return here (i.e. we
\r
316 unblock) we will leave the critical section as normal.
\r
318 It is possible that an ISR has caused an event on an unrelated and
\r
319 unlocked queue. If this was the case then the event list for that
\r
320 queue will have been updated but the ready lists left unchanged -
\r
321 instead the readied task will have been added to the pending ready
\r
323 taskENTER_CRITICAL();
\r
325 /* We can safely unlock the queue and scheduler here as
\r
326 interrupts are disabled. We must not yield with anything
\r
327 locked, but we can yield from within a critical section.
\r
329 Tasks that have been placed on the pending ready list cannot
\r
330 be tasks that are waiting for events on this queue. See
\r
331 in comment xTaskRemoveFromEventList(). */
\r
332 prvUnlockQueue( pxQueue );
\r
334 /* Resuming the scheduler may cause a yield. If so then there
\r
335 is no point yielding again here. */
\r
336 if( !xTaskResumeAll() )
\r
341 /* Before leaving the critical section we have to ensure
\r
342 exclusive access again. */
\r
344 prvLockQueue( pxQueue );
\r
346 taskEXIT_CRITICAL();
\r
350 /* When we are here it is possible that we unblocked as space became
\r
351 available on the queue. It is also possible that an ISR posted to the
\r
352 queue since we left the critical section, so it may be that again there
\r
353 is no space. This would only happen if a task and ISR post onto the
\r
355 taskENTER_CRITICAL();
\r
357 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
359 /* There is room in the queue, copy the data into the queue. */
\r
360 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
363 /* Update the TxLock count so prvUnlockQueue knows to check for
\r
364 tasks waiting for data to become available in the queue. */
\r
365 ++( pxQueue->xTxLock );
\r
369 xReturn = errQUEUE_FULL;
\r
372 taskEXIT_CRITICAL();
\r
374 if( xReturn == errQUEUE_FULL )
\r
376 if( xTicksToWait > 0 )
\r
378 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
380 xReturn = queueERRONEOUS_UNBLOCK;
\r
385 while( xReturn == queueERRONEOUS_UNBLOCK );
\r
387 prvUnlockQueue( pxQueue );
\r
392 /*-----------------------------------------------------------*/
\r
394 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken )
\r
396 /* Similar to xQueueSend, except we don't block if there is no room in the
\r
397 queue. Also we don't directly wake a task that was blocked on a queue
\r
398 read, instead we return a flag to say whether a context switch is required
\r
399 or not (i.e. has a task with a higher priority than us been woken by this
\r
401 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
403 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
405 /* If the queue is locked we do not alter the event list. This will
\r
406 be done when the queue is unlocked later. */
\r
407 if( pxQueue->xTxLock == queueUNLOCKED )
\r
409 /* We only want to wake one task per ISR, so check that a task has
\r
410 not already been woken. */
\r
411 if( !xTaskPreviouslyWoken )
\r
413 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
415 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
417 /* The task waiting has a higher priority so record that a
\r
418 context switch is required. */
\r
426 /* Increment the lock count so the task that unlocks the queue
\r
427 knows that data was posted while it was locked. */
\r
428 ++( pxQueue->xTxLock );
\r
432 return xTaskPreviouslyWoken;
\r
434 /*-----------------------------------------------------------*/
\r
436 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
438 signed portBASE_TYPE xReturn;
\r
439 xTimeOutType xTimeOut;
\r
441 /* This function is very similar to xQueueSend(). See comments within
\r
442 xQueueSend() for a more detailed explanation.
\r
444 Make sure other tasks do not access the queue. */
\r
447 /* Capture the current time status for future reference. */
\r
448 vTaskSetTimeOutState( &xTimeOut );
\r
450 /* Make sure interrupts do not access the queue. */
\r
451 prvLockQueue( pxQueue );
\r
455 /* If there are no messages in the queue we may have to block. */
\r
456 if( prvIsQueueEmpty( pxQueue ) )
\r
458 /* There are no messages in the queue, do we want to block or just
\r
459 leave with nothing? */
\r
460 if( xTicksToWait > ( portTickType ) 0 )
\r
462 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
463 taskENTER_CRITICAL();
\r
465 prvUnlockQueue( pxQueue );
\r
466 if( !xTaskResumeAll() )
\r
472 prvLockQueue( pxQueue );
\r
474 taskEXIT_CRITICAL();
\r
478 taskENTER_CRITICAL();
\r
480 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
482 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
483 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
485 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
487 --( pxQueue->uxMessagesWaiting );
\r
488 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
490 /* Increment the lock count so prvUnlockQueue knows to check for
\r
491 tasks waiting for space to become available on the queue. */
\r
492 ++( pxQueue->xRxLock );
\r
497 xReturn = errQUEUE_EMPTY;
\r
500 taskEXIT_CRITICAL();
\r
502 if( xReturn == errQUEUE_EMPTY )
\r
504 if( xTicksToWait > 0 )
\r
506 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
508 xReturn = queueERRONEOUS_UNBLOCK;
\r
512 } while( xReturn == queueERRONEOUS_UNBLOCK );
\r
514 /* We no longer require exclusive access to the queue. */
\r
515 prvUnlockQueue( pxQueue );
\r
520 /*-----------------------------------------------------------*/
\r
522 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
524 signed portBASE_TYPE xReturn;
\r
526 /* We cannot block from an ISR, so check there is data available. */
\r
527 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
529 /* Copy the data from the queue. */
\r
530 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
531 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
533 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
535 --( pxQueue->uxMessagesWaiting );
\r
536 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
538 /* If the queue is locked we will not modify the event list. Instead
\r
539 we update the lock count so the task that unlocks the queue will know
\r
540 that an ISR has removed data while the queue was locked. */
\r
541 if( pxQueue->xRxLock == queueUNLOCKED )
\r
543 /* We only want to wake one task per ISR, so check that a task has
\r
544 not already been woken. */
\r
545 if( !( *pxTaskWoken ) )
\r
547 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
549 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
551 /* The task waiting has a higher priority than us so
\r
552 force a context switch. */
\r
553 *pxTaskWoken = pdTRUE;
\r
560 /* Increment the lock count so the task that unlocks the queue
\r
561 knows that data was removed while it was locked. */
\r
562 ++( pxQueue->xRxLock );
\r
574 /*-----------------------------------------------------------*/
\r
576 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue )
\r
578 unsigned portBASE_TYPE uxReturn;
\r
580 taskENTER_CRITICAL();
\r
581 uxReturn = pxQueue->uxMessagesWaiting;
\r
582 taskEXIT_CRITICAL();
\r
586 /*-----------------------------------------------------------*/
\r
588 void vQueueDelete( xQueueHandle pxQueue )
\r
590 vPortFree( pxQueue->pcHead );
\r
591 vPortFree( pxQueue );
\r
593 /*-----------------------------------------------------------*/
\r
595 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
597 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
599 /* The lock counts contains the number of extra data items placed or
\r
600 removed from the queue while the queue was locked. When a queue is
\r
601 locked items can be added or removed, but the event lists cannot be
\r
603 taskENTER_CRITICAL();
\r
605 --( pxQueue->xTxLock );
\r
607 /* See if data was added to the queue while it was locked. */
\r
608 if( pxQueue->xTxLock > queueUNLOCKED )
\r
610 pxQueue->xTxLock = queueUNLOCKED;
\r
612 /* Data was posted while the queue was locked. Are any tasks
\r
613 blocked waiting for data to become available? */
\r
614 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
616 /* Tasks that are removed from the event list will get added to
\r
617 the pending ready list as the scheduler is still suspended. */
\r
618 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
620 /* The task waiting has a higher priority so record that a
\r
621 context switch is required. */
\r
622 vTaskMissedYield();
\r
627 taskEXIT_CRITICAL();
\r
629 /* Do the same for the Rx lock. */
\r
630 taskENTER_CRITICAL();
\r
632 --( pxQueue->xRxLock );
\r
634 if( pxQueue->xRxLock > queueUNLOCKED )
\r
636 pxQueue->xRxLock = queueUNLOCKED;
\r
638 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
640 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
642 vTaskMissedYield();
\r
647 taskEXIT_CRITICAL();
\r
649 /*-----------------------------------------------------------*/
\r
651 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
653 signed portBASE_TYPE xReturn;
\r
655 taskENTER_CRITICAL();
\r
656 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
657 taskEXIT_CRITICAL();
\r
661 /*-----------------------------------------------------------*/
\r
663 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
665 signed portBASE_TYPE xReturn;
\r
667 taskENTER_CRITICAL();
\r
668 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
669 taskEXIT_CRITICAL();
\r
673 /*-----------------------------------------------------------*/
\r
675 #if configUSE_CO_ROUTINES == 1
\r
676 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
678 signed portBASE_TYPE xReturn;
\r
680 /* If the queue is already full we may have to block. A critical section
\r
681 is required to prevent an interrupt removing something from the queue
\r
682 between the check to see if the queue is full and blocking on the queue. */
\r
683 portDISABLE_INTERRUPTS();
\r
685 if( prvIsQueueFull( pxQueue ) )
\r
687 /* The queue is full - do we want to block or just leave without
\r
689 if( xTicksToWait > ( portTickType ) 0 )
\r
691 /* As this is called from a coroutine we cannot block directly, but
\r
692 return indicating that we need to block. */
\r
693 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
694 portENABLE_INTERRUPTS();
\r
695 return errQUEUE_BLOCKED;
\r
699 portENABLE_INTERRUPTS();
\r
700 return errQUEUE_FULL;
\r
704 portENABLE_INTERRUPTS();
\r
708 portDISABLE_INTERRUPTS();
\r
710 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
712 /* There is room in the queue, copy the data into the queue. */
\r
713 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
716 /* Were any co-routines waiting for data to become available? */
\r
717 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
719 /* In this instance the co-routine could be placed directly
\r
720 into the ready list as we are within a critical section.
\r
721 Instead the same pending ready list mechansim is used as if
\r
722 the event were caused from within an interrupt. */
\r
723 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
725 /* The co-routine waiting has a higher priority so record
\r
726 that a yield might be appropriate. */
\r
727 xReturn = errQUEUE_YIELD;
\r
733 xReturn = errQUEUE_FULL;
\r
736 portENABLE_INTERRUPTS();
\r
741 /*-----------------------------------------------------------*/
\r
743 #if configUSE_CO_ROUTINES == 1
\r
744 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
746 signed portBASE_TYPE xReturn;
\r
748 /* If the queue is already empty we may have to block. A critical section
\r
749 is required to prevent an interrupt adding something to the queue
\r
750 between the check to see if the queue is empty and blocking on the queue. */
\r
751 portDISABLE_INTERRUPTS();
\r
753 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
755 /* There are no messages in the queue, do we want to block or just
\r
756 leave with nothing? */
\r
757 if( xTicksToWait > ( portTickType ) 0 )
\r
759 /* As this is a co-routine we cannot block directly, but return
\r
760 indicating that we need to block. */
\r
761 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
762 portENABLE_INTERRUPTS();
\r
763 return errQUEUE_BLOCKED;
\r
767 portENABLE_INTERRUPTS();
\r
768 return errQUEUE_FULL;
\r
772 portENABLE_INTERRUPTS();
\r
776 portDISABLE_INTERRUPTS();
\r
778 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
780 /* Data is available from the queue. */
\r
781 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
782 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
784 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
786 --( pxQueue->uxMessagesWaiting );
\r
787 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
791 /* Were any co-routines waiting for space to become available? */
\r
792 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
794 /* In this instance the co-routine could be placed directly
\r
795 into the ready list as we are within a critical section.
\r
796 Instead the same pending ready list mechansim is used as if
\r
797 the event were caused from within an interrupt. */
\r
798 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
800 xReturn = errQUEUE_YIELD;
\r
809 portENABLE_INTERRUPTS();
\r
814 /*-----------------------------------------------------------*/
\r
818 #if configUSE_CO_ROUTINES == 1
\r
819 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
821 /* Cannot block within an ISR so if there is no space on the queue then
\r
822 exit without doing anything. */
\r
823 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
825 prvCopyQueueData( pxQueue, pvItemToQueue );
\r
827 /* We only want to wake one co-routine per ISR, so check that a
\r
828 co-routine has not already been woken. */
\r
829 if( !xCoRoutinePreviouslyWoken )
\r
831 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
833 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
841 return xCoRoutinePreviouslyWoken;
\r
844 /*-----------------------------------------------------------*/
\r
846 #if configUSE_CO_ROUTINES == 1
\r
847 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
849 signed portBASE_TYPE xReturn;
\r
851 /* We cannot block from an ISR, so check there is data available. If
\r
852 not then just leave without doing anything. */
\r
853 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
855 /* Copy the data from the queue. */
\r
856 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
857 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
859 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
861 --( pxQueue->uxMessagesWaiting );
\r
862 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
864 if( !( *pxCoRoutineWoken ) )
\r
866 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
868 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
870 *pxCoRoutineWoken = pdTRUE;
\r
885 /*-----------------------------------------------------------*/
\r