changing circuitry to disable RTC, update initialization to match
[fw/openalt] / FreeRTOS / queue.c
1 /*
2         FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.
3
4         This file is part of the FreeRTOS.org distribution.
5
6         FreeRTOS.org is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10
11         FreeRTOS.org is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with FreeRTOS.org; if not, write to the Free Software
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20         A special exception to the GPL can be applied should you wish to distribute
21         a combined work that includes FreeRTOS.org, without being obliged to provide
22         the source code for any proprietary components.  See the licensing section
23         of http://www.FreeRTOS.org for full details of how and when the exception
24         can be applied.
25
26         ***************************************************************************
27         See http://www.FreeRTOS.org for documentation, latest information, license
28         and contact details.  Please ensure to read the configuration and relevant
29         port sections of the online documentation.
30
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
32         with commercial development and support options.
33         ***************************************************************************
34 */
35
36 /*
37 Changes from V1.01
38
39         + More use of 8bit data types.
40         + Function name prefixes changed where the data type returned has changed.
41
42 Changed from V2.0.0
43
44         + Added the queue locking mechanism and make more use of the scheduler
45           suspension feature to minimise the time interrupts have to be disabled
46           when accessing a queue.
47
48 Changed from V2.2.0
49
50         + Explicit use of 'signed' qualifier on portCHAR types added.
51
52 Changes from V3.0.0
53
54         + API changes as described on the FreeRTOS.org WEB site.
55
56 Changes from V3.2.3
57
58         + Added the queue functions that can be used from co-routines.
59
60 Changes from V4.0.5
61
62         + Added a loop within xQueueSend() and xQueueReceive() to prevent the
63           functions exiting when a block time remains and the function has
64           not completed.
65
66 Changes from V4.1.2:
67
68         + BUG FIX:  Removed the call to prvIsQueueEmpty from within xQueueCRReceive
69           as it exited with interrupts enabled.  Thanks Paul Katz.
70
71 Changes from V4.1.3:
72
73         + Modified xQueueSend() and xQueueReceive() to handle the (very unlikely) 
74         case whereby a task unblocking due to a temporal event can remove/send an 
75         item from/to a queue when a higher priority task is     still blocked on the 
76         queue.  This modification is a result of the SafeRTOS testing.
77 */
78
79 #include <stdlib.h>
80 #include <string.h>
81 #include "FreeRTOS.h"
82 #include "task.h"
83 #include "croutine.h"
84
85 /*-----------------------------------------------------------
86  * PUBLIC LIST API documented in list.h
87  *----------------------------------------------------------*/
88
89 /* Constants used with the cRxLock and cTxLock structure members. */
90 #define queueUNLOCKED   ( ( signed portBASE_TYPE ) -1 )
91 #define queueERRONEOUS_UNBLOCK                                  ( -1 )
92
93 /*
94  * Definition of the queue used by the scheduler.
95  * Items are queued by copy, not reference.
96  */
97 typedef struct QueueDefinition
98 {
99         signed portCHAR *pcHead;                                /*< Points to the beginning of the queue storage area. */
100         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. */
101
102         signed portCHAR *pcWriteTo;                             /*< Points to the free next place in the storage area. */
103         signed portCHAR *pcReadFrom;                    /*< Points to the last place that a queued item was read from. */
104
105         xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */
106         xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */
107
108         unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
109         unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
110         unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */
111
112         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. */
113         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. */
114 } xQUEUE;
115 /*-----------------------------------------------------------*/
116
117 /*
118  * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
119  * To keep the definition private the API header file defines it as a
120  * pointer to void.
121  */
122 typedef xQUEUE * xQueueHandle;
123
124 /*
125  * Prototypes for public functions are included here so we don't have to
126  * include the API header file (as it defines xQueueHandle differently).  These
127  * functions are documented in the API header file.
128  */
129 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
130 signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );
131 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue );
132 void vQueueDelete( xQueueHandle xQueue );
133 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );
134 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
135 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
136
137 #if configUSE_CO_ROUTINES == 1
138         signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
139         signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
140         signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
141         signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
142 #endif
143
144 /*
145  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not
146  * prevent an ISR from adding or removing items to the queue, but does prevent
147  * an ISR from removing tasks from the queue event lists.  If an ISR finds a
148  * queue is locked it will instead increment the appropriate queue lock count
149  * to indicate that a task may require unblocking.  When the queue in unlocked
150  * these lock counts are inspected, and the appropriate action taken.
151  */
152 static void prvUnlockQueue( xQueueHandle pxQueue );
153
154 /*
155  * Uses a critical section to determine if there is any data in a queue.
156  *
157  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
158  */
159 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );
160
161 /*
162  * Uses a critical section to determine if there is any space in a queue.
163  *
164  * @return pdTRUE if there is no space, otherwise pdFALSE;
165  */
166 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );
167
168 /*
169  * Macro that copies an item into the queue.  This is done by copying the item
170  * byte for byte, not by reference.  Updates the queue state to ensure it's
171  * integrity after the copy.
172  */
173 #define prvCopyQueueData( pxQueue, pvItemToQueue )                                                                                              \
174 {                                                                                                                                                                                               \
175         memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );       \
176         ++( pxQueue->uxMessagesWaiting );                                                                                                                       \
177         pxQueue->pcWriteTo += pxQueue->uxItemSize;                                                                                                      \
178         if( pxQueue->pcWriteTo >= pxQueue->pcTail )                                                                                                     \
179         {                                                                                                                                                                                       \
180                 pxQueue->pcWriteTo = pxQueue->pcHead;                                                                                                   \
181         }                                                                                                                                                                                       \
182 }
183 /*-----------------------------------------------------------*/
184
185 /*
186  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from
187  * accessing the queue event lists.
188  */
189 #define prvLockQueue( pxQueue )                 \
190 {                                                                               \
191         taskENTER_CRITICAL();                           \
192                 ++( pxQueue->xRxLock );                 \
193                 ++( pxQueue->xTxLock );                 \
194         taskEXIT_CRITICAL();                            \
195 }
196 /*-----------------------------------------------------------*/
197
198
199 /*-----------------------------------------------------------
200  * PUBLIC QUEUE MANAGEMENT API documented in queue.h
201  *----------------------------------------------------------*/
202
203 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
204 {
205 xQUEUE *pxNewQueue;
206 size_t xQueueSizeInBytes;
207
208         /* Allocate the new queue structure. */
209         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
210         {
211                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
212                 if( pxNewQueue != NULL )
213                 {
214                         /* Create the list of pointers to queue items.  The queue is one byte
215                         longer than asked for to make wrap checking easier/faster. */
216                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
217
218                         pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );
219                         if( pxNewQueue->pcHead != NULL )
220                         {
221                                 /* Initialise the queue members as described above where the
222                                 queue type is defined. */
223                                 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
224                                 pxNewQueue->uxMessagesWaiting = 0;
225                                 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
226                                 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
227                                 pxNewQueue->uxLength = uxQueueLength;
228                                 pxNewQueue->uxItemSize = uxItemSize;
229                                 pxNewQueue->xRxLock = queueUNLOCKED;
230                                 pxNewQueue->xTxLock = queueUNLOCKED;
231
232                                 /* Likewise ensure the event queues start with the correct state. */
233                                 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
234                                 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
235
236                                 return  pxNewQueue;
237                         }
238                         else
239                         {
240                                 vPortFree( pxNewQueue );
241                         }
242                 }
243         }
244
245         /* Will only reach here if we could not allocate enough memory or no memory
246         was required. */
247         return NULL;
248 }
249 /*-----------------------------------------------------------*/
250
251 signed portBASE_TYPE xQueueSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
252 {
253 signed portBASE_TYPE xReturn = pdPASS;
254 xTimeOutType xTimeOut;
255
256         /* Make sure other tasks do not access the queue. */
257         vTaskSuspendAll();
258
259         /* Capture the current time status for future reference. */
260         vTaskSetTimeOutState( &xTimeOut );
261
262         /* It is important that this is the only thread/ISR that modifies the
263         ready or delayed lists until xTaskResumeAll() is called.  Places where
264         the ready/delayed lists are modified include:
265
266                 + vTaskDelay() -  Nothing can call vTaskDelay as the scheduler is
267                   suspended, vTaskDelay() cannot be called from an ISR.
268                 + vTaskPrioritySet() - Has a critical section around the access.
269                 + vTaskSwitchContext() - This will not get executed while the scheduler
270                   is suspended.
271                 + prvCheckDelayedTasks() - This will not get executed while the
272                   scheduler is suspended.
273                 + xTaskCreate() - Has a critical section around the access.
274                 + vTaskResume() - Has a critical section around the access.
275                 + xTaskResumeAll() - Has a critical section around the access.
276                 + xTaskRemoveFromEventList - Checks to see if the scheduler is
277                   suspended.  If so then the TCB being removed from the event is
278                   removed from the event and added to the xPendingReadyList.
279         */
280
281         /* Make sure interrupts do not access the queue event list. */
282         prvLockQueue( pxQueue );
283
284         /* It is important that interrupts to not access the event list of the
285         queue being modified here.  Places where the event list is modified
286         include:
287
288                 + xQueueSendFromISR().  This checks the lock on the queue to see if
289                   it has access.  If the queue is locked then the Tx lock count is
290                   incremented to signify that a task waiting for data can be made ready
291                   once the queue lock is removed.  If the queue is not locked then
292                   a task can be moved from the event list, but will not be removed
293                   from the delayed list or placed in the ready list until the scheduler
294                   is unlocked.
295
296                 + xQueueReceiveFromISR().  As per xQueueSendFromISR().
297         */
298                 
299         /* If the queue is already full we may have to block. */
300         do
301         {
302                 if( prvIsQueueFull( pxQueue ) )
303                 {
304                         /* The queue is full - do we want to block or just leave without
305                         posting? */
306                         if( xTicksToWait > ( portTickType ) 0 )
307                         {
308                                 /* We are going to place ourselves on the xTasksWaitingToSend event
309                                 list, and will get woken should the delay expire, or space become
310                                 available on the queue.
311                                 
312                                 As detailed above we do not require mutual exclusion on the event
313                                 list as nothing else can modify it or the ready lists while we
314                                 have the scheduler suspended and queue locked.
315                                 
316                                 It is possible that an ISR has removed data from the queue since we
317                                 checked if any was available.  If this is the case then the data
318                                 will have been copied from the queue, and the queue variables
319                                 updated, but the event list will not yet have been checked to see if
320                                 anything is waiting as the queue is locked. */
321                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
322         
323                                 /* Force a context switch now as we are blocked.  We can do
324                                 this from within a critical section as the task we are
325                                 switching to has its own context.  When we return here (i.e. we
326                                 unblock) we will leave the critical section as normal.
327                                 
328                                 It is possible that an ISR has caused an event on an unrelated and
329                                 unlocked queue.  If this was the case then the event list for that
330                                 queue will have been updated but the ready lists left unchanged -
331                                 instead the readied task will have been added to the pending ready
332                                 list. */
333                                 taskENTER_CRITICAL();
334                                 {
335                                         /* We can safely unlock the queue and scheduler here as
336                                         interrupts are disabled.  We must not yield with anything
337                                         locked, but we can yield from within a critical section.
338                                         
339                                         Tasks that have been placed on the pending ready list cannot
340                                         be tasks that are waiting for events on this queue.  See
341                                         in comment xTaskRemoveFromEventList(). */
342                                         prvUnlockQueue( pxQueue );
343         
344                                         /* Resuming the scheduler may cause a yield.  If so then there
345                                         is no point yielding again here. */
346                                         if( !xTaskResumeAll() )
347                                         {
348                                                 taskYIELD();
349                                         }
350
351                                         /* We want to check to see if the queue is still full
352                                         before leaving the critical section.  This is to prevent
353                                         this task placing an item into the queue due to an
354                                         interrupt making space on the queue between critical
355                                         sections (when there might be a higher priority task
356                                         blocked on the queue that cannot run yet because the
357                                         scheduler gets suspended). */
358                                         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
359                                         {
360                                                 /* We unblocked but there is no space in the queue,
361                                                 we probably timed out. */
362                                                 xReturn = errQUEUE_FULL;
363                                         }
364         
365                                         /* Before leaving the critical section we have to ensure
366                                         exclusive access again. */
367                                         vTaskSuspendAll();
368                                         prvLockQueue( pxQueue );                                
369                                 }
370                                 taskEXIT_CRITICAL();
371                         }
372                 }
373                         
374                 /* If xReturn is errQUEUE_FULL then we unblocked when the queue
375                 was still full.  Don't check it again now as it is possible that
376                 an interrupt has removed an item from the queue since we left the
377                 critical section and we don't want to write to the queue in case
378                 there is a task of higher priority blocked waiting for space to
379                 be available on the queue.  If this is the case the higher priority
380                 task will execute when the scheduler is unsupended. */
381                 if( xReturn != errQUEUE_FULL )
382                 {
383                         /* When we are here it is possible that we unblocked as space became
384                         available on the queue.  It is also possible that an ISR posted to the
385                         queue since we left the critical section, so it may be that again there
386                         is no space.  This would only happen if a task and ISR post onto the
387                         same queue. */
388                         taskENTER_CRITICAL();
389                         {
390                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
391                                 {
392                                         /* There is room in the queue, copy the data into the queue. */                 
393                                         prvCopyQueueData( pxQueue, pvItemToQueue );             
394                                         xReturn = pdPASS;
395                 
396                                         /* Update the TxLock count so prvUnlockQueue knows to check for
397                                         tasks waiting for data to become available in the queue. */
398                                         ++( pxQueue->xTxLock );
399                                 }
400                                 else
401                                 {
402                                         xReturn = errQUEUE_FULL;
403                                 }
404                         }
405                         taskEXIT_CRITICAL();
406                 }
407
408                 if( xReturn == errQUEUE_FULL )
409                 {
410                         if( xTicksToWait > 0 )
411                         {
412                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
413                                 {
414                                         xReturn = queueERRONEOUS_UNBLOCK;
415                                 }
416                         }
417                 }
418         }
419         while( xReturn == queueERRONEOUS_UNBLOCK );
420
421         prvUnlockQueue( pxQueue );
422         xTaskResumeAll();
423
424         return xReturn;
425 }
426 /*-----------------------------------------------------------*/
427
428 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken )
429 {
430         /* Similar to xQueueSend, except we don't block if there is no room in the
431         queue.  Also we don't directly wake a task that was blocked on a queue
432         read, instead we return a flag to say whether a context switch is required
433         or not (i.e. has a task with a higher priority than us been woken by this
434         post). */
435         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
436         {
437                 prvCopyQueueData( pxQueue, pvItemToQueue );
438
439                 /* If the queue is locked we do not alter the event list.  This will
440                 be done when the queue is unlocked later. */
441                 if( pxQueue->xTxLock == queueUNLOCKED )
442                 {
443                         /* We only want to wake one task per ISR, so check that a task has
444                         not already been woken. */
445                         if( !xTaskPreviouslyWoken )             
446                         {
447                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
448                                 {
449                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
450                                         {
451                                                 /* The task waiting has a higher priority so record that a
452                                                 context switch is required. */
453                                                 return pdTRUE;
454                                         }
455                                 }
456                         }
457                 }
458                 else
459                 {
460                         /* Increment the lock count so the task that unlocks the queue
461                         knows that data was posted while it was locked. */
462                         ++( pxQueue->xTxLock );
463                 }
464         }
465
466         return xTaskPreviouslyWoken;
467 }
468 /*-----------------------------------------------------------*/
469
470 signed portBASE_TYPE xQueueReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
471 {
472 signed portBASE_TYPE xReturn = pdTRUE;
473 xTimeOutType xTimeOut;
474
475         /* This function is very similar to xQueueSend().  See comments within
476         xQueueSend() for a more detailed explanation.
477
478         Make sure other tasks do not access the queue. */
479         vTaskSuspendAll();
480
481         /* Capture the current time status for future reference. */
482         vTaskSetTimeOutState( &xTimeOut );
483
484         /* Make sure interrupts do not access the queue. */
485         prvLockQueue( pxQueue );
486
487         do
488         {
489                 /* If there are no messages in the queue we may have to block. */
490                 if( prvIsQueueEmpty( pxQueue ) )
491                 {
492                         /* There are no messages in the queue, do we want to block or just
493                         leave with nothing? */                  
494                         if( xTicksToWait > ( portTickType ) 0 )
495                         {
496                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
497                                 taskENTER_CRITICAL();
498                                 {
499                                         prvUnlockQueue( pxQueue );
500                                         if( !xTaskResumeAll() )
501                                         {
502                                                 taskYIELD();
503                                         }
504
505                                         if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
506                                         {
507                                                 /* We unblocked but the queue is empty.  We probably
508                                                 timed out. */
509                                                 xReturn = errQUEUE_EMPTY;
510                                         }
511         
512                                         vTaskSuspendAll();
513                                         prvLockQueue( pxQueue );
514                                 }
515                                 taskEXIT_CRITICAL();
516                         }
517                 }
518         
519                 if( xReturn != errQUEUE_EMPTY )
520                 {
521                         taskENTER_CRITICAL();
522                         {
523                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
524                                 {
525                                         pxQueue->pcReadFrom += pxQueue->uxItemSize;
526                                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )
527                                         {
528                                                 pxQueue->pcReadFrom = pxQueue->pcHead;
529                                         }
530                                         --( pxQueue->uxMessagesWaiting );
531                                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
532                 
533                                         /* Increment the lock count so prvUnlockQueue knows to check for
534                                         tasks waiting for space to become available on the queue. */
535                                         ++( pxQueue->xRxLock );
536                                         xReturn = pdPASS;
537                                 }
538                                 else
539                                 {
540                                         xReturn = errQUEUE_EMPTY;
541                                 }
542                         }
543                         taskEXIT_CRITICAL();
544                 }
545
546                 if( xReturn == errQUEUE_EMPTY )
547                 {
548                         if( xTicksToWait > 0 )
549                         {
550                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
551                                 {
552                                         xReturn = queueERRONEOUS_UNBLOCK;
553                                 }
554                         }
555                 }
556         } while( xReturn == queueERRONEOUS_UNBLOCK );
557
558         /* We no longer require exclusive access to the queue. */
559         prvUnlockQueue( pxQueue );
560         xTaskResumeAll();
561
562         return xReturn;
563 }
564 /*-----------------------------------------------------------*/
565
566 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken )
567 {
568 signed portBASE_TYPE xReturn;
569
570         /* We cannot block from an ISR, so check there is data available. */
571         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
572         {
573                 /* Copy the data from the queue. */
574                 pxQueue->pcReadFrom += pxQueue->uxItemSize;
575                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
576                 {
577                         pxQueue->pcReadFrom = pxQueue->pcHead;
578                 }
579                 --( pxQueue->uxMessagesWaiting );
580                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
581
582                 /* If the queue is locked we will not modify the event list.  Instead
583                 we update the lock count so the task that unlocks the queue will know
584                 that an ISR has removed data while the queue was locked. */
585                 if( pxQueue->xRxLock == queueUNLOCKED )
586                 {
587                         /* We only want to wake one task per ISR, so check that a task has
588                         not already been woken. */
589                         if( !( *pxTaskWoken ) )
590                         {
591                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
592                                 {
593                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
594                                         {
595                                                 /* The task waiting has a higher priority than us so
596                                                 force a context switch. */
597                                                 *pxTaskWoken = pdTRUE;
598                                         }
599                                 }
600                         }
601                 }
602                 else
603                 {
604                         /* Increment the lock count so the task that unlocks the queue
605                         knows that data was removed while it was locked. */
606                         ++( pxQueue->xRxLock );
607                 }
608
609                 xReturn = pdPASS;
610         }
611         else
612         {
613                 xReturn = pdFAIL;
614         }
615
616         return xReturn;
617 }
618 /*-----------------------------------------------------------*/
619
620 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle pxQueue )
621 {
622 unsigned portBASE_TYPE uxReturn;
623
624         taskENTER_CRITICAL();
625                 uxReturn = pxQueue->uxMessagesWaiting;
626         taskEXIT_CRITICAL();
627
628         return uxReturn;
629 }
630 /*-----------------------------------------------------------*/
631
632 void vQueueDelete( xQueueHandle pxQueue )
633 {
634         vPortFree( pxQueue->pcHead );
635         vPortFree( pxQueue );
636 }
637 /*-----------------------------------------------------------*/
638
639 static void prvUnlockQueue( xQueueHandle pxQueue )
640 {
641         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
642
643         /* The lock counts contains the number of extra data items placed or
644         removed from the queue while the queue was locked.  When a queue is
645         locked items can be added or removed, but the event lists cannot be
646         updated. */
647         taskENTER_CRITICAL();
648         {
649                 --( pxQueue->xTxLock );
650
651                 /* See if data was added to the queue while it was locked. */
652                 if( pxQueue->xTxLock > queueUNLOCKED )
653                 {
654                         pxQueue->xTxLock = queueUNLOCKED;
655
656                         /* Data was posted while the queue was locked.  Are any tasks
657                         blocked waiting for data to become available? */
658                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
659                         {
660                                 /* Tasks that are removed from the event list will get added to
661                                 the pending ready list as the scheduler is still suspended. */
662                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
663                                 {
664                                         /* The task waiting has a higher priority so record that a
665                                         context switch is required. */
666                                         vTaskMissedYield();
667                                 }
668                         }                       
669                 }
670         }
671         taskEXIT_CRITICAL();
672
673         /* Do the same for the Rx lock. */
674         taskENTER_CRITICAL();
675         {
676                 --( pxQueue->xRxLock );
677
678                 if( pxQueue->xRxLock > queueUNLOCKED )
679                 {
680                         pxQueue->xRxLock = queueUNLOCKED;
681
682                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
683                         {
684                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
685                                 {
686                                         vTaskMissedYield();
687                                 }
688                         }                       
689                 }
690         }
691         taskEXIT_CRITICAL();
692 }
693 /*-----------------------------------------------------------*/
694
695 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
696 {
697 signed portBASE_TYPE xReturn;
698
699         taskENTER_CRITICAL();
700                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
701         taskEXIT_CRITICAL();
702
703         return xReturn;
704 }
705 /*-----------------------------------------------------------*/
706
707 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
708 {
709 signed portBASE_TYPE xReturn;
710
711         taskENTER_CRITICAL();
712                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
713         taskEXIT_CRITICAL();
714
715         return xReturn;
716 }
717 /*-----------------------------------------------------------*/
718
719 #if configUSE_CO_ROUTINES == 1
720 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
721 {
722 signed portBASE_TYPE xReturn;
723                 
724         /* If the queue is already full we may have to block.  A critical section
725         is required to prevent an interrupt removing something from the queue 
726         between the check to see if the queue is full and blocking on the queue. */
727         portDISABLE_INTERRUPTS();
728         {
729                 if( prvIsQueueFull( pxQueue ) )
730                 {
731                         /* The queue is full - do we want to block or just leave without
732                         posting? */
733                         if( xTicksToWait > ( portTickType ) 0 )
734                         {
735                                 /* As this is called from a coroutine we cannot block directly, but
736                                 return indicating that we need to block. */
737                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );                          
738                                 portENABLE_INTERRUPTS();
739                                 return errQUEUE_BLOCKED;
740                         }
741                         else
742                         {
743                                 portENABLE_INTERRUPTS();
744                                 return errQUEUE_FULL;
745                         }
746                 }
747         }
748         portENABLE_INTERRUPTS();
749                 
750         portNOP();
751
752         portDISABLE_INTERRUPTS();
753         {
754                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
755                 {
756                         /* There is room in the queue, copy the data into the queue. */                 
757                         prvCopyQueueData( pxQueue, pvItemToQueue );             
758                         xReturn = pdPASS;
759
760                         /* Were any co-routines waiting for data to become available? */
761                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
762                         {
763                                 /* In this instance the co-routine could be placed directly 
764                                 into the ready list as we are within a critical section.  
765                                 Instead the same pending ready list mechansim is used as if
766                                 the event were caused from within an interrupt. */
767                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
768                                 {
769                                         /* The co-routine waiting has a higher priority so record 
770                                         that a yield might be appropriate. */
771                                         xReturn = errQUEUE_YIELD;
772                                 }
773                         }
774                 }
775                 else
776                 {
777                         xReturn = errQUEUE_FULL;
778                 }
779         }
780         portENABLE_INTERRUPTS();
781
782         return xReturn;
783 }
784 #endif
785 /*-----------------------------------------------------------*/
786
787 #if configUSE_CO_ROUTINES == 1
788 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
789 {
790 signed portBASE_TYPE xReturn;
791
792         /* If the queue is already empty we may have to block.  A critical section
793         is required to prevent an interrupt adding something to the queue 
794         between the check to see if the queue is empty and blocking on the queue. */
795         portDISABLE_INTERRUPTS();
796         {
797                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
798                 {
799                         /* There are no messages in the queue, do we want to block or just
800                         leave with nothing? */                  
801                         if( xTicksToWait > ( portTickType ) 0 )
802                         {
803                                 /* As this is a co-routine we cannot block directly, but return
804                                 indicating that we need to block. */
805                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
806                                 portENABLE_INTERRUPTS();
807                                 return errQUEUE_BLOCKED;
808                         }
809                         else
810                         {
811                                 portENABLE_INTERRUPTS();
812                                 return errQUEUE_FULL;
813                         }
814                 }
815         }
816         portENABLE_INTERRUPTS();
817
818         portNOP();
819
820         portDISABLE_INTERRUPTS();
821         {
822                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
823                 {
824                         /* Data is available from the queue. */
825                         pxQueue->pcReadFrom += pxQueue->uxItemSize;
826                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )
827                         {
828                                 pxQueue->pcReadFrom = pxQueue->pcHead;
829                         }
830                         --( pxQueue->uxMessagesWaiting );
831                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
832
833                         xReturn = pdPASS;
834
835                         /* Were any co-routines waiting for space to become available? */
836                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
837                         {
838                                 /* In this instance the co-routine could be placed directly 
839                                 into the ready list as we are within a critical section.  
840                                 Instead the same pending ready list mechansim is used as if
841                                 the event were caused from within an interrupt. */
842                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
843                                 {
844                                         xReturn = errQUEUE_YIELD;
845                                 }
846                         }       
847                 }
848                 else
849                 {
850                         xReturn = pdFAIL;
851                 }
852         }
853         portENABLE_INTERRUPTS();
854
855         return xReturn;
856 }
857 #endif
858 /*-----------------------------------------------------------*/
859
860
861
862 #if configUSE_CO_ROUTINES == 1
863 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
864 {
865         /* Cannot block within an ISR so if there is no space on the queue then
866         exit without doing anything. */
867         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
868         {
869                 prvCopyQueueData( pxQueue, pvItemToQueue );
870
871                 /* We only want to wake one co-routine per ISR, so check that a 
872                 co-routine has not already been woken. */
873                 if( !xCoRoutinePreviouslyWoken )                
874                 {
875                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
876                         {
877                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
878                                 {
879                                         return pdTRUE;
880                                 }
881                         }
882                 }
883         }
884
885         return xCoRoutinePreviouslyWoken;
886 }
887 #endif
888 /*-----------------------------------------------------------*/
889
890 #if configUSE_CO_ROUTINES == 1
891 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
892 {
893 signed portBASE_TYPE xReturn;
894
895         /* We cannot block from an ISR, so check there is data available. If
896         not then just leave without doing anything. */
897         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
898         {
899                 /* Copy the data from the queue. */
900                 pxQueue->pcReadFrom += pxQueue->uxItemSize;
901                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
902                 {
903                         pxQueue->pcReadFrom = pxQueue->pcHead;
904                 }
905                 --( pxQueue->uxMessagesWaiting );
906                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
907
908                 if( !( *pxCoRoutineWoken ) )
909                 {
910                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
911                         {
912                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
913                                 {
914                                         *pxCoRoutineWoken = pdTRUE;
915                                 }
916                         }
917                 }
918
919                 xReturn = pdPASS;
920         }
921         else
922         {
923                 xReturn = pdFAIL;
924         }
925
926         return xReturn;
927 }
928 #endif
929 /*-----------------------------------------------------------*/
930