semphr.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  27. */
  28. #ifndef SEMAPHORE_H
  29. #define SEMAPHORE_H
  30. #ifndef INC_FREERTOS_H
  31. #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
  32. #endif
  33. #include "queue.h"
  34. typedef QueueHandle_t SemaphoreHandle_t;
  35. #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( uint8_t ) 1U )
  36. #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( uint8_t ) 0U )
  37. #define semGIVE_BLOCK_TIME ( ( TickType_t ) 0U )
  38. /**
  39. * semphr. h
  40. * <pre>vSemaphoreCreateBinary( SemaphoreHandle_t xSemaphore )</pre>
  41. *
  42. * In many usage scenarios it is faster and more memory efficient to use a
  43. * direct to task notification in place of a binary semaphore!
  44. * http://www.freertos.org/RTOS-task-notifications.html
  45. *
  46. * This old vSemaphoreCreateBinary() macro is now deprecated in favour of the
  47. * xSemaphoreCreateBinary() function. Note that binary semaphores created using
  48. * the vSemaphoreCreateBinary() macro are created in a state such that the
  49. * first call to 'take' the semaphore would pass, whereas binary semaphores
  50. * created using xSemaphoreCreateBinary() are created in a state such that the
  51. * the semaphore must first be 'given' before it can be 'taken'.
  52. *
  53. * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
  54. * The queue length is 1 as this is a binary semaphore. The data size is 0
  55. * as we don't want to actually store any data - we just want to know if the
  56. * queue is empty or full.
  57. *
  58. * This type of semaphore can be used for pure synchronisation between tasks or
  59. * between an interrupt and a task. The semaphore need not be given back once
  60. * obtained, so one task/interrupt can continuously 'give' the semaphore while
  61. * another continuously 'takes' the semaphore. For this reason this type of
  62. * semaphore does not use a priority inheritance mechanism. For an alternative
  63. * that does use priority inheritance see xSemaphoreCreateMutex().
  64. *
  65. * @param xSemaphore Handle to the created semaphore. Should be of type SemaphoreHandle_t.
  66. *
  67. * Example usage:
  68. <pre>
  69. SemaphoreHandle_t xSemaphore = NULL;
  70. void vATask( void * pvParameters )
  71. {
  72. // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
  73. // This is a macro so pass the variable in directly.
  74. vSemaphoreCreateBinary( xSemaphore );
  75. if( xSemaphore != NULL )
  76. {
  77. // The semaphore was created successfully.
  78. // The semaphore can now be used.
  79. }
  80. }
  81. </pre>
  82. * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
  83. * \ingroup Semaphores
  84. */
  85. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  86. #define vSemaphoreCreateBinary( xSemaphore ) \
  87. { \
  88. ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
  89. if( ( xSemaphore ) != NULL ) \
  90. { \
  91. ( void ) xSemaphoreGive( ( xSemaphore ) ); \
  92. } \
  93. }
  94. #endif
  95. /**
  96. * semphr. h
  97. * <pre>SemaphoreHandle_t xSemaphoreCreateBinary( void )</pre>
  98. *
  99. * Creates a new binary semaphore instance, and returns a handle by which the
  100. * new semaphore can be referenced.
  101. *
  102. * In many usage scenarios it is faster and more memory efficient to use a
  103. * direct to task notification in place of a binary semaphore!
  104. * http://www.freertos.org/RTOS-task-notifications.html
  105. *
  106. * Internally, within the FreeRTOS implementation, binary semaphores use a block
  107. * of memory, in which the semaphore structure is stored. If a binary semaphore
  108. * is created using xSemaphoreCreateBinary() then the required memory is
  109. * automatically dynamically allocated inside the xSemaphoreCreateBinary()
  110. * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
  111. * is created using xSemaphoreCreateBinaryStatic() then the application writer
  112. * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
  113. * binary semaphore to be created without using any dynamic memory allocation.
  114. *
  115. * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this
  116. * xSemaphoreCreateBinary() function. Note that binary semaphores created using
  117. * the vSemaphoreCreateBinary() macro are created in a state such that the
  118. * first call to 'take' the semaphore would pass, whereas binary semaphores
  119. * created using xSemaphoreCreateBinary() are created in a state such that the
  120. * the semaphore must first be 'given' before it can be 'taken'.
  121. *
  122. * This type of semaphore can be used for pure synchronisation between tasks or
  123. * between an interrupt and a task. The semaphore need not be given back once
  124. * obtained, so one task/interrupt can continuously 'give' the semaphore while
  125. * another continuously 'takes' the semaphore. For this reason this type of
  126. * semaphore does not use a priority inheritance mechanism. For an alternative
  127. * that does use priority inheritance see xSemaphoreCreateMutex().
  128. *
  129. * @return Handle to the created semaphore, or NULL if the memory required to
  130. * hold the semaphore's data structures could not be allocated.
  131. *
  132. * Example usage:
  133. <pre>
  134. SemaphoreHandle_t xSemaphore = NULL;
  135. void vATask( void * pvParameters )
  136. {
  137. // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
  138. // This is a macro so pass the variable in directly.
  139. xSemaphore = xSemaphoreCreateBinary();
  140. if( xSemaphore != NULL )
  141. {
  142. // The semaphore was created successfully.
  143. // The semaphore can now be used.
  144. }
  145. }
  146. </pre>
  147. * \defgroup xSemaphoreCreateBinary xSemaphoreCreateBinary
  148. * \ingroup Semaphores
  149. */
  150. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  151. #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
  152. #endif
  153. /**
  154. * semphr. h
  155. * <pre>SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer )</pre>
  156. *
  157. * Creates a new binary semaphore instance, and returns a handle by which the
  158. * new semaphore can be referenced.
  159. *
  160. * NOTE: In many usage scenarios it is faster and more memory efficient to use a
  161. * direct to task notification in place of a binary semaphore!
  162. * http://www.freertos.org/RTOS-task-notifications.html
  163. *
  164. * Internally, within the FreeRTOS implementation, binary semaphores use a block
  165. * of memory, in which the semaphore structure is stored. If a binary semaphore
  166. * is created using xSemaphoreCreateBinary() then the required memory is
  167. * automatically dynamically allocated inside the xSemaphoreCreateBinary()
  168. * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
  169. * is created using xSemaphoreCreateBinaryStatic() then the application writer
  170. * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
  171. * binary semaphore to be created without using any dynamic memory allocation.
  172. *
  173. * This type of semaphore can be used for pure synchronisation between tasks or
  174. * between an interrupt and a task. The semaphore need not be given back once
  175. * obtained, so one task/interrupt can continuously 'give' the semaphore while
  176. * another continuously 'takes' the semaphore. For this reason this type of
  177. * semaphore does not use a priority inheritance mechanism. For an alternative
  178. * that does use priority inheritance see xSemaphoreCreateMutex().
  179. *
  180. * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
  181. * which will then be used to hold the semaphore's data structure, removing the
  182. * need for the memory to be allocated dynamically.
  183. *
  184. * @return If the semaphore is created then a handle to the created semaphore is
  185. * returned. If pxSemaphoreBuffer is NULL then NULL is returned.
  186. *
  187. * Example usage:
  188. <pre>
  189. SemaphoreHandle_t xSemaphore = NULL;
  190. StaticSemaphore_t xSemaphoreBuffer;
  191. void vATask( void * pvParameters )
  192. {
  193. // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
  194. // The semaphore's data structures will be placed in the xSemaphoreBuffer
  195. // variable, the address of which is passed into the function. The
  196. // function's parameter is not NULL, so the function will not attempt any
  197. // dynamic memory allocation, and therefore the function will not return
  198. // return NULL.
  199. xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );
  200. // Rest of task code goes here.
  201. }
  202. </pre>
  203. * \defgroup xSemaphoreCreateBinaryStatic xSemaphoreCreateBinaryStatic
  204. * \ingroup Semaphores
  205. */
  206. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  207. #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )
  208. #endif /* configSUPPORT_STATIC_ALLOCATION */
  209. /**
  210. * semphr. h
  211. * <pre>xSemaphoreTake(
  212. * SemaphoreHandle_t xSemaphore,
  213. * TickType_t xBlockTime
  214. * )</pre>
  215. *
  216. * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
  217. * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
  218. * xSemaphoreCreateCounting().
  219. *
  220. * @param xSemaphore A handle to the semaphore being taken - obtained when
  221. * the semaphore was created.
  222. *
  223. * @param xBlockTime The time in ticks to wait for the semaphore to become
  224. * available. The macro portTICK_PERIOD_MS can be used to convert this to a
  225. * real time. A block time of zero can be used to poll the semaphore. A block
  226. * time of portMAX_DELAY can be used to block indefinitely (provided
  227. * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
  228. *
  229. * @return pdTRUE if the semaphore was obtained. pdFALSE
  230. * if xBlockTime expired without the semaphore becoming available.
  231. *
  232. * Example usage:
  233. <pre>
  234. SemaphoreHandle_t xSemaphore = NULL;
  235. // A task that creates a semaphore.
  236. void vATask( void * pvParameters )
  237. {
  238. // Create the semaphore to guard a shared resource.
  239. xSemaphore = xSemaphoreCreateBinary();
  240. }
  241. // A task that uses the semaphore.
  242. void vAnotherTask( void * pvParameters )
  243. {
  244. // ... Do other things.
  245. if( xSemaphore != NULL )
  246. {
  247. // See if we can obtain the semaphore. If the semaphore is not available
  248. // wait 10 ticks to see if it becomes free.
  249. if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
  250. {
  251. // We were able to obtain the semaphore and can now access the
  252. // shared resource.
  253. // ...
  254. // We have finished accessing the shared resource. Release the
  255. // semaphore.
  256. xSemaphoreGive( xSemaphore );
  257. }
  258. else
  259. {
  260. // We could not obtain the semaphore and can therefore not access
  261. // the shared resource safely.
  262. }
  263. }
  264. }
  265. </pre>
  266. * \defgroup xSemaphoreTake xSemaphoreTake
  267. * \ingroup Semaphores
  268. */
  269. #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueSemaphoreTake( ( xSemaphore ), ( xBlockTime ) )
  270. /**
  271. * semphr. h
  272. * xSemaphoreTakeRecursive(
  273. * SemaphoreHandle_t xMutex,
  274. * TickType_t xBlockTime
  275. * )
  276. *
  277. * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
  278. * The mutex must have previously been created using a call to
  279. * xSemaphoreCreateRecursiveMutex();
  280. *
  281. * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
  282. * macro to be available.
  283. *
  284. * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
  285. *
  286. * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
  287. * doesn't become available again until the owner has called
  288. * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
  289. * if a task successfully 'takes' the same mutex 5 times then the mutex will
  290. * not be available to any other task until it has also 'given' the mutex back
  291. * exactly five times.
  292. *
  293. * @param xMutex A handle to the mutex being obtained. This is the
  294. * handle returned by xSemaphoreCreateRecursiveMutex();
  295. *
  296. * @param xBlockTime The time in ticks to wait for the semaphore to become
  297. * available. The macro portTICK_PERIOD_MS can be used to convert this to a
  298. * real time. A block time of zero can be used to poll the semaphore. If
  299. * the task already owns the semaphore then xSemaphoreTakeRecursive() will
  300. * return immediately no matter what the value of xBlockTime.
  301. *
  302. * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
  303. * expired without the semaphore becoming available.
  304. *
  305. * Example usage:
  306. <pre>
  307. SemaphoreHandle_t xMutex = NULL;
  308. // A task that creates a mutex.
  309. void vATask( void * pvParameters )
  310. {
  311. // Create the mutex to guard a shared resource.
  312. xMutex = xSemaphoreCreateRecursiveMutex();
  313. }
  314. // A task that uses the mutex.
  315. void vAnotherTask( void * pvParameters )
  316. {
  317. // ... Do other things.
  318. if( xMutex != NULL )
  319. {
  320. // See if we can obtain the mutex. If the mutex is not available
  321. // wait 10 ticks to see if it becomes free.
  322. if( xSemaphoreTakeRecursive( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
  323. {
  324. // We were able to obtain the mutex and can now access the
  325. // shared resource.
  326. // ...
  327. // For some reason due to the nature of the code further calls to
  328. // xSemaphoreTakeRecursive() are made on the same mutex. In real
  329. // code these would not be just sequential calls as this would make
  330. // no sense. Instead the calls are likely to be buried inside
  331. // a more complex call structure.
  332. xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
  333. xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
  334. // The mutex has now been 'taken' three times, so will not be
  335. // available to another task until it has also been given back
  336. // three times. Again it is unlikely that real code would have
  337. // these calls sequentially, but instead buried in a more complex
  338. // call structure. This is just for illustrative purposes.
  339. xSemaphoreGiveRecursive( xMutex );
  340. xSemaphoreGiveRecursive( xMutex );
  341. xSemaphoreGiveRecursive( xMutex );
  342. // Now the mutex can be taken by other tasks.
  343. }
  344. else
  345. {
  346. // We could not obtain the mutex and can therefore not access
  347. // the shared resource safely.
  348. }
  349. }
  350. }
  351. </pre>
  352. * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
  353. * \ingroup Semaphores
  354. */
  355. #if( configUSE_RECURSIVE_MUTEXES == 1 )
  356. #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )
  357. #endif
  358. /**
  359. * semphr. h
  360. * <pre>xSemaphoreGive( SemaphoreHandle_t xSemaphore )</pre>
  361. *
  362. * <i>Macro</i> to release a semaphore. The semaphore must have previously been
  363. * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
  364. * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
  365. *
  366. * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
  367. * an alternative which can be used from an ISR.
  368. *
  369. * This macro must also not be used on semaphores created using
  370. * xSemaphoreCreateRecursiveMutex().
  371. *
  372. * @param xSemaphore A handle to the semaphore being released. This is the
  373. * handle returned when the semaphore was created.
  374. *
  375. * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
  376. * Semaphores are implemented using queues. An error can occur if there is
  377. * no space on the queue to post a message - indicating that the
  378. * semaphore was not first obtained correctly.
  379. *
  380. * Example usage:
  381. <pre>
  382. SemaphoreHandle_t xSemaphore = NULL;
  383. void vATask( void * pvParameters )
  384. {
  385. // Create the semaphore to guard a shared resource.
  386. xSemaphore = vSemaphoreCreateBinary();
  387. if( xSemaphore != NULL )
  388. {
  389. if( xSemaphoreGive( xSemaphore ) != pdTRUE )
  390. {
  391. // We would expect this call to fail because we cannot give
  392. // a semaphore without first "taking" it!
  393. }
  394. // Obtain the semaphore - don't block if the semaphore is not
  395. // immediately available.
  396. if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) )
  397. {
  398. // We now have the semaphore and can access the shared resource.
  399. // ...
  400. // We have finished accessing the shared resource so can free the
  401. // semaphore.
  402. if( xSemaphoreGive( xSemaphore ) != pdTRUE )
  403. {
  404. // We would not expect this call to fail because we must have
  405. // obtained the semaphore to get here.
  406. }
  407. }
  408. }
  409. }
  410. </pre>
  411. * \defgroup xSemaphoreGive xSemaphoreGive
  412. * \ingroup Semaphores
  413. */
  414. #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
  415. /**
  416. * semphr. h
  417. * <pre>xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex )</pre>
  418. *
  419. * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
  420. * The mutex must have previously been created using a call to
  421. * xSemaphoreCreateRecursiveMutex();
  422. *
  423. * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
  424. * macro to be available.
  425. *
  426. * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
  427. *
  428. * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
  429. * doesn't become available again until the owner has called
  430. * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
  431. * if a task successfully 'takes' the same mutex 5 times then the mutex will
  432. * not be available to any other task until it has also 'given' the mutex back
  433. * exactly five times.
  434. *
  435. * @param xMutex A handle to the mutex being released, or 'given'. This is the
  436. * handle returned by xSemaphoreCreateMutex();
  437. *
  438. * @return pdTRUE if the semaphore was given.
  439. *
  440. * Example usage:
  441. <pre>
  442. SemaphoreHandle_t xMutex = NULL;
  443. // A task that creates a mutex.
  444. void vATask( void * pvParameters )
  445. {
  446. // Create the mutex to guard a shared resource.
  447. xMutex = xSemaphoreCreateRecursiveMutex();
  448. }
  449. // A task that uses the mutex.
  450. void vAnotherTask( void * pvParameters )
  451. {
  452. // ... Do other things.
  453. if( xMutex != NULL )
  454. {
  455. // See if we can obtain the mutex. If the mutex is not available
  456. // wait 10 ticks to see if it becomes free.
  457. if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )
  458. {
  459. // We were able to obtain the mutex and can now access the
  460. // shared resource.
  461. // ...
  462. // For some reason due to the nature of the code further calls to
  463. // xSemaphoreTakeRecursive() are made on the same mutex. In real
  464. // code these would not be just sequential calls as this would make
  465. // no sense. Instead the calls are likely to be buried inside
  466. // a more complex call structure.
  467. xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
  468. xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
  469. // The mutex has now been 'taken' three times, so will not be
  470. // available to another task until it has also been given back
  471. // three times. Again it is unlikely that real code would have
  472. // these calls sequentially, it would be more likely that the calls
  473. // to xSemaphoreGiveRecursive() would be called as a call stack
  474. // unwound. This is just for demonstrative purposes.
  475. xSemaphoreGiveRecursive( xMutex );
  476. xSemaphoreGiveRecursive( xMutex );
  477. xSemaphoreGiveRecursive( xMutex );
  478. // Now the mutex can be taken by other tasks.
  479. }
  480. else
  481. {
  482. // We could not obtain the mutex and can therefore not access
  483. // the shared resource safely.
  484. }
  485. }
  486. }
  487. </pre>
  488. * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
  489. * \ingroup Semaphores
  490. */
  491. #if( configUSE_RECURSIVE_MUTEXES == 1 )
  492. #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( ( xMutex ) )
  493. #endif
  494. /**
  495. * semphr. h
  496. * <pre>
  497. xSemaphoreGiveFromISR(
  498. SemaphoreHandle_t xSemaphore,
  499. BaseType_t *pxHigherPriorityTaskWoken
  500. )</pre>
  501. *
  502. * <i>Macro</i> to release a semaphore. The semaphore must have previously been
  503. * created with a call to xSemaphoreCreateBinary() or xSemaphoreCreateCounting().
  504. *
  505. * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
  506. * must not be used with this macro.
  507. *
  508. * This macro can be used from an ISR.
  509. *
  510. * @param xSemaphore A handle to the semaphore being released. This is the
  511. * handle returned when the semaphore was created.
  512. *
  513. * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
  514. * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
  515. * to unblock, and the unblocked task has a priority higher than the currently
  516. * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
  517. * a context switch should be requested before the interrupt is exited.
  518. *
  519. * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
  520. *
  521. * Example usage:
  522. <pre>
  523. \#define LONG_TIME 0xffff
  524. \#define TICKS_TO_WAIT 10
  525. SemaphoreHandle_t xSemaphore = NULL;
  526. // Repetitive task.
  527. void vATask( void * pvParameters )
  528. {
  529. for( ;; )
  530. {
  531. // We want this task to run every 10 ticks of a timer. The semaphore
  532. // was created before this task was started.
  533. // Block waiting for the semaphore to become available.
  534. if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
  535. {
  536. // It is time to execute.
  537. // ...
  538. // We have finished our task. Return to the top of the loop where
  539. // we will block on the semaphore until it is time to execute
  540. // again. Note when using the semaphore for synchronisation with an
  541. // ISR in this manner there is no need to 'give' the semaphore back.
  542. }
  543. }
  544. }
  545. // Timer ISR
  546. void vTimerISR( void * pvParameters )
  547. {
  548. static uint8_t ucLocalTickCount = 0;
  549. static BaseType_t xHigherPriorityTaskWoken;
  550. // A timer tick has occurred.
  551. // ... Do other time functions.
  552. // Is it time for vATask () to run?
  553. xHigherPriorityTaskWoken = pdFALSE;
  554. ucLocalTickCount++;
  555. if( ucLocalTickCount >= TICKS_TO_WAIT )
  556. {
  557. // Unblock the task by releasing the semaphore.
  558. xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
  559. // Reset the count so we release the semaphore again in 10 ticks time.
  560. ucLocalTickCount = 0;
  561. }
  562. if( xHigherPriorityTaskWoken != pdFALSE )
  563. {
  564. // We can force a context switch here. Context switching from an
  565. // ISR uses port specific syntax. Check the demo task for your port
  566. // to find the syntax required.
  567. }
  568. }
  569. </pre>
  570. * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
  571. * \ingroup Semaphores
  572. */
  573. #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )
  574. /**
  575. * semphr. h
  576. * <pre>
  577. xSemaphoreTakeFromISR(
  578. SemaphoreHandle_t xSemaphore,
  579. BaseType_t *pxHigherPriorityTaskWoken
  580. )</pre>
  581. *
  582. * <i>Macro</i> to take a semaphore from an ISR. The semaphore must have
  583. * previously been created with a call to xSemaphoreCreateBinary() or
  584. * xSemaphoreCreateCounting().
  585. *
  586. * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
  587. * must not be used with this macro.
  588. *
  589. * This macro can be used from an ISR, however taking a semaphore from an ISR
  590. * is not a common operation. It is likely to only be useful when taking a
  591. * counting semaphore when an interrupt is obtaining an object from a resource
  592. * pool (when the semaphore count indicates the number of resources available).
  593. *
  594. * @param xSemaphore A handle to the semaphore being taken. This is the
  595. * handle returned when the semaphore was created.
  596. *
  597. * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set
  598. * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task
  599. * to unblock, and the unblocked task has a priority higher than the currently
  600. * running task. If xSemaphoreTakeFromISR() sets this value to pdTRUE then
  601. * a context switch should be requested before the interrupt is exited.
  602. *
  603. * @return pdTRUE if the semaphore was successfully taken, otherwise
  604. * pdFALSE
  605. */
  606. #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
  607. /**
  608. * semphr. h
  609. * <pre>SemaphoreHandle_t xSemaphoreCreateMutex( void )</pre>
  610. *
  611. * Creates a new mutex type semaphore instance, and returns a handle by which
  612. * the new mutex can be referenced.
  613. *
  614. * Internally, within the FreeRTOS implementation, mutex semaphores use a block
  615. * of memory, in which the mutex structure is stored. If a mutex is created
  616. * using xSemaphoreCreateMutex() then the required memory is automatically
  617. * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
  618. * http://www.freertos.org/a00111.html). If a mutex is created using
  619. * xSemaphoreCreateMutexStatic() then the application writer must provided the
  620. * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
  621. * without using any dynamic memory allocation.
  622. *
  623. * Mutexes created using this function can be accessed using the xSemaphoreTake()
  624. * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
  625. * xSemaphoreGiveRecursive() macros must not be used.
  626. *
  627. * This type of semaphore uses a priority inheritance mechanism so a task
  628. * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
  629. * semaphore it is no longer required.
  630. *
  631. * Mutex type semaphores cannot be used from within interrupt service routines.
  632. *
  633. * See xSemaphoreCreateBinary() for an alternative implementation that can be
  634. * used for pure synchronisation (where one task or interrupt always 'gives' the
  635. * semaphore and another always 'takes' the semaphore) and from within interrupt
  636. * service routines.
  637. *
  638. * @return If the mutex was successfully created then a handle to the created
  639. * semaphore is returned. If there was not enough heap to allocate the mutex
  640. * data structures then NULL is returned.
  641. *
  642. * Example usage:
  643. <pre>
  644. SemaphoreHandle_t xSemaphore;
  645. void vATask( void * pvParameters )
  646. {
  647. // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
  648. // This is a macro so pass the variable in directly.
  649. xSemaphore = xSemaphoreCreateMutex();
  650. if( xSemaphore != NULL )
  651. {
  652. // The semaphore was created successfully.
  653. // The semaphore can now be used.
  654. }
  655. }
  656. </pre>
  657. * \defgroup xSemaphoreCreateMutex xSemaphoreCreateMutex
  658. * \ingroup Semaphores
  659. */
  660. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  661. #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
  662. #endif
  663. /**
  664. * semphr. h
  665. * <pre>SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
  666. *
  667. * Creates a new mutex type semaphore instance, and returns a handle by which
  668. * the new mutex can be referenced.
  669. *
  670. * Internally, within the FreeRTOS implementation, mutex semaphores use a block
  671. * of memory, in which the mutex structure is stored. If a mutex is created
  672. * using xSemaphoreCreateMutex() then the required memory is automatically
  673. * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
  674. * http://www.freertos.org/a00111.html). If a mutex is created using
  675. * xSemaphoreCreateMutexStatic() then the application writer must provided the
  676. * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
  677. * without using any dynamic memory allocation.
  678. *
  679. * Mutexes created using this function can be accessed using the xSemaphoreTake()
  680. * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
  681. * xSemaphoreGiveRecursive() macros must not be used.
  682. *
  683. * This type of semaphore uses a priority inheritance mechanism so a task
  684. * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
  685. * semaphore it is no longer required.
  686. *
  687. * Mutex type semaphores cannot be used from within interrupt service routines.
  688. *
  689. * See xSemaphoreCreateBinary() for an alternative implementation that can be
  690. * used for pure synchronisation (where one task or interrupt always 'gives' the
  691. * semaphore and another always 'takes' the semaphore) and from within interrupt
  692. * service routines.
  693. *
  694. * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
  695. * which will be used to hold the mutex's data structure, removing the need for
  696. * the memory to be allocated dynamically.
  697. *
  698. * @return If the mutex was successfully created then a handle to the created
  699. * mutex is returned. If pxMutexBuffer was NULL then NULL is returned.
  700. *
  701. * Example usage:
  702. <pre>
  703. SemaphoreHandle_t xSemaphore;
  704. StaticSemaphore_t xMutexBuffer;
  705. void vATask( void * pvParameters )
  706. {
  707. // A mutex cannot be used before it has been created. xMutexBuffer is
  708. // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is
  709. // attempted.
  710. xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );
  711. // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
  712. // so there is no need to check it.
  713. }
  714. </pre>
  715. * \defgroup xSemaphoreCreateMutexStatic xSemaphoreCreateMutexStatic
  716. * \ingroup Semaphores
  717. */
  718. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  719. #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )
  720. #endif /* configSUPPORT_STATIC_ALLOCATION */
  721. /**
  722. * semphr. h
  723. * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )</pre>
  724. *
  725. * Creates a new recursive mutex type semaphore instance, and returns a handle
  726. * by which the new recursive mutex can be referenced.
  727. *
  728. * Internally, within the FreeRTOS implementation, recursive mutexs use a block
  729. * of memory, in which the mutex structure is stored. If a recursive mutex is
  730. * created using xSemaphoreCreateRecursiveMutex() then the required memory is
  731. * automatically dynamically allocated inside the
  732. * xSemaphoreCreateRecursiveMutex() function. (see
  733. * http://www.freertos.org/a00111.html). If a recursive mutex is created using
  734. * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
  735. * provide the memory that will get used by the mutex.
  736. * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
  737. * be created without using any dynamic memory allocation.
  738. *
  739. * Mutexes created using this macro can be accessed using the
  740. * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
  741. * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
  742. *
  743. * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
  744. * doesn't become available again until the owner has called
  745. * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
  746. * if a task successfully 'takes' the same mutex 5 times then the mutex will
  747. * not be available to any other task until it has also 'given' the mutex back
  748. * exactly five times.
  749. *
  750. * This type of semaphore uses a priority inheritance mechanism so a task
  751. * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
  752. * semaphore it is no longer required.
  753. *
  754. * Mutex type semaphores cannot be used from within interrupt service routines.
  755. *
  756. * See xSemaphoreCreateBinary() for an alternative implementation that can be
  757. * used for pure synchronisation (where one task or interrupt always 'gives' the
  758. * semaphore and another always 'takes' the semaphore) and from within interrupt
  759. * service routines.
  760. *
  761. * @return xSemaphore Handle to the created mutex semaphore. Should be of type
  762. * SemaphoreHandle_t.
  763. *
  764. * Example usage:
  765. <pre>
  766. SemaphoreHandle_t xSemaphore;
  767. void vATask( void * pvParameters )
  768. {
  769. // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
  770. // This is a macro so pass the variable in directly.
  771. xSemaphore = xSemaphoreCreateRecursiveMutex();
  772. if( xSemaphore != NULL )
  773. {
  774. // The semaphore was created successfully.
  775. // The semaphore can now be used.
  776. }
  777. }
  778. </pre>
  779. * \defgroup xSemaphoreCreateRecursiveMutex xSemaphoreCreateRecursiveMutex
  780. * \ingroup Semaphores
  781. */
  782. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
  783. #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
  784. #endif
  785. /**
  786. * semphr. h
  787. * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
  788. *
  789. * Creates a new recursive mutex type semaphore instance, and returns a handle
  790. * by which the new recursive mutex can be referenced.
  791. *
  792. * Internally, within the FreeRTOS implementation, recursive mutexs use a block
  793. * of memory, in which the mutex structure is stored. If a recursive mutex is
  794. * created using xSemaphoreCreateRecursiveMutex() then the required memory is
  795. * automatically dynamically allocated inside the
  796. * xSemaphoreCreateRecursiveMutex() function. (see
  797. * http://www.freertos.org/a00111.html). If a recursive mutex is created using
  798. * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
  799. * provide the memory that will get used by the mutex.
  800. * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
  801. * be created without using any dynamic memory allocation.
  802. *
  803. * Mutexes created using this macro can be accessed using the
  804. * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
  805. * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
  806. *
  807. * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
  808. * doesn't become available again until the owner has called
  809. * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
  810. * if a task successfully 'takes' the same mutex 5 times then the mutex will
  811. * not be available to any other task until it has also 'given' the mutex back
  812. * exactly five times.
  813. *
  814. * This type of semaphore uses a priority inheritance mechanism so a task
  815. * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
  816. * semaphore it is no longer required.
  817. *
  818. * Mutex type semaphores cannot be used from within interrupt service routines.
  819. *
  820. * See xSemaphoreCreateBinary() for an alternative implementation that can be
  821. * used for pure synchronisation (where one task or interrupt always 'gives' the
  822. * semaphore and another always 'takes' the semaphore) and from within interrupt
  823. * service routines.
  824. *
  825. * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
  826. * which will then be used to hold the recursive mutex's data structure,
  827. * removing the need for the memory to be allocated dynamically.
  828. *
  829. * @return If the recursive mutex was successfully created then a handle to the
  830. * created recursive mutex is returned. If pxMutexBuffer was NULL then NULL is
  831. * returned.
  832. *
  833. * Example usage:
  834. <pre>
  835. SemaphoreHandle_t xSemaphore;
  836. StaticSemaphore_t xMutexBuffer;
  837. void vATask( void * pvParameters )
  838. {
  839. // A recursive semaphore cannot be used before it is created. Here a
  840. // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().
  841. // The address of xMutexBuffer is passed into the function, and will hold
  842. // the mutexes data structures - so no dynamic memory allocation will be
  843. // attempted.
  844. xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );
  845. // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
  846. // so there is no need to check it.
  847. }
  848. </pre>
  849. * \defgroup xSemaphoreCreateRecursiveMutexStatic xSemaphoreCreateRecursiveMutexStatic
  850. * \ingroup Semaphores
  851. */
  852. #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
  853. #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )
  854. #endif /* configSUPPORT_STATIC_ALLOCATION */
  855. /**
  856. * semphr. h
  857. * <pre>SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount )</pre>
  858. *
  859. * Creates a new counting semaphore instance, and returns a handle by which the
  860. * new counting semaphore can be referenced.
  861. *
  862. * In many usage scenarios it is faster and more memory efficient to use a
  863. * direct to task notification in place of a counting semaphore!
  864. * http://www.freertos.org/RTOS-task-notifications.html
  865. *
  866. * Internally, within the FreeRTOS implementation, counting semaphores use a
  867. * block of memory, in which the counting semaphore structure is stored. If a
  868. * counting semaphore is created using xSemaphoreCreateCounting() then the
  869. * required memory is automatically dynamically allocated inside the
  870. * xSemaphoreCreateCounting() function. (see
  871. * http://www.freertos.org/a00111.html). If a counting semaphore is created
  872. * using xSemaphoreCreateCountingStatic() then the application writer can
  873. * instead optionally provide the memory that will get used by the counting
  874. * semaphore. xSemaphoreCreateCountingStatic() therefore allows a counting
  875. * semaphore to be created without using any dynamic memory allocation.
  876. *
  877. * Counting semaphores are typically used for two things:
  878. *
  879. * 1) Counting events.
  880. *
  881. * In this usage scenario an event handler will 'give' a semaphore each time
  882. * an event occurs (incrementing the semaphore count value), and a handler
  883. * task will 'take' a semaphore each time it processes an event
  884. * (decrementing the semaphore count value). The count value is therefore
  885. * the difference between the number of events that have occurred and the
  886. * number that have been processed. In this case it is desirable for the
  887. * initial count value to be zero.
  888. *
  889. * 2) Resource management.
  890. *
  891. * In this usage scenario the count value indicates the number of resources
  892. * available. To obtain control of a resource a task must first obtain a
  893. * semaphore - decrementing the semaphore count value. When the count value
  894. * reaches zero there are no free resources. When a task finishes with the
  895. * resource it 'gives' the semaphore back - incrementing the semaphore count
  896. * value. In this case it is desirable for the initial count value to be
  897. * equal to the maximum count value, indicating that all resources are free.
  898. *
  899. * @param uxMaxCount The maximum count value that can be reached. When the
  900. * semaphore reaches this value it can no longer be 'given'.
  901. *
  902. * @param uxInitialCount The count value assigned to the semaphore when it is
  903. * created.
  904. *
  905. * @return Handle to the created semaphore. Null if the semaphore could not be
  906. * created.
  907. *
  908. * Example usage:
  909. <pre>
  910. SemaphoreHandle_t xSemaphore;
  911. void vATask( void * pvParameters )
  912. {
  913. SemaphoreHandle_t xSemaphore = NULL;
  914. // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
  915. // The max value to which the semaphore can count should be 10, and the
  916. // initial value assigned to the count should be 0.
  917. xSemaphore = xSemaphoreCreateCounting( 10, 0 );
  918. if( xSemaphore != NULL )
  919. {
  920. // The semaphore was created successfully.
  921. // The semaphore can now be used.
  922. }
  923. }
  924. </pre>
  925. * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
  926. * \ingroup Semaphores
  927. */
  928. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  929. #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
  930. #endif
  931. /**
  932. * semphr. h
  933. * <pre>SemaphoreHandle_t xSemaphoreCreateCountingStatic( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer )</pre>
  934. *
  935. * Creates a new counting semaphore instance, and returns a handle by which the
  936. * new counting semaphore can be referenced.
  937. *
  938. * In many usage scenarios it is faster and more memory efficient to use a
  939. * direct to task notification in place of a counting semaphore!
  940. * http://www.freertos.org/RTOS-task-notifications.html
  941. *
  942. * Internally, within the FreeRTOS implementation, counting semaphores use a
  943. * block of memory, in which the counting semaphore structure is stored. If a
  944. * counting semaphore is created using xSemaphoreCreateCounting() then the
  945. * required memory is automatically dynamically allocated inside the
  946. * xSemaphoreCreateCounting() function. (see
  947. * http://www.freertos.org/a00111.html). If a counting semaphore is created
  948. * using xSemaphoreCreateCountingStatic() then the application writer must
  949. * provide the memory. xSemaphoreCreateCountingStatic() therefore allows a
  950. * counting semaphore to be created without using any dynamic memory allocation.
  951. *
  952. * Counting semaphores are typically used for two things:
  953. *
  954. * 1) Counting events.
  955. *
  956. * In this usage scenario an event handler will 'give' a semaphore each time
  957. * an event occurs (incrementing the semaphore count value), and a handler
  958. * task will 'take' a semaphore each time it processes an event
  959. * (decrementing the semaphore count value). The count value is therefore
  960. * the difference between the number of events that have occurred and the
  961. * number that have been processed. In this case it is desirable for the
  962. * initial count value to be zero.
  963. *
  964. * 2) Resource management.
  965. *
  966. * In this usage scenario the count value indicates the number of resources
  967. * available. To obtain control of a resource a task must first obtain a
  968. * semaphore - decrementing the semaphore count value. When the count value
  969. * reaches zero there are no free resources. When a task finishes with the
  970. * resource it 'gives' the semaphore back - incrementing the semaphore count
  971. * value. In this case it is desirable for the initial count value to be
  972. * equal to the maximum count value, indicating that all resources are free.
  973. *
  974. * @param uxMaxCount The maximum count value that can be reached. When the
  975. * semaphore reaches this value it can no longer be 'given'.
  976. *
  977. * @param uxInitialCount The count value assigned to the semaphore when it is
  978. * created.
  979. *
  980. * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
  981. * which will then be used to hold the semaphore's data structure, removing the
  982. * need for the memory to be allocated dynamically.
  983. *
  984. * @return If the counting semaphore was successfully created then a handle to
  985. * the created counting semaphore is returned. If pxSemaphoreBuffer was NULL
  986. * then NULL is returned.
  987. *
  988. * Example usage:
  989. <pre>
  990. SemaphoreHandle_t xSemaphore;
  991. StaticSemaphore_t xSemaphoreBuffer;
  992. void vATask( void * pvParameters )
  993. {
  994. SemaphoreHandle_t xSemaphore = NULL;
  995. // Counting semaphore cannot be used before they have been created. Create
  996. // a counting semaphore using xSemaphoreCreateCountingStatic(). The max
  997. // value to which the semaphore can count is 10, and the initial value
  998. // assigned to the count will be 0. The address of xSemaphoreBuffer is
  999. // passed in and will be used to hold the semaphore structure, so no dynamic
  1000. // memory allocation will be used.
  1001. xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );
  1002. // No memory allocation was attempted so xSemaphore cannot be NULL, so there
  1003. // is no need to check its value.
  1004. }
  1005. </pre>
  1006. * \defgroup xSemaphoreCreateCountingStatic xSemaphoreCreateCountingStatic
  1007. * \ingroup Semaphores
  1008. */
  1009. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  1010. #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )
  1011. #endif /* configSUPPORT_STATIC_ALLOCATION */
  1012. /**
  1013. * semphr. h
  1014. * <pre>void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );</pre>
  1015. *
  1016. * Delete a semaphore. This function must be used with care. For example,
  1017. * do not delete a mutex type semaphore if the mutex is held by a task.
  1018. *
  1019. * @param xSemaphore A handle to the semaphore to be deleted.
  1020. *
  1021. * \defgroup vSemaphoreDelete vSemaphoreDelete
  1022. * \ingroup Semaphores
  1023. */
  1024. #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
  1025. /**
  1026. * semphr.h
  1027. * <pre>TaskHandle_t xSemaphoreGetMutexHolder( SemaphoreHandle_t xMutex );</pre>
  1028. *
  1029. * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
  1030. * If xMutex is not a mutex type semaphore, or the mutex is available (not held
  1031. * by a task), return NULL.
  1032. *
  1033. * Note: This is a good way of determining if the calling task is the mutex
  1034. * holder, but not a good way of determining the identity of the mutex holder as
  1035. * the holder may change between the function exiting and the returned value
  1036. * being tested.
  1037. */
  1038. #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )
  1039. /**
  1040. * semphr.h
  1041. * <pre>TaskHandle_t xSemaphoreGetMutexHolderFromISR( SemaphoreHandle_t xMutex );</pre>
  1042. *
  1043. * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
  1044. * If xMutex is not a mutex type semaphore, or the mutex is available (not held
  1045. * by a task), return NULL.
  1046. *
  1047. */
  1048. #define xSemaphoreGetMutexHolderFromISR( xSemaphore ) xQueueGetMutexHolderFromISR( ( xSemaphore ) )
  1049. /**
  1050. * semphr.h
  1051. * <pre>UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );</pre>
  1052. *
  1053. * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns
  1054. * its current count value. If the semaphore is a binary semaphore then
  1055. * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the
  1056. * semaphore is not available.
  1057. *
  1058. */
  1059. #define uxSemaphoreGetCount( xSemaphore ) uxQueueMessagesWaiting( ( QueueHandle_t ) ( xSemaphore ) )
  1060. #endif /* SEMAPHORE_H */