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