2 FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
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
34 ***************************************************************************
\r
42 typedef xQueueHandle xSemaphoreHandle;
\r
44 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( unsigned portCHAR ) 1 )
\r
45 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portCHAR ) 0 )
\r
46 #define semGIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
51 * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>
\r
53 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
\r
54 * The queue length is 1 as this is a binary semaphore. The data size is 0
\r
55 * as we don't want to actually store any data - we just want to know if the
\r
56 * queue is empty or full.
\r
58 * This type of semaphore can be used for pure synchronisation between tasks or
\r
59 * between an interrupt and a task. The semaphore need not be given back once
\r
60 * obtained, so one task/interrupt can continuously 'give' the semaphore while
\r
61 * another continuously 'takes' the semaphore. For this reason this type of
\r
62 * semaphore does not use a priority inheritance mechanism. For an alternative
\r
63 * that does use priority inheritance see xSemaphoreCreateMutex().
\r
65 * @param xSemaphore Handle to the created semaphore. Should be of type xSemaphoreHandle.
\r
69 xSemaphoreHandle xSemaphore;
\r
71 void vATask( void * pvParameters )
\r
73 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
\r
74 // This is a macro so pass the variable in directly.
\r
75 vSemaphoreCreateBinary( xSemaphore );
\r
77 if( xSemaphore != NULL )
\r
79 // The semaphore was created successfully.
\r
80 // The semaphore can now be used.
\r
84 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
\r
85 * \ingroup Semaphores
\r
87 #define vSemaphoreCreateBinary( xSemaphore ) { \
\r
88 xSemaphore = xQueueCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH ); \
\r
89 if( xSemaphore != NULL ) \
\r
91 xSemaphoreGive( xSemaphore ); \
\r
98 * xSemaphoreHandle xSemaphore,
\r
99 * portTickType xBlockTime
\r
102 * <i>Macro</i> to obtain a semaphore. The semaphore must of been created using
\r
103 * vSemaphoreCreateBinary ().
\r
105 * @param xSemaphore A handle to the semaphore being obtained. This is the
\r
106 * handle returned by vSemaphoreCreateBinary ();
\r
108 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
109 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
110 * real time. A block time of zero can be used to poll the semaphore.
\r
112 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
\r
113 * expired without the semaphore becoming available.
\r
117 xSemaphoreHandle xSemaphore = NULL;
\r
119 // A task that creates a semaphore.
\r
120 void vATask( void * pvParameters )
\r
122 // Create the semaphore to guard a shared resource.
\r
123 vSemaphoreCreateBinary( xSemaphore );
\r
126 // A task that uses the semaphore.
\r
127 void vAnotherTask( void * pvParameters )
\r
129 // ... Do other things.
\r
131 if( xSemaphore != NULL )
\r
133 // See if we can obtain the semaphore. If the semaphore is not available
\r
134 // wait 10 ticks to see if it becomes free.
\r
135 if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
137 // We were able to obtain the semaphore and can now access the
\r
138 // shared resource.
\r
142 // We have finished accessing the shared resource. Release the
\r
144 xSemaphoreGive( xSemaphore );
\r
148 // We could not obtain the semaphore and can therefore not access
\r
149 // the shared resource safely.
\r
154 * \defgroup xSemaphoreTake xSemaphoreTake
\r
155 * \ingroup Semaphores
\r
157 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )
\r
158 #define xSemaphoreAltTake( xSemaphore, xBlockTime ) xQueueAltGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )
\r
162 * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>
\r
164 * <i>Macro</i> to release a semaphore. The semaphore must of been created using
\r
165 * vSemaphoreCreateBinary (), and obtained using sSemaphoreTake ().
\r
167 * This must not be used from an ISR. See xSemaphoreGiveFromISR () for
\r
168 * an alternative which can be used from an ISR.
\r
170 * @param xSemaphore A handle to the semaphore being released. This is the
\r
171 * handle returned by vSemaphoreCreateBinary ();
\r
173 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
\r
174 * Semaphores are implemented using queues. An error can occur if there is
\r
175 * no space on the queue to post a message - indicating that the
\r
176 * semaphore was not first obtained correctly.
\r
180 xSemaphoreHandle xSemaphore = NULL;
\r
182 void vATask( void * pvParameters )
\r
184 // Create the semaphore to guard a shared resource.
\r
185 vSemaphoreCreateBinary( xSemaphore );
\r
187 if( xSemaphore != NULL )
\r
189 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
191 // We would expect this call to fail because we cannot give
\r
192 // a semaphore without first "taking" it!
\r
195 // Obtain the semaphore - don't block if the semaphore is not
\r
196 // immediately available.
\r
197 if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
\r
199 // We now have the semaphore and can access the shared resource.
\r
203 // We have finished accessing the shared resource so can free the
\r
205 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
207 // We would not expect this call to fail because we must have
\r
208 // obtained the semaphore to get here.
\r
214 * \defgroup xSemaphoreGive xSemaphoreGive
\r
215 * \ingroup Semaphores
\r
217 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
218 #define xSemaphoreAltGive( xSemaphore ) xQueueAltGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
223 xSemaphoreGiveFromISR(
\r
224 xSemaphoreHandle xSemaphore,
\r
225 portSHORT sTaskPreviouslyWoken
\r
228 * <i>Macro</i> to release a semaphore. The semaphore must of been created using
\r
229 * vSemaphoreCreateBinary (), and obtained using xSemaphoreTake ().
\r
231 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
\r
232 * must not be used with this macro.
\r
234 * This macro can be used from an ISR.
\r
236 * @param xSemaphore A handle to the semaphore being released. This is the
\r
237 * handle returned by vSemaphoreCreateBinary ();
\r
239 * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls
\r
240 * to xSemaphoreGiveFromISR () from a single interrupt. The first call
\r
241 * should always pass in pdFALSE. Subsequent calls should pass in
\r
242 * the value returned from the previous call. See the file serial .c in the
\r
243 * PC port for a good example of using xSemaphoreGiveFromISR ().
\r
245 * @return pdTRUE if a task was woken by releasing the semaphore. This is
\r
246 * used by the ISR to determine if a context switch may be required following
\r
251 #define LONG_TIME 0xffff
\r
252 #define TICKS_TO_WAIT 10
\r
253 xSemaphoreHandle xSemaphore = NULL;
\r
255 // Repetitive task.
\r
256 void vATask( void * pvParameters )
\r
260 // We want this task to run every 10 ticks or a timer. The semaphore
\r
261 // was created before this task was started
\r
263 // Block waiting for the semaphore to become available.
\r
264 if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
\r
266 // It is time to execute.
\r
270 // We have finished our task. Return to the top of the loop where
\r
271 // we will block on the semaphore until it is time to execute
\r
278 void vTimerISR( void * pvParameters )
\r
280 static unsigned portCHAR ucLocalTickCount = 0;
\r
282 // A timer tick has occurred.
\r
284 // ... Do other time functions.
\r
286 // Is it time for vATask () to run?
\r
287 ucLocalTickCount++;
\r
288 if( ucLocalTickCount >= TICKS_TO_WAIT )
\r
290 // Unblock the task by releasing the semaphore.
\r
291 xSemaphoreGive( xSemaphore );
\r
293 // Reset the count so we release the semaphore again in 10 ticks time.
\r
294 ucLocalTickCount = 0;
\r
298 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
\r
299 * \ingroup Semaphores
\r
301 #define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken ) xQueueGenericSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken, queueSEND_TO_BACK )
\r
305 * <pre>xSemaphoreCreateMutex( xSemaphoreHandle xSemaphore )</pre>
\r
307 * <i>Macro</i> that implements a mutex semaphore by using the existing queue
\r
310 * This type of semaphore uses a priority inheritance mechanism so a task
\r
311 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
312 * semaphore it is no longer required.
\r
314 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
316 * See xSemaphoreCreateBinary() for an alternative implemnetation that can be
\r
317 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
318 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
319 * service routines.
\r
321 * @param xSemaphore Handle to the created mutex semaphore. Should be of type
\r
322 * xSemaphoreHandle.
\r
326 xSemaphoreHandle xSemaphore;
\r
328 void vATask( void * pvParameters )
\r
330 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
\r
331 // This is a macro so pass the variable in directly.
\r
332 vSemaphoreCreateMutex( xSemaphore );
\r
334 if( xSemaphore != NULL )
\r
336 // The semaphore was created successfully.
\r
337 // The semaphore can now be used.
\r
341 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
342 * \ingroup Semaphores
\r
344 #define xSemaphoreCreateMutex() xQueueCreateMutex()
\r
348 * <pre>vSemaphoreCreateCounting( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE uxMaxCount )</pre>
\r
350 * <i>Macro</i> that creates a counting semaphore by using the existing
\r
351 * queue mechanism. The queue length is used as the maximum count. The data
\r
352 * size is 0 as we don't want to actually store any data - we just want to
\r
353 * know if the queue is empty or full.
\r
355 * Counting semaphores are typically used for two things:
\r
357 * 1) Counting events.
\r
359 * In this usage scenario an event handler will 'give' a semphore each time
\r
360 * an event occurs (incrementing the semaphore count value), and a handler
\r
361 * task will 'take' a semaphore each time it processes an event
\r
362 * (decrementing the semaphore count value). The count value is therefore
\r
363 * the difference between the number of events that have occurred and the
\r
364 * number that have been processed. In this case it is desirable for the
\r
365 * initial count value to be zero.
\r
367 * 2) Resource management.
\r
369 * In this usage scenario the count value indicates the number of resources
\r
370 * available. To obtain control of a resource a task must first obtain a
\r
371 * semphoare - decrementing the semaphore count value. When the count value
\r
372 * reaches zero there are no free resources. When a task finishes with the
\r
373 * resource it 'gives' the semahore back - incrementing the semaphore count
\r
374 * value. In this case it is desirable for the initial count value to be
\r
375 * equal to the maximum count value, indicating that all resources are free.
\r
377 * @param uxMaxCount The maximum count value that can be reached. When the
\r
378 * semaphore reaches this value it can nolonger be 'given'.
\r
379 * @param uxInitialCount
\r
381 * @return Handle to the created semaphore. Should be of type xSemaphoreHandle.
\r
385 xSemaphoreHandle xSemaphore;
\r
387 void vATask( void * pvParameters )
\r
389 xSemaphoreHandle xSemaphore = NULL;
\r
391 // Semaphore cannot be used before a call to vSemaphoreCreateCounting().
\r
392 // This is a macro so pass the variable in directly.
\r
393 vSemaphoreCreateBinary( xSemaphore, );
\r
395 if( xSemaphore != NULL )
\r
397 // The semaphore was created successfully.
\r
398 // The semaphore can now be used.
\r
402 * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
\r
403 * \ingroup Semaphores
\r
405 #define xSemaphoreCreateCounting( uxCountValue, uxInitialCount ) xQueueCreateCountingSemaphore( uxCountValue, uxInitialCount )
\r
407 #endif /* SEMAPHORE_H */
\r