]> git.sur5r.net Git - freertos/blob - Source/include/queue.h
Update to V4.4.0.
[freertos] / Source / include / queue.h
1 /*\r
2         FreeRTOS.org V4.4.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 for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 #ifndef QUEUE_H\r
37 #define QUEUE_H\r
38 \r
39 typedef void * xQueueHandle;\r
40 \r
41 /**\r
42  * queue. h\r
43  * <pre>\r
44  xQueueHandle xQueueCreate( \r
45                               unsigned portBASE_TYPE uxQueueLength, \r
46                               unsigned portBASE_TYPE uxItemSize \r
47                           );\r
48  * </pre>\r
49  *\r
50  * Creates a new queue instance.  This allocates the storage required by the\r
51  * new queue and returns a handle for the queue.\r
52  *\r
53  * @param uxQueueLength The maximum number of items that the queue can contain.\r
54  *\r
55  * @param uxItemSize The number of bytes each item in the queue will require.  \r
56  * Items are queued by copy, not by reference, so this is the number of bytes\r
57  * that will be copied for each posted item.  Each item on the queue must be\r
58  * the same size.\r
59  *\r
60  * @return If the queue is successfully create then a handle to the newly \r
61  * created queue is returned.  If the queue cannot be created then 0 is\r
62  * returned.\r
63  * \r
64  * Example usage:\r
65    <pre>\r
66  struct AMessage\r
67  {\r
68     portCHAR ucMessageID;\r
69     portCHAR ucData[ 20 ];\r
70  };\r
71 \r
72  void vATask( void *pvParameters )\r
73  {\r
74  xQueueHandle xQueue1, xQueue2;\r
75 \r
76     // Create a queue capable of containing 10 unsigned long values.\r
77     xQueue1 = xQueueCreate( 10, sizeof( unsigned portLONG ) );\r
78     if( xQueue1 == 0 )\r
79     {\r
80         // Queue was not created and must not be used.\r
81     }\r
82 \r
83     // Create a queue capable of containing 10 pointers to AMessage structures.\r
84     // These should be passed by pointer as they contain a lot of data.\r
85     xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );\r
86     if( xQueue2 == 0 )\r
87     {\r
88         // Queue was not created and must not be used.\r
89     }\r
90 \r
91     // ... Rest of task code.\r
92  }\r
93  </pre>\r
94  * \defgroup xQueueCreate xQueueCreate\r
95  * \ingroup QueueManagement\r
96  */\r
97 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );\r
98 \r
99 /**\r
100  * queue. h\r
101  * <pre>\r
102  portBASE_TYPE xQueueSend( \r
103                              xQueueHandle xQueue, \r
104                              const void * pvItemToQueue, \r
105                              portTickType xTicksToWait \r
106                          );\r
107  * </pre>\r
108  *\r
109  * Post an item on a queue.  The item is queued by copy, not by reference.\r
110  * This function must not be called from an interrupt service routine.\r
111  * See xQueueSendFromISR () for an alternative which may be used in an ISR.\r
112  *\r
113  * @param xQueue The handle to the queue on which the item is to be posted.\r
114  * \r
115  * @param pvItemToQueue A pointer to the item that is to be placed on the \r
116  * queue.  The size of the items the queue will hold was defined when the\r
117  * queue was created, so this many bytes will be copied from pvItemToQueue\r
118  * into the queue storage area.\r
119  *\r
120  * @param xTicksToWait The maximum amount of time the task should block\r
121  * waiting for space to become available on the queue, should it already\r
122  * be full.  The call will return immediately if this is set to 0.  The\r
123  * time is defined in tick periods so the constant portTICK_RATE_MS \r
124  * should be used to convert to real time if this is required.\r
125  *\r
126  * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.\r
127  *\r
128  * Example usage:\r
129    <pre>\r
130  struct AMessage\r
131  {\r
132     portCHAR ucMessageID;\r
133     portCHAR ucData[ 20 ];\r
134  } xMessage;\r
135 \r
136  unsigned portLONG ulVar = 10UL;\r
137 \r
138  void vATask( void *pvParameters )\r
139  {\r
140  xQueueHandle xQueue1, xQueue2;\r
141  struct AMessage *pxMessage;\r
142 \r
143     // Create a queue capable of containing 10 unsigned long values.\r
144     xQueue1 = xQueueCreate( 10, sizeof( unsigned portLONG ) );\r
145 \r
146     // Create a queue capable of containing 10 pointers to AMessage structures.\r
147     // These should be passed by pointer as they contain a lot of data.\r
148     xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );\r
149 \r
150     // ...\r
151 \r
152     if( xQueue1 != 0 )\r
153     {\r
154         // Send an unsigned long.  Wait for 10 ticks for space to become \r
155         // available if necessary.\r
156         if( xQueueSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )\r
157         {\r
158             // Failed to post the message, even after 10 ticks.\r
159         }\r
160     }\r
161 \r
162     if( xQueue2 != 0 )\r
163     {\r
164         // Send a pointer to a struct AMessage object.  Don't block if the\r
165         // queue is already full.\r
166         pxMessage = & xMessage;\r
167         xQueueSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );\r
168     }\r
169 \r
170         // ... Rest of task code.\r
171  }\r
172  </pre>\r
173  * \defgroup xQueueSend xQueueSend\r
174  * \ingroup QueueManagement\r
175  */\r
176 signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );\r
177 \r
178 /**\r
179  * queue. h\r
180  * <pre>\r
181  portBASE_TYPE xQueueReceive( \r
182                                 xQueueHandle xQueue, \r
183                                 void *pvBuffer, \r
184                                 portTickType xTicksToWait \r
185                             );</pre>\r
186  *\r
187  * Receive an item from a queue.  The item is received by copy so a buffer of \r
188  * adequate size must be provided.  The number of bytes copied into the buffer\r
189  * was defined when the queue was created.\r
190  *\r
191  * This function must not be used in an interrupt service routine.  See\r
192  * xQueueReceiveFromISR for an alternative that can.\r
193  *\r
194  * @param pxQueue The handle to the queue from which the item is to be\r
195  * received.\r
196  *\r
197  * @param pvBuffer Pointer to the buffer into which the received item will\r
198  * be copied.\r
199  * \r
200  * @param xTicksToWait The maximum amount of time the task should block\r
201  * waiting for an item to receive should the queue be empty at the time\r
202  * of the call.    The time is defined in tick periods so the constant \r
203  * portTICK_RATE_MS should be used to convert to real time if this is required.\r
204  *\r
205  * @return pdTRUE if an item was successfully received from the queue,\r
206  * otherwise pdFALSE.\r
207  *\r
208  * Example usage:\r
209    <pre>\r
210  struct AMessage\r
211  {\r
212     portCHAR ucMessageID;\r
213     portCHAR ucData[ 20 ];\r
214  } xMessage;\r
215 \r
216  xQueueHandle xQueue;\r
217  \r
218  // Task to create a queue and post a value.\r
219  void vATask( void *pvParameters )\r
220  {\r
221  struct AMessage *pxMessage;\r
222 \r
223     // Create a queue capable of containing 10 pointers to AMessage structures.\r
224     // These should be passed by pointer as they contain a lot of data.\r
225     xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );\r
226     if( xQueue == 0 )\r
227     {\r
228         // Failed to create the queue.\r
229     }\r
230 \r
231     // ...\r
232 \r
233     // Send a pointer to a struct AMessage object.  Don't block if the\r
234     // queue is already full.\r
235     pxMessage = & xMessage;\r
236     xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );\r
237 \r
238         // ... Rest of task code.\r
239  }\r
240 \r
241  // Task to receive from the queue.\r
242  void vADifferentTask( void *pvParameters )\r
243  {\r
244  struct AMessage *pxRxedMessage;\r
245 \r
246     if( xQueue != 0 )\r
247     {\r
248         // Receive a message on the created queue.  Block for 10 ticks if a\r
249         // message is not immediately available.\r
250         if( xQueueReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )\r
251         {\r
252             // pcRxedMessage now points to the struct AMessage variable posted\r
253             // by vATask.\r
254         }\r
255     }\r
256 \r
257         // ... Rest of task code.\r
258  }\r
259  </pre>\r
260  * \defgroup xQueueReceive xQueueReceive\r
261  * \ingroup QueueManagement\r
262  */\r
263 signed portBASE_TYPE xQueueReceive( xQueueHandle xQueue, void *pvBuffer, portTickType xTicksToWait );\r
264 \r
265 /**\r
266  * queue. h\r
267  * <pre>unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle xQueue );</pre>\r
268  *\r
269  * Return the number of messages stored in a queue.\r
270  *\r
271  * @param xQueue A handle to the queue being queried.\r
272  * \r
273  * @return The number of messages available in the queue.\r
274  *\r
275  * \page uxQueueMessagesWaiting uxQueueMessagesWaiting\r
276  * \ingroup QueueManagement\r
277  */\r
278 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle xQueue );\r
279 \r
280 /**\r
281  * queue. h\r
282  * <pre>void vQueueDelete( xQueueHandle xQueue );</pre>\r
283  *\r
284  * Delete a queue - freeing all the memory allocated for storing of items\r
285  * placed on the queue.\r
286  * \r
287  * @param xQueue A handle to the queue to be deleted.\r
288  *\r
289  * \page vQueueDelete vQueueDelete\r
290  * \ingroup QueueManagement\r
291  */\r
292 void vQueueDelete( xQueueHandle xQueue );\r
293 \r
294 /**\r
295  * queue. h\r
296  * <pre>\r
297  portBASE_TYPE xQueueSendFromISR( \r
298                                     xQueueHandle pxQueue, \r
299                                     const void *pvItemToQueue, \r
300                                     portBASE_TYPE xTaskPreviouslyWoken \r
301                                 );\r
302  </pre>\r
303  *\r
304  * Post an item on a queue.  It is safe to use this function from within an\r
305  * interrupt service routine.\r
306  *\r
307  * Items are queued by copy not reference so it is preferable to only\r
308  * queue small items, especially when called from an ISR.  In most cases\r
309  * it would be preferable to store a pointer to the item being queued.\r
310  *\r
311  * @param xQueue The handle to the queue on which the item is to be posted.\r
312  * \r
313  * @param pvItemToQueue A pointer to the item that is to be placed on the \r
314  * queue.  The size of the items the queue will hold was defined when the\r
315  * queue was created, so this many bytes will be copied from pvItemToQueue\r
316  * into the queue storage area.\r
317  *\r
318  * @param cTaskPreviouslyWoken This is included so an ISR can post onto\r
319  * the same queue multiple times from a single interrupt.  The first call\r
320  * should always pass in pdFALSE.  Subsequent calls should pass in\r
321  * the value returned from the previous call.  See the file serial .c in the\r
322  * PC port for a good example of this mechanism.\r
323  *\r
324  * @return pdTRUE if a task was woken by posting onto the queue.  This is \r
325  * used by the ISR to determine if a context switch may be required following\r
326  * the ISR.\r
327  *\r
328  * Example usage for buffered IO (where the ISR can obtain more than one value\r
329  * per call):\r
330    <pre>\r
331  void vBufferISR( void )\r
332  {\r
333  portCHAR cIn;\r
334  portBASE_TYPE xTaskWokenByPost;\r
335 \r
336     // We have not woken a task at the start of the ISR.\r
337     cTaskWokenByPost = pdFALSE;\r
338 \r
339     // Loop until the buffer is empty.\r
340     do\r
341     {\r
342         // Obtain a byte from the buffer.\r
343         cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );                                            \r
344 \r
345         // Post the byte.  The first time round the loop cTaskWokenByPost\r
346         // will be pdFALSE.  If the queue send causes a task to wake we do\r
347         // not want the task to run until we have finished the ISR, so\r
348         // xQueueSendFromISR does not cause a context switch.  Also we \r
349         // don't want subsequent posts to wake any other tasks, so we store\r
350         // the return value back into cTaskWokenByPost so xQueueSendFromISR\r
351         // knows not to wake any task the next iteration of the loop.\r
352         xTaskWokenByPost = xQueueSendFromISR( xRxQueue, &cIn, cTaskWokenByPost );\r
353 \r
354     } while( portINPUT_BYTE( BUFFER_COUNT ) );\r
355 \r
356     // Now the buffer is empty we can switch context if necessary.\r
357     if( cTaskWokenByPost )\r
358     {\r
359         taskYIELD ();\r
360     }\r
361  }\r
362  </pre>\r
363  *\r
364  * \defgroup xQueueSendFromISR xQueueSendFromISR\r
365  * \ingroup QueueManagement\r
366  */\r
367 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );\r
368 \r
369 /**\r
370  * queue. h\r
371  * <pre>\r
372  portBASE_TYPE xQueueReceiveFromISR( \r
373                                        xQueueHandle pxQueue, \r
374                                        void *pvBuffer, \r
375                                        portBASE_TYPE *pxTaskWoken \r
376                                    ); \r
377  * </pre>\r
378  *\r
379  * Receive an item from a queue.  It is safe to use this function from within an\r
380  * interrupt service routine.\r
381  *\r
382  * @param pxQueue The handle to the queue from which the item is to be\r
383  * received.\r
384  *\r
385  * @param pvBuffer Pointer to the buffer into which the received item will\r
386  * be copied.\r
387  * \r
388  * @param pxTaskWoken A task may be blocked waiting for space to become\r
389  * available on the queue.  If xQueueReceiveFromISR causes such a task to\r
390  * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will\r
391  * remain unchanged.\r
392  *\r
393  * @return pdTRUE if an item was successfully received from the queue,\r
394  * otherwise pdFALSE.\r
395  *\r
396  * Example usage:\r
397    <pre>\r
398  \r
399  xQueueHandle xQueue;\r
400  \r
401  // Function to create a queue and post some values.\r
402  void vAFunction( void *pvParameters )\r
403  {\r
404  portCHAR cValueToPost;\r
405  const portTickType xBlockTime = ( portTickType )0xff;\r
406 \r
407     // Create a queue capable of containing 10 characters.\r
408     xQueue = xQueueCreate( 10, sizeof( portCHAR ) );\r
409     if( xQueue == 0 )\r
410     {\r
411         // Failed to create the queue.\r
412     }\r
413 \r
414     // ...\r
415 \r
416     // Post some characters that will be used within an ISR.  If the queue\r
417     // is full then this task will block for xBlockTime ticks.\r
418     cValueToPost = 'a';\r
419     xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );\r
420     cValueToPost = 'b';\r
421     xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );\r
422 \r
423     // ... keep posting characters ... this task may block when the queue\r
424     // becomes full.\r
425 \r
426     cValueToPost = 'c';\r
427     xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );\r
428  }\r
429 \r
430  // ISR that outputs all the characters received on the queue. \r
431  void vISR_Routine( void )\r
432  {\r
433  portBASE_TYPE xTaskWokenByReceive = pdFALSE;\r
434  portCHAR cRxedChar;\r
435 \r
436     while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, &xTaskWokenByReceive) )\r
437     {\r
438         // A character was received.  Output the character now.\r
439         vOutputCharacter( cRxedChar );\r
440 \r
441         // If removing the character from the queue woke the task that was \r
442         // posting onto the queue cTaskWokenByReceive will have been set to\r
443         // pdTRUE.  No matter how many times this loop iterates only one\r
444         // task will be woken.\r
445     }\r
446 \r
447     if( cTaskWokenByPost != ( portCHAR ) pdFALSE;\r
448     {\r
449         taskYIELD ();\r
450     }\r
451  }\r
452  </pre>\r
453  * \defgroup xQueueReceiveFromISR xQueueReceiveFromISR\r
454  * \ingroup QueueManagement\r
455  */\r
456 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
457 \r
458 \r
459 /* \r
460  * The functions defined above are for passing data to and from tasks.  The \r
461  * functions below are the equivalents for passing data to and from \r
462  * co-rtoutines.\r
463  *\r
464  * These functions are called from the co-routine macro implementation and\r
465  * should not be called directly from application code.  Instead use the macro\r
466  * wrappers defined within croutine.h.\r
467  */\r
468 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );\r
469 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );\r
470 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );\r
471 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );\r
472 \r
473 #endif\r
474 \r