]> git.sur5r.net Git - freertos/blob - Source/queue.c
Update counting semaphore function prototype.
[freertos] / Source / queue.c
1 /*\r
2         FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \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
10 \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
15 \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
19 \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
24         can be applied.\r
25 \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
31         Also see http://www.SafeRTOS.com a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 #include <stdlib.h>\r
38 #include <string.h>\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 #include "croutine.h"\r
42 \r
43 /*-----------------------------------------------------------\r
44  * PUBLIC LIST API documented in list.h\r
45  *----------------------------------------------------------*/\r
46 \r
47 /* Constants used with the cRxLock and cTxLock structure members. */\r
48 #define queueUNLOCKED   ( ( signed portBASE_TYPE ) -1 )\r
49 #define queueERRONEOUS_UNBLOCK                                  ( -1 )\r
50 \r
51 /* For internal use only. */\r
52 #define queueSEND_TO_BACK       ( 0 )\r
53 #define queueSEND_TO_FRONT      ( 1 )\r
54 \r
55 /* Effectively make a union out of the xQUEUE structure. */\r
56 #define pxMutexHolder                           pcTail\r
57 #define uxQueueType                                     pcHead\r
58 #define uxRecursiveCallCount            pcReadFrom\r
59 #define queueQUEUE_IS_MUTEX                     NULL\r
60 \r
61 /* Semaphores do not actually store or copy data, so have an items size of\r
62 zero. */\r
63 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )\r
64 #define queueDONT_BLOCK                                  ( ( portTickType ) 0 )\r
65 #define queueMUTEX_GIVE_BLOCK_TIME               ( ( portTickType ) 0 )\r
66 /*\r
67  * Definition of the queue used by the scheduler.\r
68  * Items are queued by copy, not reference.\r
69  */\r
70 typedef struct QueueDefinition\r
71 {\r
72         signed portCHAR *pcHead;                                /*< Points to the beginning of the queue storage area. */\r
73         signed portCHAR *pcTail;                                /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */\r
74 \r
75         signed portCHAR *pcWriteTo;                             /*< Points to the free next place in the storage area. */\r
76         signed portCHAR *pcReadFrom;                    /*< Points to the last place that a queued item was read from. */\r
77 \r
78         xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
79         xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
80 \r
81         volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */\r
82         unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
83         unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */\r
84 \r
85         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
86         signed portBASE_TYPE xTxLock;                   /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
87 } xQUEUE;\r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /*\r
91  * Inside this file xQueueHandle is a pointer to a xQUEUE structure.\r
92  * To keep the definition private the API header file defines it as a\r
93  * pointer to void.\r
94  */\r
95 typedef xQUEUE * xQueueHandle;\r
96 \r
97 /*\r
98  * Prototypes for public functions are included here so we don't have to\r
99  * include the API header file (as it defines xQueueHandle differently).  These\r
100  * functions are documented in the API header file.\r
101  */\r
102 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );\r
103 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );\r
104 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue );\r
105 void vQueueDelete( xQueueHandle xQueue );\r
106 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition );\r
107 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );\r
108 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
109 xQueueHandle xQueueCreateMutex( void );\r
110 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );\r
111 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime );\r
112 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex );\r
113 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );\r
114 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );\r
115 \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
121 #endif\r
122 \r
123 /*\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
130  */\r
131 static void prvUnlockQueue( xQueueHandle pxQueue );\r
132 \r
133 /*\r
134  * Uses a critical section to determine if there is any data in a queue.\r
135  *\r
136  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
137  */\r
138 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue );\r
139 \r
140 /*\r
141  * Uses a critical section to determine if there is any space in a queue.\r
142  *\r
143  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
144  */\r
145 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue );\r
146 \r
147 /*\r
148  * Copies an item into the queue, either at the front of the queue or the\r
149  * back of the queue.\r
150  */\r
151 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition );\r
152 \r
153 /*\r
154  * Copies an item out of a queue.\r
155  */\r
156 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer );\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
161  * accessing the queue event lists.\r
162  */\r
163 #define prvLockQueue( pxQueue )                 \\r
164 {                                                                               \\r
165         taskENTER_CRITICAL();                           \\r
166                 ++( pxQueue->xRxLock );                 \\r
167                 ++( pxQueue->xTxLock );                 \\r
168         taskEXIT_CRITICAL();                            \\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 \r
173 /*-----------------------------------------------------------\r
174  * PUBLIC QUEUE MANAGEMENT API documented in queue.h\r
175  *----------------------------------------------------------*/\r
176 \r
177 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )\r
178 {\r
179 xQUEUE *pxNewQueue;\r
180 size_t xQueueSizeInBytes;\r
181 \r
182         /* Allocate the new queue structure. */\r
183         if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
184         {\r
185                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
186                 if( pxNewQueue != NULL )\r
187                 {\r
188                         /* Create the list of pointers to queue items.  The queue is one byte\r
189                         longer than asked for to make wrap checking easier/faster. */\r
190                         xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;\r
191 \r
192                         pxNewQueue->pcHead = ( signed portCHAR * ) pvPortMalloc( xQueueSizeInBytes );\r
193                         if( pxNewQueue->pcHead != NULL )\r
194                         {\r
195                                 /* Initialise the queue members as described above where the\r
196                                 queue type is defined. */\r
197                                 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );\r
198                                 pxNewQueue->uxMessagesWaiting = 0;\r
199                                 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;\r
200                                 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );\r
201                                 pxNewQueue->uxLength = uxQueueLength;\r
202                                 pxNewQueue->uxItemSize = uxItemSize;\r
203                                 pxNewQueue->xRxLock = queueUNLOCKED;\r
204                                 pxNewQueue->xTxLock = queueUNLOCKED;\r
205 \r
206                                 /* Likewise ensure the event queues start with the correct state. */\r
207                                 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
208                                 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
209 \r
210                                 return  pxNewQueue;\r
211                         }\r
212                         else\r
213                         {\r
214                                 vPortFree( pxNewQueue );\r
215                         }\r
216                 }\r
217         }\r
218 \r
219         /* Will only reach here if we could not allocate enough memory or no memory\r
220         was required. */\r
221         return NULL;\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 #if ( configUSE_MUTEXES == 1 )\r
226 \r
227         xQueueHandle xQueueCreateMutex( void )\r
228         {\r
229         xQUEUE *pxNewQueue;\r
230         \r
231                 /* Allocate the new queue structure. */\r
232                 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
233                 if( pxNewQueue != NULL )\r
234                 {\r
235                         /* Information required for priority inheritance. */\r
236                         pxNewQueue->pxMutexHolder = NULL;\r
237                         pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
238         \r
239                         /* Queues used as a mutex no data is actually copied into or out\r
240                         of the queue. */\r
241                         pxNewQueue->pcWriteTo = NULL;\r
242                         pxNewQueue->pcReadFrom = NULL;\r
243                         \r
244                         /* Each mutex has a length of 1 (like a binary semaphore) and\r
245                         an item size of 0 as nothing is actually copied into or out\r
246                         of the mutex. */\r
247                         pxNewQueue->uxMessagesWaiting = 0;\r
248                         pxNewQueue->uxLength = 1;\r
249                         pxNewQueue->uxItemSize = 0;\r
250                         pxNewQueue->xRxLock = queueUNLOCKED;\r
251                         pxNewQueue->xTxLock = queueUNLOCKED;\r
252         \r
253                         /* Ensure the event queues start with the correct state. */\r
254                         vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
255                         vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
256 \r
257                         /* Start with the semaphore in the expected state. */\r
258                         xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );\r
259                 }\r
260         \r
261                 return pxNewQueue;\r
262         }\r
263 \r
264 #endif /* configUSE_MUTEXES */\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 #if configUSE_RECURSIVE_MUTEXES == 1\r
268 \r
269         portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )\r
270         {\r
271         portBASE_TYPE xReturn;\r
272 \r
273                 /* If this is the task that holds the mutex then pxMutexHolder will not \r
274                 change outside of this task.  If this task does not hold the mutex then\r
275                 pxMutexHolder can never coincidentally equal the tasks handle, and as\r
276                 this is the only condition we are interested in it does not matter if\r
277                 pxMutexHolder is accessed simultaneously by another task.  Therefore no\r
278                 mutual exclusion is required to test the pxMutexHolder variable. */\r
279                 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
280                 {\r
281                         /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to\r
282                         the task handle, therefore no underflow check is required.  Also, \r
283                         uxRecursiveCallCount is only modified by the mutex holder, and as\r
284                         there can only be one, no mutual exclusion is required to modify the\r
285                         uxRecursiveCallCount member. */\r
286                         ( pxMutex->uxRecursiveCallCount )--;\r
287 \r
288                         /* Have we unwound the call count? */\r
289                         if( pxMutex->uxRecursiveCallCount == 0 )\r
290                         {\r
291                                 /* Return the mutex.  This will automatically unblock any other\r
292                                 task that might be waiting to access the mutex. */\r
293                 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );\r
294                         }\r
295 \r
296                         xReturn = pdPASS;\r
297                 }\r
298                 else\r
299                 {\r
300                         /* We cannot give the mutex because we are not the holder. */\r
301                         xReturn = pdFAIL;\r
302                 }\r
303 \r
304                 return xReturn;\r
305         }\r
306 \r
307 #endif /* configUSE_RECURSIVE_MUTEXES */\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 #if configUSE_RECURSIVE_MUTEXES == 1\r
311 \r
312         portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )\r
313         {\r
314         portBASE_TYPE xReturn;\r
315 \r
316                 /* Comments regarding mutual exclusion as per those within \r
317                 xQueueGiveMutexRecursive(). */\r
318 \r
319                 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
320                 {\r
321                         ( pxMutex->uxRecursiveCallCount )++;\r
322                         xReturn = pdPASS;\r
323                 }\r
324                 else\r
325                 {\r
326             xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );\r
327 \r
328                         /* pdPASS will only be returned if we successfully obtained the mutex,\r
329                         we may have blocked to reach here. */\r
330                         if( xReturn == pdPASS )\r
331                         {\r
332                                 ( pxMutex->uxRecursiveCallCount )++;\r
333                         }\r
334                 }\r
335 \r
336                 return xReturn;\r
337         }\r
338 \r
339 #endif /* configUSE_RECURSIVE_MUTEXES */\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 #if configUSE_COUNTING_SEMAPHORES == 1\r
343 \r
344         xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )\r
345         {\r
346         xQueueHandle pxHandle;\r
347         \r
348                 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );\r
349 \r
350                 if( pxHandle != NULL )\r
351                 {\r
352                         pxHandle->uxMessagesWaiting = uxInitialCount;\r
353                 }\r
354 \r
355                 return pxHandle;\r
356         }\r
357 \r
358 #endif /* configUSE_COUNTING_SEMAPHORES */\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
362 {\r
363 signed portBASE_TYPE xReturn = pdPASS;\r
364 xTimeOutType xTimeOut;\r
365 \r
366         /* Make sure other tasks do not access the queue. */\r
367         vTaskSuspendAll();\r
368 \r
369         /* Capture the current time status for future reference. */\r
370         vTaskSetTimeOutState( &xTimeOut );\r
371 \r
372         /* It is important that this is the only thread/ISR that modifies the\r
373         ready or delayed lists until xTaskResumeAll() is called.  Places where\r
374         the ready/delayed lists are modified include:\r
375 \r
376                 + vTaskDelay() -  Nothing can call vTaskDelay as the scheduler is\r
377                   suspended, vTaskDelay() cannot be called from an ISR.\r
378                 + vTaskPrioritySet() - Has a critical section around the access.\r
379                 + vTaskSwitchContext() - This will not get executed while the scheduler\r
380                   is suspended.\r
381                 + prvCheckDelayedTasks() - This will not get executed while the\r
382                   scheduler is suspended.\r
383                 + xTaskCreate() - Has a critical section around the access.\r
384                 + vTaskResume() - Has a critical section around the access.\r
385                 + xTaskResumeAll() - Has a critical section around the access.\r
386                 + xTaskRemoveFromEventList - Checks to see if the scheduler is\r
387                   suspended.  If so then the TCB being removed from the event is\r
388                   removed from the event and added to the xPendingReadyList.\r
389         */\r
390 \r
391         /* Make sure interrupts do not access the queue event list. */\r
392         prvLockQueue( pxQueue );\r
393 \r
394         /* It is important that interrupts to not access the event list of the\r
395         queue being modified here.  Places where the event list is modified\r
396         include:\r
397 \r
398                 + xQueueGenericSendFromISR().  This checks the lock on the queue to see\r
399                   if it has access.  If the queue is locked then the Tx lock count is\r
400                   incremented to signify that a task waiting for data can be made ready\r
401                   once the queue lock is removed.  If the queue is not locked then\r
402                   a task can be moved from the event list, but will not be removed\r
403                   from the delayed list or placed in the ready list until the scheduler\r
404                   is unlocked.\r
405 \r
406                 + xQueueReceiveFromISR().  As per xQueueGenericSendFromISR().\r
407         */\r
408                 \r
409         /* If the queue is already full we may have to block. */\r
410         do\r
411         {\r
412                 if( prvIsQueueFull( pxQueue ) )\r
413                 {\r
414                         /* The queue is full - do we want to block or just leave without\r
415                         posting? */\r
416                         if( xTicksToWait > ( portTickType ) 0 )\r
417                         {\r
418                                 /* We are going to place ourselves on the xTasksWaitingToSend event\r
419                                 list, and will get woken should the delay expire, or space become\r
420                                 available on the queue.\r
421                                 \r
422                                 As detailed above we do not require mutual exclusion on the event\r
423                                 list as nothing else can modify it or the ready lists while we\r
424                                 have the scheduler suspended and queue locked.\r
425                                 \r
426                                 It is possible that an ISR has removed data from the queue since we\r
427                                 checked if any was available.  If this is the case then the data\r
428                                 will have been copied from the queue, and the queue variables\r
429                                 updated, but the event list will not yet have been checked to see if\r
430                                 anything is waiting as the queue is locked. */\r
431                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
432         \r
433                                 /* Force a context switch now as we are blocked.  We can do\r
434                                 this from within a critical section as the task we are\r
435                                 switching to has its own context.  When we return here (i.e. we\r
436                                 unblock) we will leave the critical section as normal.\r
437                                 \r
438                                 It is possible that an ISR has caused an event on an unrelated and\r
439                                 unlocked queue.  If this was the case then the event list for that\r
440                                 queue will have been updated but the ready lists left unchanged -\r
441                                 instead the readied task will have been added to the pending ready\r
442                                 list. */\r
443                                 taskENTER_CRITICAL();\r
444                                 {\r
445                                         /* We can safely unlock the queue and scheduler here as\r
446                                         interrupts are disabled.  We must not yield with anything\r
447                                         locked, but we can yield from within a critical section.\r
448                                         \r
449                                         Tasks that have been placed on the pending ready list cannot\r
450                                         be tasks that are waiting for events on this queue.  See\r
451                                         in comment xTaskRemoveFromEventList(). */\r
452                                         prvUnlockQueue( pxQueue );\r
453         \r
454                                         /* Resuming the scheduler may cause a yield.  If so then there\r
455                                         is no point yielding again here. */\r
456                                         if( !xTaskResumeAll() )\r
457                                         {\r
458                                                 taskYIELD();\r
459                                         }\r
460 \r
461                                         /* We want to check to see if the queue is still full\r
462                                         before leaving the critical section.  This is to prevent\r
463                                         this task placing an item into the queue due to an\r
464                                         interrupt making space on the queue between critical\r
465                                         sections (when there might be a higher priority task\r
466                                         blocked on the queue that cannot run yet because the\r
467                                         scheduler gets suspended). */\r
468                                         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
469                                         {\r
470                                                 /* We unblocked but there is no space in the queue,\r
471                                                 we probably timed out. */\r
472                                                 xReturn = errQUEUE_FULL;\r
473                                         }\r
474         \r
475                                         /* Before leaving the critical section we have to ensure\r
476                                         exclusive access again. */\r
477                                         vTaskSuspendAll();\r
478                                         prvLockQueue( pxQueue );                                \r
479                                 }\r
480                                 taskEXIT_CRITICAL();\r
481                         }\r
482                 }\r
483                         \r
484                 /* If xReturn is errQUEUE_FULL then we unblocked when the queue\r
485                 was still full.  Don't check it again now as it is possible that\r
486                 an interrupt has removed an item from the queue since we left the\r
487                 critical section and we don't want to write to the queue in case\r
488                 there is a task of higher priority blocked waiting for space to\r
489                 be available on the queue.  If this is the case the higher priority\r
490                 task will execute when the scheduler is unsupended. */\r
491                 if( xReturn != errQUEUE_FULL )\r
492                 {\r
493                         /* When we are here it is possible that we unblocked as space became\r
494                         available on the queue.  It is also possible that an ISR posted to the\r
495                         queue since we left the critical section, so it may be that again there\r
496                         is no space.  This would only happen if a task and ISR post onto the\r
497                         same queue. */\r
498                         taskENTER_CRITICAL();\r
499                         {\r
500                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
501                                 {\r
502                                         /* There is room in the queue, copy the data into the queue. */                 \r
503                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
504                                         xReturn = pdPASS;\r
505                 \r
506                                         /* Update the TxLock count so prvUnlockQueue knows to check for\r
507                                         tasks waiting for data to become available in the queue. */\r
508                                         ++( pxQueue->xTxLock );\r
509                                 }\r
510                                 else\r
511                                 {\r
512                                         xReturn = errQUEUE_FULL;\r
513                                 }\r
514                         }\r
515                         taskEXIT_CRITICAL();\r
516                 }\r
517 \r
518                 if( xReturn == errQUEUE_FULL )\r
519                 {\r
520                         if( xTicksToWait > 0 )\r
521                         {\r
522                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
523                                 {\r
524                                         xReturn = queueERRONEOUS_UNBLOCK;\r
525                                 }\r
526                         }\r
527                 }\r
528         }\r
529         while( xReturn == queueERRONEOUS_UNBLOCK );\r
530 \r
531         prvUnlockQueue( pxQueue );\r
532         xTaskResumeAll();\r
533 \r
534         return xReturn;\r
535 }\r
536 /*-----------------------------------------------------------*/\r
537 \r
538 #if configUSE_ALTERNATIVE_API == 1\r
539 \r
540         signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
541         {\r
542         signed portBASE_TYPE xReturn;\r
543         xTimeOutType xTimeOut;\r
544 \r
545                 /* The source code that implements the alternative (Alt) API is much \r
546                 simpler because it executes everything from within a critical section.  \r
547                 This is the approach taken by many other RTOSes, but FreeRTOS.org has the \r
548                 preferred fully featured API too.  The fully featured API has more \r
549                 complex code that takes longer to execute, but makes much less use of \r
550                 critical sections.  Therefore the alternative API sacrifices interrupt \r
551                 responsiveness to gain execution speed, whereas the fully featured API\r
552                 sacrifices execution speed to ensure better interrupt responsiveness.  */\r
553 \r
554                 taskENTER_CRITICAL();\r
555                 {\r
556                         /* Capture the current time status for future reference. */\r
557                         vTaskSetTimeOutState( &xTimeOut );\r
558 \r
559                         /* If the queue is already full we may have to block. */\r
560                         do\r
561                         {\r
562                                 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
563                                 {\r
564                                         /* The queue is full - do we want to block or just leave without\r
565                                         posting? */\r
566                                         if( xTicksToWait > ( portTickType ) 0 )\r
567                                         {\r
568                                                 /* We are going to place ourselves on the xTasksWaitingToSend \r
569                                                 event list, and will get woken should the delay expire, or \r
570                                                 space become available on the queue. */\r
571                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
572                         \r
573                                                 /* Force a context switch now as we are blocked.  We can do\r
574                                                 this from within a critical section as the task we are\r
575                                                 switching to has its own context.  When we return here (i.e.\r
576                                                 we unblock) we will leave the critical section as normal. */\r
577                                                 taskYIELD();\r
578                                         }\r
579                                 }\r
580                                         \r
581                                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
582                                 {\r
583                                         /* There is room in the queue, copy the data into the queue. */                 \r
584                                         prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
585                                         xReturn = pdPASS;\r
586 \r
587                                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
588                                         {\r
589                                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
590                                                 {\r
591                                                         /* The task waiting has a higher priority. */\r
592                                                         taskYIELD();\r
593                                                 }\r
594                                         }                       \r
595                                 }\r
596                                 else\r
597                                 {\r
598                                         xReturn = errQUEUE_FULL;\r
599 \r
600                                         if( xTicksToWait > 0 )\r
601                                         {                                       \r
602                                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
603                                                 {\r
604                                                         /* Another task must have accessed the queue between \r
605                                                         this task unblocking and actually executing. */\r
606                                                         xReturn = queueERRONEOUS_UNBLOCK;\r
607                                                 }\r
608                                         }\r
609                                 }\r
610                         }\r
611                         while( xReturn == queueERRONEOUS_UNBLOCK );\r
612                 }\r
613                 taskEXIT_CRITICAL();\r
614 \r
615                 return xReturn;\r
616         }\r
617 \r
618 #endif /* configUSE_ALTERNATIVE_API */\r
619 /*-----------------------------------------------------------*/\r
620 \r
621 #if configUSE_ALTERNATIVE_API == 1\r
622 \r
623         signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
624         {\r
625         signed portBASE_TYPE xReturn = pdTRUE;\r
626         xTimeOutType xTimeOut;\r
627         signed portCHAR *pcOriginalReadPosition;\r
628 \r
629                 /* The source code that implements the alternative (Alt) API is much \r
630                 simpler because it executes everything from within a critical section.  \r
631                 This is the approach taken by many other RTOSes, but FreeRTOS.org has the \r
632                 preferred fully featured API too.  The fully featured API has more \r
633                 complex code that takes longer to execute, but makes much less use of \r
634                 critical sections.  Therefore the alternative API sacrifices interrupt \r
635                 responsiveness to gain execution speed, whereas the fully featured API\r
636                 sacrifices execution speed to ensure better interrupt responsiveness.  */\r
637 \r
638                 taskENTER_CRITICAL();\r
639                 {\r
640                         /* Capture the current time status for future reference. */\r
641                         vTaskSetTimeOutState( &xTimeOut );\r
642 \r
643                         do\r
644                         {\r
645                                 /* If there are no messages in the queue we may have to block. */\r
646                                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
647                                 {\r
648                                         /* There are no messages in the queue, do we want to block or just\r
649                                         leave with nothing? */                  \r
650                                         if( xTicksToWait > ( portTickType ) 0 )\r
651                                         {\r
652                                                 #if ( configUSE_MUTEXES == 1 )\r
653                                                 {\r
654                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
655                                                         {\r
656                                                                 vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );\r
657                                                         }\r
658                                                 }\r
659                                                 #endif\r
660                                                 \r
661                                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
662                                                 taskYIELD();\r
663                                         }\r
664                                 }\r
665                         \r
666                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
667                                 {\r
668                                         /* Remember our read position in case we are just peeking. */\r
669                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
670 \r
671                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
672 \r
673                                         if( xJustPeeking == pdFALSE )\r
674                                         {\r
675                                                 /* We are actually removing data. */\r
676                                                 --( pxQueue->uxMessagesWaiting );\r
677                                                         \r
678                                                 #if ( configUSE_MUTEXES == 1 )\r
679                                                 {\r
680                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
681                                                         {\r
682                                                                 /* Record the information required to implement\r
683                                                                 priority inheritance should it become necessary. */\r
684                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
685                                                         }\r
686                                                 }\r
687                                                 #endif\r
688 \r
689                                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
690                                                 {\r
691                                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
692                                                         {\r
693                                                                 /* The task waiting has a higher priority. */\r
694                                                                 taskYIELD();\r
695                                                         }\r
696                                                 }\r
697                                         }\r
698                                         else\r
699                                         {\r
700                                                 /* We are not removing the data, so reset our read\r
701                                                 pointer. */\r
702                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;\r
703                                         }\r
704                                         \r
705                                         xReturn = pdPASS;                                       \r
706                                 }\r
707                                 else\r
708                                 {\r
709                                         xReturn = errQUEUE_EMPTY;\r
710 \r
711                                         if( xTicksToWait > 0 )\r
712                                         {\r
713                                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
714                                                 {\r
715                                                         xReturn = queueERRONEOUS_UNBLOCK;\r
716                                                 }\r
717                                         }\r
718                                 }\r
719 \r
720                         } while( xReturn == queueERRONEOUS_UNBLOCK );\r
721                 }\r
722                 taskEXIT_CRITICAL();\r
723 \r
724                 return xReturn;\r
725         }\r
726 \r
727 #endif /* configUSE_ALTERNATIVE_API */\r
728 /*-----------------------------------------------------------*/\r
729 \r
730 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken, portBASE_TYPE xCopyPosition )\r
731 {\r
732         /* Similar to xQueueGenericSend, except we don't block if there is no room\r
733         in the queue.  Also we don't directly wake a task that was blocked on a\r
734         queue read, instead we return a flag to say whether a context switch is\r
735         required or not (i.e. has a task with a higher priority than us been woken\r
736         by this post). */\r
737         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
738         {\r
739                 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
740 \r
741                 /* If the queue is locked we do not alter the event list.  This will\r
742                 be done when the queue is unlocked later. */\r
743                 if( pxQueue->xTxLock == queueUNLOCKED )\r
744                 {\r
745                         /* We only want to wake one task per ISR, so check that a task has\r
746                         not already been woken. */\r
747                         if( !xTaskPreviouslyWoken )             \r
748                         {\r
749                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
750                                 {\r
751                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
752                                         {\r
753                                                 /* The task waiting has a higher priority so record that a\r
754                                                 context switch is required. */\r
755                                                 return pdTRUE;\r
756                                         }\r
757                                 }\r
758                         }\r
759                 }\r
760                 else\r
761                 {\r
762                         /* Increment the lock count so the task that unlocks the queue\r
763                         knows that data was posted while it was locked. */\r
764                         ++( pxQueue->xTxLock );\r
765                 }\r
766         }\r
767 \r
768         return xTaskPreviouslyWoken;\r
769 }\r
770 /*-----------------------------------------------------------*/\r
771 \r
772 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
773 {\r
774 signed portBASE_TYPE xReturn = pdTRUE;\r
775 xTimeOutType xTimeOut;\r
776 signed portCHAR *pcOriginalReadPosition;\r
777 \r
778         /* This function is very similar to xQueueGenericSend().  See comments\r
779         within xQueueGenericSend() for a more detailed explanation.\r
780 \r
781         Make sure other tasks do not access the queue. */\r
782         vTaskSuspendAll();\r
783 \r
784         /* Capture the current time status for future reference. */\r
785         vTaskSetTimeOutState( &xTimeOut );\r
786 \r
787         /* Make sure interrupts do not access the queue. */\r
788         prvLockQueue( pxQueue );\r
789 \r
790         do\r
791         {\r
792                 /* If there are no messages in the queue we may have to block. */\r
793                 if( prvIsQueueEmpty( pxQueue ) )\r
794                 {\r
795                         /* There are no messages in the queue, do we want to block or just\r
796                         leave with nothing? */                  \r
797                         if( xTicksToWait > ( portTickType ) 0 )\r
798                         {\r
799                                 #if ( configUSE_MUTEXES == 1 )\r
800                                 {\r
801                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
802                                         {\r
803                                                 portENTER_CRITICAL();\r
804                                                         vTaskPriorityInherit( ( void * const ) pxQueue->pxMutexHolder );\r
805                                                 portEXIT_CRITICAL();\r
806                                         }\r
807                                 }\r
808                                 #endif\r
809                                 \r
810                                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
811                                 taskENTER_CRITICAL();\r
812                                 {\r
813                                         prvUnlockQueue( pxQueue );\r
814                                         if( !xTaskResumeAll() )\r
815                                         {\r
816                                                 taskYIELD();\r
817                                         }\r
818 \r
819                                         if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
820                                         {\r
821                                                 /* We unblocked but the queue is empty.  We probably\r
822                                                 timed out. */\r
823                                                 xReturn = errQUEUE_EMPTY;\r
824                                         }\r
825         \r
826                                         vTaskSuspendAll();\r
827                                         prvLockQueue( pxQueue );\r
828                                 }\r
829                                 taskEXIT_CRITICAL();\r
830                         }\r
831                 }\r
832         \r
833                 if( xReturn != errQUEUE_EMPTY )\r
834                 {\r
835                         taskENTER_CRITICAL();\r
836                         {\r
837                                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
838                                 {\r
839                                         /* Remember our read position in case we are just peeking. */\r
840                                         pcOriginalReadPosition = pxQueue->pcReadFrom;\r
841 \r
842                                         prvCopyDataFromQueue( pxQueue, pvBuffer );\r
843 \r
844                                         if( xJustPeeking == pdFALSE )\r
845                                         {\r
846                                                 /* We are actually removing data. */\r
847                                                 --( pxQueue->uxMessagesWaiting );\r
848                                                         \r
849                                                 /* Increment the lock count so prvUnlockQueue knows to check for\r
850                                                 tasks waiting for space to become available on the queue. */\r
851                                                 ++( pxQueue->xRxLock );\r
852                                                 \r
853                                                 #if ( configUSE_MUTEXES == 1 )\r
854                                                 {\r
855                                                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
856                                                         {\r
857                                                                 /* Record the information required to implement\r
858                                                                 priority inheritance should it become necessary. */\r
859                                                                 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
860                                                         }\r
861                                                 }\r
862                                                 #endif\r
863                                         }\r
864                                         else\r
865                                         {\r
866                                                 /* We are not removing the data, so reset our read\r
867                                                 pointer. */\r
868                                                 pxQueue->pcReadFrom = pcOriginalReadPosition;\r
869 \r
870                                                 /* The data is being left in the queue, so increment the\r
871                                                 lock count so prvUnlockQueue knows to check for other\r
872                                                 tasks waiting for the data to be available. */\r
873                                                 ++( pxQueue->xTxLock );                                         \r
874                                         }\r
875                                         \r
876                                         xReturn = pdPASS;                                       \r
877                                 }\r
878                                 else\r
879                                 {\r
880                                         xReturn = errQUEUE_EMPTY;\r
881                                 }\r
882                         }\r
883                         taskEXIT_CRITICAL();\r
884                 }\r
885 \r
886                 if( xReturn == errQUEUE_EMPTY )\r
887                 {\r
888                         if( xTicksToWait > 0 )\r
889                         {\r
890                                 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
891                                 {\r
892                                         xReturn = queueERRONEOUS_UNBLOCK;\r
893                                 }\r
894                         }\r
895                 }\r
896         } while( xReturn == queueERRONEOUS_UNBLOCK );\r
897 \r
898         /* We no longer require exclusive access to the queue. */\r
899         prvUnlockQueue( pxQueue );\r
900         xTaskResumeAll();\r
901 \r
902         return xReturn;\r
903 }\r
904 /*-----------------------------------------------------------*/\r
905 \r
906 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, const void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )\r
907 {\r
908 signed portBASE_TYPE xReturn;\r
909 \r
910         /* We cannot block from an ISR, so check there is data available. */\r
911         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
912         {\r
913                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
914                 --( pxQueue->uxMessagesWaiting );\r
915 \r
916                 /* If the queue is locked we will not modify the event list.  Instead\r
917                 we update the lock count so the task that unlocks the queue will know\r
918                 that an ISR has removed data while the queue was locked. */\r
919                 if( pxQueue->xRxLock == queueUNLOCKED )\r
920                 {\r
921                         /* We only want to wake one task per ISR, so check that a task has\r
922                         not already been woken. */\r
923                         if( !( *pxTaskWoken ) )\r
924                         {\r
925                                 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
926                                 {\r
927                                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
928                                         {\r
929                                                 /* The task waiting has a higher priority than us so\r
930                                                 force a context switch. */\r
931                                                 *pxTaskWoken = pdTRUE;\r
932                                         }\r
933                                 }\r
934                         }\r
935                 }\r
936                 else\r
937                 {\r
938                         /* Increment the lock count so the task that unlocks the queue\r
939                         knows that data was removed while it was locked. */\r
940                         ++( pxQueue->xRxLock );\r
941                 }\r
942 \r
943                 xReturn = pdPASS;\r
944         }\r
945         else\r
946         {\r
947                 xReturn = pdFAIL;\r
948         }\r
949 \r
950         return xReturn;\r
951 }\r
952 /*-----------------------------------------------------------*/\r
953 \r
954 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )\r
955 {\r
956 unsigned portBASE_TYPE uxReturn;\r
957 \r
958         taskENTER_CRITICAL();\r
959                 uxReturn = pxQueue->uxMessagesWaiting;\r
960         taskEXIT_CRITICAL();\r
961 \r
962         return uxReturn;\r
963 }\r
964 /*-----------------------------------------------------------*/\r
965 \r
966 void vQueueDelete( xQueueHandle pxQueue )\r
967 {\r
968         vPortFree( pxQueue->pcHead );\r
969         vPortFree( pxQueue );\r
970 }\r
971 /*-----------------------------------------------------------*/\r
972 \r
973 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )\r
974 {\r
975         if( pxQueue->uxItemSize == 0 )\r
976         {\r
977                 #if ( configUSE_MUTEXES == 1 )\r
978                 {\r
979                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
980                         {\r
981                                 /* The mutex is no longer being held. */\r
982                                 vTaskPriorityDisinherit( ( void * const ) pxQueue->pxMutexHolder );\r
983                 pxQueue->pxMutexHolder = NULL;\r
984                         }\r
985                 }\r
986                 #endif\r
987         }\r
988         else if( xPosition == queueSEND_TO_BACK )\r
989         {\r
990                 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
991                 pxQueue->pcWriteTo += pxQueue->uxItemSize;\r
992                 if( pxQueue->pcWriteTo >= pxQueue->pcTail )\r
993                 {\r
994                         pxQueue->pcWriteTo = pxQueue->pcHead;\r
995                 }\r
996         }\r
997         else\r
998         {\r
999                 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );\r
1000                 pxQueue->pcReadFrom -= pxQueue->uxItemSize;\r
1001                 if( pxQueue->pcReadFrom < pxQueue->pcHead )\r
1002                 {\r
1003                         pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
1004                 }               \r
1005         }\r
1006 \r
1007         ++( pxQueue->uxMessagesWaiting );\r
1008 }\r
1009 /*-----------------------------------------------------------*/\r
1010 \r
1011 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )\r
1012 {\r
1013         if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
1014         {\r
1015                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1016                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1017                 {\r
1018                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1019                 }\r
1020                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1021         }       \r
1022 }\r
1023 /*-----------------------------------------------------------*/\r
1024 \r
1025 static void prvUnlockQueue( xQueueHandle pxQueue )\r
1026 {\r
1027         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
1028 \r
1029         /* The lock counts contains the number of extra data items placed or\r
1030         removed from the queue while the queue was locked.  When a queue is\r
1031         locked items can be added or removed, but the event lists cannot be\r
1032         updated. */\r
1033         taskENTER_CRITICAL();\r
1034         {\r
1035                 --( pxQueue->xTxLock );\r
1036 \r
1037                 /* See if data was added to the queue while it was locked. */\r
1038                 if( pxQueue->xTxLock > queueUNLOCKED )\r
1039                 {\r
1040                         pxQueue->xTxLock = queueUNLOCKED;\r
1041 \r
1042                         /* Data was posted while the queue was locked.  Are any tasks\r
1043                         blocked waiting for data to become available? */\r
1044                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
1045                         {\r
1046                                 /* Tasks that are removed from the event list will get added to\r
1047                                 the pending ready list as the scheduler is still suspended. */\r
1048                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1049                                 {\r
1050                                         /* The task waiting has a higher priority so record that a\r
1051                                         context switch is required. */\r
1052                                         vTaskMissedYield();\r
1053                                 }\r
1054                         }                       \r
1055                 }\r
1056         }\r
1057         taskEXIT_CRITICAL();\r
1058 \r
1059         /* Do the same for the Rx lock. */\r
1060         taskENTER_CRITICAL();\r
1061         {\r
1062                 --( pxQueue->xRxLock );\r
1063 \r
1064                 if( pxQueue->xRxLock > queueUNLOCKED )\r
1065                 {\r
1066                         pxQueue->xRxLock = queueUNLOCKED;\r
1067 \r
1068                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
1069                         {\r
1070                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1071                                 {\r
1072                                         vTaskMissedYield();\r
1073                                 }\r
1074                         }                       \r
1075                 }\r
1076         }\r
1077         taskEXIT_CRITICAL();\r
1078 }\r
1079 /*-----------------------------------------------------------*/\r
1080 \r
1081 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )\r
1082 {\r
1083 signed portBASE_TYPE xReturn;\r
1084 \r
1085         taskENTER_CRITICAL();\r
1086                 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );\r
1087         taskEXIT_CRITICAL();\r
1088 \r
1089         return xReturn;\r
1090 }\r
1091 /*-----------------------------------------------------------*/\r
1092 \r
1093 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )\r
1094 {\r
1095 signed portBASE_TYPE xReturn;\r
1096 \r
1097         taskENTER_CRITICAL();\r
1098                 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );\r
1099         taskEXIT_CRITICAL();\r
1100 \r
1101         return xReturn;\r
1102 }\r
1103 /*-----------------------------------------------------------*/\r
1104 \r
1105 #if configUSE_CO_ROUTINES == 1\r
1106 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
1107 {\r
1108 signed portBASE_TYPE xReturn;\r
1109                 \r
1110         /* If the queue is already full we may have to block.  A critical section\r
1111         is required to prevent an interrupt removing something from the queue\r
1112         between the check to see if the queue is full and blocking on the queue. */\r
1113         portDISABLE_INTERRUPTS();\r
1114         {\r
1115                 if( prvIsQueueFull( pxQueue ) )\r
1116                 {\r
1117                         /* The queue is full - do we want to block or just leave without\r
1118                         posting? */\r
1119                         if( xTicksToWait > ( portTickType ) 0 )\r
1120                         {\r
1121                                 /* As this is called from a coroutine we cannot block directly, but\r
1122                                 return indicating that we need to block. */\r
1123                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );                          \r
1124                                 portENABLE_INTERRUPTS();\r
1125                                 return errQUEUE_BLOCKED;\r
1126                         }\r
1127                         else\r
1128                         {\r
1129                                 portENABLE_INTERRUPTS();\r
1130                                 return errQUEUE_FULL;\r
1131                         }\r
1132                 }\r
1133         }\r
1134         portENABLE_INTERRUPTS();\r
1135                 \r
1136         portNOP();\r
1137 \r
1138         portDISABLE_INTERRUPTS();\r
1139         {\r
1140                 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1141                 {\r
1142                         /* There is room in the queue, copy the data into the queue. */                 \r
1143                         prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1144                         xReturn = pdPASS;\r
1145 \r
1146                         /* Were any co-routines waiting for data to become available? */\r
1147                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
1148                         {\r
1149                                 /* In this instance the co-routine could be placed directly\r
1150                                 into the ready list as we are within a critical section.\r
1151                                 Instead the same pending ready list mechanism is used as if\r
1152                                 the event were caused from within an interrupt. */\r
1153                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1154                                 {\r
1155                                         /* The co-routine waiting has a higher priority so record\r
1156                                         that a yield might be appropriate. */\r
1157                                         xReturn = errQUEUE_YIELD;\r
1158                                 }\r
1159                         }\r
1160                 }\r
1161                 else\r
1162                 {\r
1163                         xReturn = errQUEUE_FULL;\r
1164                 }\r
1165         }\r
1166         portENABLE_INTERRUPTS();\r
1167 \r
1168         return xReturn;\r
1169 }\r
1170 #endif\r
1171 /*-----------------------------------------------------------*/\r
1172 \r
1173 #if configUSE_CO_ROUTINES == 1\r
1174 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )\r
1175 {\r
1176 signed portBASE_TYPE xReturn;\r
1177 \r
1178         /* If the queue is already empty we may have to block.  A critical section\r
1179         is required to prevent an interrupt adding something to the queue\r
1180         between the check to see if the queue is empty and blocking on the queue. */\r
1181         portDISABLE_INTERRUPTS();\r
1182         {\r
1183                 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
1184                 {\r
1185                         /* There are no messages in the queue, do we want to block or just\r
1186                         leave with nothing? */                  \r
1187                         if( xTicksToWait > ( portTickType ) 0 )\r
1188                         {\r
1189                                 /* As this is a co-routine we cannot block directly, but return\r
1190                                 indicating that we need to block. */\r
1191                                 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );\r
1192                                 portENABLE_INTERRUPTS();\r
1193                                 return errQUEUE_BLOCKED;\r
1194                         }\r
1195                         else\r
1196                         {\r
1197                                 portENABLE_INTERRUPTS();\r
1198                                 return errQUEUE_FULL;\r
1199                         }\r
1200                 }\r
1201         }\r
1202         portENABLE_INTERRUPTS();\r
1203 \r
1204         portNOP();\r
1205 \r
1206         portDISABLE_INTERRUPTS();\r
1207         {\r
1208                 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1209                 {\r
1210                         /* Data is available from the queue. */\r
1211                         pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1212                         if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1213                         {\r
1214                                 pxQueue->pcReadFrom = pxQueue->pcHead;\r
1215                         }\r
1216                         --( pxQueue->uxMessagesWaiting );\r
1217                         memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1218 \r
1219                         xReturn = pdPASS;\r
1220 \r
1221                         /* Were any co-routines waiting for space to become available? */\r
1222                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
1223                         {\r
1224                                 /* In this instance the co-routine could be placed directly\r
1225                                 into the ready list as we are within a critical section.\r
1226                                 Instead the same pending ready list mechanism is used as if\r
1227                                 the event were caused from within an interrupt. */\r
1228                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1229                                 {\r
1230                                         xReturn = errQUEUE_YIELD;\r
1231                                 }\r
1232                         }       \r
1233                 }\r
1234                 else\r
1235                 {\r
1236                         xReturn = pdFAIL;\r
1237                 }\r
1238         }\r
1239         portENABLE_INTERRUPTS();\r
1240 \r
1241         return xReturn;\r
1242 }\r
1243 #endif\r
1244 /*-----------------------------------------------------------*/\r
1245 \r
1246 \r
1247 \r
1248 #if configUSE_CO_ROUTINES == 1\r
1249 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
1250 {\r
1251         /* Cannot block within an ISR so if there is no space on the queue then\r
1252         exit without doing anything. */\r
1253         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
1254         {\r
1255                 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );\r
1256 \r
1257                 /* We only want to wake one co-routine per ISR, so check that a\r
1258                 co-routine has not already been woken. */\r
1259                 if( !xCoRoutinePreviouslyWoken )                \r
1260                 {\r
1261                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )\r
1262                         {\r
1263                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1264                                 {\r
1265                                         return pdTRUE;\r
1266                                 }\r
1267                         }\r
1268                 }\r
1269         }\r
1270 \r
1271         return xCoRoutinePreviouslyWoken;\r
1272 }\r
1273 #endif\r
1274 /*-----------------------------------------------------------*/\r
1275 \r
1276 #if configUSE_CO_ROUTINES == 1\r
1277 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
1278 {\r
1279 signed portBASE_TYPE xReturn;\r
1280 \r
1281         /* We cannot block from an ISR, so check there is data available. If\r
1282         not then just leave without doing anything. */\r
1283         if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
1284         {\r
1285                 /* Copy the data from the queue. */\r
1286                 pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
1287                 if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
1288                 {\r
1289                         pxQueue->pcReadFrom = pxQueue->pcHead;\r
1290                 }\r
1291                 --( pxQueue->uxMessagesWaiting );\r
1292                 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
1293 \r
1294                 if( !( *pxCoRoutineWoken ) )\r
1295                 {\r
1296                         if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )\r
1297                         {\r
1298                                 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1299                                 {\r
1300                                         *pxCoRoutineWoken = pdTRUE;\r
1301                                 }\r
1302                         }\r
1303                 }\r
1304 \r
1305                 xReturn = pdPASS;\r
1306         }\r
1307         else\r
1308         {\r
1309                 xReturn = pdFAIL;\r
1310         }\r
1311 \r
1312         return xReturn;\r
1313 }\r
1314 #endif\r
1315 /*-----------------------------------------------------------*/\r
1316 \r