named_locks.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2016, Fairview 5 Engineering, LLC
  5. *
  6. * George Joseph <george.joseph@fairview5.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Named Locks
  21. *
  22. * \author George Joseph <george.joseph@fairview5.com>
  23. */
  24. #ifndef INCLUDE_ASTERISK_NAMED_LOCKS_H_
  25. #define INCLUDE_ASTERISK_NAMED_LOCKS_H_
  26. #include "asterisk/astobj2.h"
  27. /*!
  28. * \defgroup named_locks Named mutex and read-write locks
  29. * @{
  30. * \page NamedLocks Named mutex and read-write locks
  31. * \since 13.9.0
  32. *
  33. * Locking some objects like sorcery objects can be tricky because the underlying
  34. * ao2 object may not be the same for all callers. For instance, two threads that
  35. * call ast_sorcery_retrieve_by_id on the same aor name might actually get 2 different
  36. * ao2 objects if the underlying wizard had to rehydrate the aor from a database.
  37. * Locking one ao2 object doesn't have any effect on the other even if those objects
  38. * had locks in the first place
  39. *
  40. * Named locks allow access control by name. Now an aor named "1000" can be locked and
  41. * any other thread attempting to lock the aor named "1000" will wait regardless of whether
  42. * the underlying ao2 object is the same or not.
  43. *
  44. * To use a named lock:
  45. * Call ast_named_lock_get with the appropriate keyspace and key.
  46. * Use the standard ao2 lock/unlock functions as needed.
  47. * Call ast_named_lock_put when you're finished with it.
  48. */
  49. /*!
  50. * \brief Which type of lock to request.
  51. */
  52. enum ast_named_lock_type {
  53. /*! Request a named mutex. */
  54. AST_NAMED_LOCK_TYPE_MUTEX = AO2_ALLOC_OPT_LOCK_MUTEX,
  55. /*! Request a named read/write lock. */
  56. AST_NAMED_LOCK_TYPE_RWLOCK = AO2_ALLOC_OPT_LOCK_RWLOCK,
  57. };
  58. struct ast_named_lock;
  59. struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
  60. enum ast_named_lock_type lock_type, const char *keyspace, const char *key);
  61. int __ast_named_lock_put(const char *filename, int lineno, const char *func,
  62. struct ast_named_lock *lock);
  63. /*!
  64. * \brief Geta named lock handle
  65. * \since 13.9.0
  66. *
  67. * \param lock_type One of ast_named_lock_type
  68. * \param keyspace
  69. * \param key
  70. * \retval A pointer to an ast_named_lock structure
  71. * \retval NULL on error
  72. *
  73. * \note
  74. * keyspace and key can be anything. For sorcery objects, keyspace could be the object type
  75. * and key could be the object id.
  76. */
  77. #define ast_named_lock_get(lock_type, keyspace, key) \
  78. __ast_named_lock_get(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock_type, \
  79. keyspace, key)
  80. /*!
  81. * \brief Put a named lock handle away
  82. * \since 13.9.0
  83. *
  84. * \param lock The pointer to the ast_named_lock structure returned by ast_named_lock_get
  85. * \retval 0 Success
  86. * \retval -1 Failure
  87. */
  88. #define ast_named_lock_put(lock) \
  89. __ast_named_lock_put(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock)
  90. /*!
  91. * @}
  92. */
  93. #endif /* INCLUDE_ASTERISK_NAMED_LOCKS_H_ */