semphr.h 49 KB

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