lock.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef B10_THREAD_LOCK_H
  15. #define B10_THREAD_LOCK_H
  16. #include <boost/noncopyable.hpp>
  17. #include <cstdlib> // for NULL.
  18. namespace isc {
  19. namespace util {
  20. namespace thread {
  21. /// \brief Mutex with very simple interface
  22. ///
  23. /// Since mutexes are very system dependant, we create our own wrapper around
  24. /// whatever is available on the system and hide it.
  25. ///
  26. /// To use this mutex, create it and then lock and unlock it by creating the
  27. /// Mutex::Locker object.
  28. ///
  29. /// Also, as mutex is a low-level system object, an error might happen at any
  30. /// operation with it. We convert many errors to the isc::InvalidOperation,
  31. /// since the errors usually happen only when used in a wrong way. Any methods,
  32. /// constructors or even destructors in this class can throw. Allocation errors
  33. /// are converted to std::bad_alloc (for example when OS-dependant limit of
  34. /// mutexes is exceeded).
  35. ///
  36. /// The current interface is somewhat minimalistic. If we ever need more, we
  37. /// can add it later.
  38. class Mutex : public boost::noncopyable {
  39. public:
  40. /// \brief Constructor.
  41. ///
  42. /// Creates a mutex. Depending on the parameter, it is either recursive
  43. /// (the same thread may lock it multiple times, others wait; it must be
  44. /// unlocked as many times to become really unlocked) or normal (can be
  45. /// locked just once, if the same threads tries to lock it again, Bad
  46. /// Things Happen).
  47. ///
  48. /// Depending on compilation parameters and OS, the mutex may or may not
  49. /// do some error and sanity checking. However, such checking is meant
  50. /// only to aid development, not rely on it as a feature.
  51. ///
  52. /// \param recursive If the thread should be recursive (lockable multiple
  53. /// times from the same thread) or not.
  54. /// \throw std::bad_alloc In case allocation of something (memory, the
  55. /// OS mutex) fails.
  56. /// \throw isc::InvalidOperation Other unspecified errors around the mutex.
  57. /// This should be rare.
  58. Mutex(bool recursive = false);
  59. /// \brief Destructor.
  60. ///
  61. /// Destroyes the mutex. It is not allowed to destroy a mutex which is
  62. /// currently locked. This means a Locker created with this Mutex must
  63. /// never live longer than the Mutex itself.
  64. ///
  65. /// \throw isc::InvalidOperation when the OS reports an error. This should
  66. /// generally happen only when the Mutex was used in a wrong way,
  67. /// meaning programmer error.
  68. ~ Mutex();
  69. /// \brief This holds a lock on a Mutex.
  70. ///
  71. /// To lock a mutex, create a locket. It'll get unlocked when the locker
  72. /// is destroyed.
  73. ///
  74. /// If you create the locker on the stack or using some other "garbage
  75. /// collecting" mechanism (auto_ptr, for example), it ensures exception
  76. /// safety with regards to the mutex - it'll get released on the exit
  77. /// of function no matter by what means.
  78. class Locker : public boost::noncopyable {
  79. public:
  80. /// \brief Constructor.
  81. ///
  82. /// Locks the mutex. May block for extended period of time.
  83. ///
  84. /// \throw isc::InvalidOperation when OS reports error. This usually
  85. /// means an attempt to use the mutex in a wrong way (locking
  86. /// a non-recursive mutex a second time from the same thread,
  87. /// for example).
  88. Locker(Mutex& mutex) :
  89. mutex_(NULL)
  90. {
  91. // Set the mutex_ after we acquire the lock. This is because of
  92. // exception safety. If lock() throws, it didn't work, so we must
  93. // not unlock when we are destroyed. In such case, mutex_ is
  94. // NULL and checked in the destructor.
  95. mutex.lock();
  96. mutex_ = &mutex;
  97. }
  98. /// \brief Destructor.
  99. ///
  100. /// Unlocks the mutex.
  101. ///
  102. /// \throw isc::InvalidOperation when OS repotrs error. This usually
  103. /// means an attempt to use the mutex in a wrong way (unlocking
  104. /// a mutex belonging to a differen thread).
  105. ~ Locker() {
  106. if (mutex_ != NULL) {
  107. mutex_->unlock();
  108. }
  109. }
  110. private:
  111. Mutex* mutex_;
  112. };
  113. /// \brief If the mutex is currently locked
  114. ///
  115. /// This is debug aiding method only. And it might be unavailable in
  116. /// non-debug build (because keeping the state might be needlesly
  117. /// slow).
  118. ///
  119. /// \todo Disable in non-debug build
  120. bool locked() const;
  121. private:
  122. friend class Locker;
  123. struct Impl;
  124. Impl* impl_;
  125. void lock();
  126. void unlock();
  127. };
  128. }
  129. }
  130. }
  131. #endif