lock.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_LOCK_H__
  20. #define __PJ_LOCK_H__
  21. /**
  22. * @file lock.h
  23. * @brief Higher abstraction for locking objects.
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_LOCK Lock Objects
  29. * @ingroup PJ_OS
  30. * @{
  31. *
  32. * <b>Lock Objects</b> are higher abstraction for different lock mechanisms.
  33. * It offers the same API for manipulating different lock types (e.g.
  34. * @ref PJ_MUTEX "mutex", @ref PJ_SEM "semaphores", or null locks).
  35. * Because Lock Objects have the same API for different types of lock
  36. * implementation, it can be passed around in function arguments. As the
  37. * result, it can be used to control locking policy for a particular
  38. * feature.
  39. */
  40. /**
  41. * Create simple, non recursive mutex lock object.
  42. *
  43. * @param pool Memory pool.
  44. * @param name Lock object's name.
  45. * @param lock Pointer to store the returned handle.
  46. *
  47. * @return PJ_SUCCESS or the appropriate error code.
  48. */
  49. PJ_DECL(pj_status_t) pj_lock_create_simple_mutex( pj_pool_t *pool,
  50. const char *name,
  51. pj_lock_t **lock );
  52. /**
  53. * Create recursive mutex lock object.
  54. *
  55. * @param pool Memory pool.
  56. * @param name Lock object's name.
  57. * @param lock Pointer to store the returned handle.
  58. *
  59. * @return PJ_SUCCESS or the appropriate error code.
  60. */
  61. PJ_DECL(pj_status_t) pj_lock_create_recursive_mutex( pj_pool_t *pool,
  62. const char *name,
  63. pj_lock_t **lock );
  64. /**
  65. * Create NULL mutex. A NULL mutex doesn't actually have any synchronization
  66. * object attached to it.
  67. *
  68. * @param pool Memory pool.
  69. * @param name Lock object's name.
  70. * @param lock Pointer to store the returned handle.
  71. *
  72. * @return PJ_SUCCESS or the appropriate error code.
  73. */
  74. PJ_DECL(pj_status_t) pj_lock_create_null_mutex( pj_pool_t *pool,
  75. const char *name,
  76. pj_lock_t **lock );
  77. #if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0
  78. /**
  79. * Create semaphore lock object.
  80. *
  81. * @param pool Memory pool.
  82. * @param name Lock object's name.
  83. * @param initial Initial value of the semaphore.
  84. * @param max Maximum value of the semaphore.
  85. * @param lock Pointer to store the returned handle.
  86. *
  87. * @return PJ_SUCCESS or the appropriate error code.
  88. */
  89. PJ_DECL(pj_status_t) pj_lock_create_semaphore( pj_pool_t *pool,
  90. const char *name,
  91. unsigned initial,
  92. unsigned max,
  93. pj_lock_t **lock );
  94. #endif /* PJ_HAS_SEMAPHORE */
  95. /**
  96. * Acquire lock on the specified lock object.
  97. *
  98. * @param lock The lock object.
  99. *
  100. * @return PJ_SUCCESS or the appropriate error code.
  101. */
  102. PJ_DECL(pj_status_t) pj_lock_acquire( pj_lock_t *lock );
  103. /**
  104. * Try to acquire lock on the specified lock object.
  105. *
  106. * @param lock The lock object.
  107. *
  108. * @return PJ_SUCCESS or the appropriate error code.
  109. */
  110. PJ_DECL(pj_status_t) pj_lock_tryacquire( pj_lock_t *lock );
  111. /**
  112. * Release lock on the specified lock object.
  113. *
  114. * @param lock The lock object.
  115. *
  116. * @return PJ_SUCCESS or the appropriate error code.
  117. */
  118. PJ_DECL(pj_status_t) pj_lock_release( pj_lock_t *lock );
  119. /**
  120. * Destroy the lock object.
  121. *
  122. * @param lock The lock object.
  123. *
  124. * @return PJ_SUCCESS or the appropriate error code.
  125. */
  126. PJ_DECL(pj_status_t) pj_lock_destroy( pj_lock_t *lock );
  127. /** @} */
  128. /**
  129. * @defgroup PJ_GRP_LOCK Group Lock
  130. * @ingroup PJ_LOCK
  131. * @{
  132. *
  133. * Group lock is a synchronization object to manage concurrency among members
  134. * within the same logical group. Example of such groups are:
  135. *
  136. * - dialog, which has members such as the dialog itself, an invite session,
  137. * and several transactions
  138. * - ICE, which has members such as ICE stream transport, ICE session, STUN
  139. * socket, TURN socket, and down to ioqueue key
  140. *
  141. * Group lock has three functions:
  142. *
  143. * - mutual exclusion: to protect resources from being accessed by more than
  144. * one threads at the same time
  145. * - session management: to make sure that the resource is not destroyed
  146. * while others are still using or about to use it.
  147. * - lock coordinator: to provide uniform lock ordering among more than one
  148. * lock objects, which is necessary to avoid deadlock.
  149. *
  150. * The requirements of the group lock are:
  151. *
  152. * - must satisfy all the functions above
  153. * - must allow members to join or leave the group (for example,
  154. * transaction may be added or removed from a dialog)
  155. * - must be able to synchronize with external lock (for example, a dialog
  156. * lock must be able to sync itself with PJSUA lock)
  157. *
  158. * Please see
  159. * https://docs.pjsip.org/en/latest/specific-guides/develop/group_lock.html
  160. * for more info.
  161. */
  162. /**
  163. * Settings for creating the group lock.
  164. */
  165. typedef struct pj_grp_lock_config
  166. {
  167. /**
  168. * Creation flags, currently must be zero.
  169. */
  170. unsigned flags;
  171. } pj_grp_lock_config;
  172. /**
  173. * The group lock destroy handler, a destructor function called when
  174. * a group lock is about to be destroyed.
  175. *
  176. * @param member A pointer to be passed to the handler.
  177. */
  178. typedef void (*pj_grp_lock_handler)(void *member);
  179. /**
  180. * Initialize the config with the default values.
  181. *
  182. * @param cfg The config to be initialized.
  183. */
  184. PJ_DECL(void) pj_grp_lock_config_default(pj_grp_lock_config *cfg);
  185. /**
  186. * Create a group lock object. Initially the group lock will have reference
  187. * counter of zero.
  188. *
  189. * @param pool The group lock only uses the pool parameter to get
  190. * the pool factory, from which it will create its own
  191. * pool.
  192. * @param cfg Optional configuration.
  193. * @param p_grp_lock Pointer to receive the newly created group lock.
  194. *
  195. * @return PJ_SUCCESS or the appropriate error code.
  196. */
  197. PJ_DECL(pj_status_t) pj_grp_lock_create(pj_pool_t *pool,
  198. const pj_grp_lock_config *cfg,
  199. pj_grp_lock_t **p_grp_lock);
  200. /**
  201. * Create a group lock object, with the specified destructor handler, to be
  202. * called by the group lock when it is about to be destroyed. Initially the
  203. * group lock will have reference counter of zero.
  204. *
  205. * @param pool The group lock only uses the pool parameter to get
  206. * the pool factory, from which it will create its own
  207. * pool.
  208. * @param cfg Optional configuration.
  209. * @param member A pointer to be passed to the handler.
  210. * @param handler The destroy handler.
  211. * @param p_grp_lock Pointer to receive the newly created group lock.
  212. *
  213. * @return PJ_SUCCESS or the appropriate error code.
  214. */
  215. PJ_DECL(pj_status_t) pj_grp_lock_create_w_handler(pj_pool_t *pool,
  216. const pj_grp_lock_config *cfg,
  217. void *member,
  218. pj_grp_lock_handler handler,
  219. pj_grp_lock_t **p_grp_lock);
  220. /**
  221. * Forcibly destroy the group lock, ignoring the reference counter value.
  222. *
  223. * @param grp_lock The group lock.
  224. *
  225. * @return PJ_SUCCESS or the appropriate error code.
  226. */
  227. PJ_DECL(pj_status_t) pj_grp_lock_destroy( pj_grp_lock_t *grp_lock);
  228. /**
  229. * Move the contents of the old lock to the new lock and destroy the
  230. * old lock.
  231. *
  232. * @param old_lock The old group lock to be destroyed.
  233. * @param new_lock The new group lock.
  234. *
  235. * @return PJ_SUCCESS or the appropriate error code.
  236. */
  237. PJ_DECL(pj_status_t) pj_grp_lock_replace(pj_grp_lock_t *old_lock,
  238. pj_grp_lock_t *new_lock);
  239. /**
  240. * Acquire lock on the specified group lock.
  241. *
  242. * @param grp_lock The group lock.
  243. *
  244. * @return PJ_SUCCESS or the appropriate error code.
  245. */
  246. PJ_DECL(pj_status_t) pj_grp_lock_acquire( pj_grp_lock_t *grp_lock);
  247. /**
  248. * Acquire lock on the specified group lock if it is available, otherwise
  249. * return immediately wihout waiting.
  250. *
  251. * @param grp_lock The group lock.
  252. *
  253. * @return PJ_SUCCESS or the appropriate error code.
  254. */
  255. PJ_DECL(pj_status_t) pj_grp_lock_tryacquire( pj_grp_lock_t *grp_lock);
  256. /**
  257. * Release the previously held lock. This may cause the group lock
  258. * to be destroyed if it is the last one to hold the reference counter.
  259. * In that case, the function will return PJ_EGONE.
  260. *
  261. * @param grp_lock The group lock.
  262. *
  263. * @return PJ_SUCCESS or the appropriate error code.
  264. */
  265. PJ_DECL(pj_status_t) pj_grp_lock_release( pj_grp_lock_t *grp_lock);
  266. /**
  267. * Add a destructor handler, to be called by the group lock when it is
  268. * about to be destroyed.
  269. *
  270. * @param grp_lock The group lock.
  271. * @param pool Pool to allocate memory for the handler.
  272. * @param member A pointer to be passed to the handler.
  273. * @param handler The destroy handler.
  274. *
  275. * @return PJ_SUCCESS or the appropriate error code.
  276. */
  277. PJ_DECL(pj_status_t) pj_grp_lock_add_handler(pj_grp_lock_t *grp_lock,
  278. pj_pool_t *pool,
  279. void *member,
  280. pj_grp_lock_handler handler);
  281. /**
  282. * Remove previously registered handler. All parameters must be the same
  283. * as when the handler was added.
  284. *
  285. * @param grp_lock The group lock.
  286. * @param member A pointer to be passed to the handler.
  287. * @param handler The destroy handler.
  288. *
  289. * @return PJ_SUCCESS or the appropriate error code.
  290. */
  291. PJ_DECL(pj_status_t) pj_grp_lock_del_handler(pj_grp_lock_t *grp_lock,
  292. void *member,
  293. pj_grp_lock_handler handler);
  294. /**
  295. * Increment reference counter to prevent the group lock grom being destroyed.
  296. *
  297. * @param grp_lock The group lock.
  298. *
  299. * @return PJ_SUCCESS or the appropriate error code.
  300. */
  301. #if !PJ_GRP_LOCK_DEBUG
  302. PJ_DECL(pj_status_t) pj_grp_lock_add_ref(pj_grp_lock_t *grp_lock);
  303. /**
  304. * Debug version of pj_grp_lock_add_ref(), allowing to specify file and lineno.
  305. *
  306. * @param grp_lock The group lock.
  307. * @param x Filename
  308. * @param y Line number
  309. *
  310. * @return PJ_SUCCESS or the appropriate error code.
  311. */
  312. #define pj_grp_lock_add_ref_dbg(grp_lock, x, y) pj_grp_lock_add_ref(grp_lock)
  313. #else
  314. #define pj_grp_lock_add_ref(g) pj_grp_lock_add_ref_dbg(g, __FILE__, __LINE__)
  315. PJ_DECL(pj_status_t) pj_grp_lock_add_ref_dbg(pj_grp_lock_t *grp_lock,
  316. const char *file,
  317. int line);
  318. #endif
  319. /**
  320. * Decrement the reference counter. When the counter value reaches zero, the
  321. * group lock will be destroyed and all destructor handlers will be called.
  322. *
  323. * @param grp_lock The group lock.
  324. *
  325. * @return PJ_SUCCESS or the appropriate error code.
  326. */
  327. #if !PJ_GRP_LOCK_DEBUG
  328. PJ_DECL(pj_status_t) pj_grp_lock_dec_ref(pj_grp_lock_t *grp_lock);
  329. /**
  330. * Debug version of pj_grp_lock_dec_ref(), allowing to specify file and lineno.
  331. *
  332. * @param grp_lock The group lock.
  333. * @param x Filename
  334. * @param y Line number
  335. *
  336. * @return PJ_SUCCESS or the appropriate error code.
  337. */
  338. #define pj_grp_lock_dec_ref_dbg(grp_lock, x, y) pj_grp_lock_dec_ref(grp_lock)
  339. #else
  340. #define pj_grp_lock_dec_ref(g) pj_grp_lock_dec_ref_dbg(g, __FILE__, __LINE__)
  341. PJ_DECL(pj_status_t) pj_grp_lock_dec_ref_dbg(pj_grp_lock_t *grp_lock,
  342. const char *file,
  343. int line);
  344. #endif
  345. /**
  346. * Get current reference count value. This normally is only used for
  347. * debugging purpose.
  348. *
  349. * @param grp_lock The group lock.
  350. *
  351. * @return The reference count value.
  352. */
  353. PJ_DECL(int) pj_grp_lock_get_ref(pj_grp_lock_t *grp_lock);
  354. /**
  355. * Dump group lock info for debugging purpose. If group lock debugging is
  356. * enabled (via PJ_GRP_LOCK_DEBUG) macro, this will print the group lock
  357. * reference counter value along with the source file and line. If
  358. * debugging is disabled, this will only print the reference counter.
  359. *
  360. * @param grp_lock The group lock.
  361. */
  362. PJ_DECL(void) pj_grp_lock_dump(pj_grp_lock_t *grp_lock);
  363. /**
  364. * Synchronize an external lock with the group lock, by adding it to the
  365. * list of locks to be acquired by the group lock when the group lock is
  366. * acquired.
  367. *
  368. * The ''pos'' argument specifies the lock order and also the relative
  369. * position with regard to lock ordering against the group lock. Locks with
  370. * lower ''pos'' value will be locked first, and those with negative value
  371. * will be locked before the group lock (the group lock's ''pos'' value is
  372. * zero).
  373. *
  374. * @param grp_lock The group lock.
  375. * @param ext_lock The external lock
  376. * @param pos The position.
  377. *
  378. * @return PJ_SUCCESS or the appropriate error code.
  379. */
  380. PJ_DECL(pj_status_t) pj_grp_lock_chain_lock(pj_grp_lock_t *grp_lock,
  381. pj_lock_t *ext_lock,
  382. int pos);
  383. /**
  384. * Remove an external lock from group lock's list of synchronized locks.
  385. *
  386. * @param grp_lock The group lock.
  387. * @param ext_lock The external lock
  388. *
  389. * @return PJ_SUCCESS or the appropriate error code.
  390. */
  391. PJ_DECL(pj_status_t) pj_grp_lock_unchain_lock(pj_grp_lock_t *grp_lock,
  392. pj_lock_t *ext_lock);
  393. /** @} */
  394. PJ_END_DECL
  395. #endif /* __PJ_LOCK_H__ */