semphr.h 48 KB

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