reMutexDirect.I 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file reMutexDirect.I
  10. * @author drose
  11. * @date 2006-02-13
  12. */
  13. /**
  14. *
  15. */
  16. INLINE ReMutexDirect::
  17. ReMutexDirect()
  18. #ifndef HAVE_REMUTEXTRUEIMPL
  19. : _cvar_impl(_lock_impl)
  20. #endif
  21. {
  22. #ifndef HAVE_REMUTEXTRUEIMPL
  23. _locking_thread = nullptr;
  24. _lock_count = 0;
  25. #endif
  26. }
  27. /**
  28. * Alias for acquire() to match C++11 semantics.
  29. * @see acquire()
  30. */
  31. INLINE void ReMutexDirect::
  32. lock() {
  33. TAU_PROFILE("void ReMutexDirect::acquire()", " ", TAU_USER);
  34. #ifdef HAVE_REMUTEXTRUEIMPL
  35. _impl.lock();
  36. #else
  37. ((ReMutexDirect *)this)->do_lock();
  38. #endif // HAVE_REMUTEXTRUEIMPL
  39. }
  40. /**
  41. * Alias for try_acquire() to match C++11 semantics.
  42. * @see try_acquire()
  43. */
  44. INLINE bool ReMutexDirect::
  45. try_lock() {
  46. TAU_PROFILE("void ReMutexDirect::try_acquire()", " ", TAU_USER);
  47. #ifdef HAVE_REMUTEXTRUEIMPL
  48. return _impl.try_lock();
  49. #else
  50. return ((ReMutexDirect *)this)->do_try_lock();
  51. #endif // HAVE_REMUTEXTRUEIMPL
  52. }
  53. /**
  54. * Alias for release() to match C++11 semantics.
  55. * @see release()
  56. */
  57. INLINE void ReMutexDirect::
  58. unlock() {
  59. TAU_PROFILE("void ReMutexDirect::unlock()", " ", TAU_USER);
  60. #ifdef HAVE_REMUTEXTRUEIMPL
  61. _impl.unlock();
  62. #else
  63. ((ReMutexDirect *)this)->do_unlock();
  64. #endif // HAVE_REMUTEXTRUEIMPL
  65. }
  66. /**
  67. * Grabs the reMutex if it is available. If it is not available, blocks until
  68. * it becomes available, then grabs it. In either case, the function does not
  69. * return until the reMutex is held; you should then call unlock().
  70. *
  71. * This method is considered const so that you can lock and unlock const
  72. * reMutexes, mainly to allow thread-safe access to otherwise const data.
  73. *
  74. * Also see ReMutexHolder.
  75. */
  76. INLINE void ReMutexDirect::
  77. acquire() const {
  78. TAU_PROFILE("void ReMutexDirect::acquire()", " ", TAU_USER);
  79. #ifdef HAVE_REMUTEXTRUEIMPL
  80. _impl.lock();
  81. #else
  82. ((ReMutexDirect *)this)->do_lock();
  83. #endif // HAVE_REMUTEXTRUEIMPL
  84. }
  85. /**
  86. * This variant on acquire() accepts the current thread as a parameter, if it
  87. * is already known, as an optimization.
  88. */
  89. INLINE void ReMutexDirect::
  90. acquire(Thread *current_thread) const {
  91. TAU_PROFILE("void ReMutexDirect::acquire(Thread *)", " ", TAU_USER);
  92. #ifdef HAVE_REMUTEXTRUEIMPL
  93. _impl.lock();
  94. #else
  95. ((ReMutexDirect *)this)->do_lock(current_thread);
  96. #endif // HAVE_REMUTEXTRUEIMPL
  97. }
  98. /**
  99. * Returns immediately, with a true value indicating the mutex has been
  100. * acquired, and false indicating it has not.
  101. *
  102. * @deprecated Python users should use acquire(False), C++ users try_lock()
  103. */
  104. INLINE bool ReMutexDirect::
  105. try_acquire() const {
  106. TAU_PROFILE("void ReMutexDirect::acquire(bool)", " ", TAU_USER);
  107. #ifdef HAVE_REMUTEXTRUEIMPL
  108. return _impl.try_lock();
  109. #else
  110. return ((ReMutexDirect *)this)->do_try_lock();
  111. #endif // HAVE_REMUTEXTRUEIMPL
  112. }
  113. /**
  114. * Returns immediately, with a true value indicating the mutex has been
  115. * acquired, and false indicating it has not.
  116. *
  117. * @deprecated Python users should use acquire(False), C++ users try_lock()
  118. */
  119. INLINE bool ReMutexDirect::
  120. try_acquire(Thread *current_thread) const {
  121. TAU_PROFILE("void ReMutexDirect::acquire(bool)", " ", TAU_USER);
  122. #ifdef HAVE_REMUTEXTRUEIMPL
  123. return _impl.try_lock();
  124. #else
  125. return ((ReMutexDirect *)this)->do_try_lock(current_thread);
  126. #endif // HAVE_REMUTEXTRUEIMPL
  127. }
  128. /**
  129. * This method increments the lock count, assuming the calling thread already
  130. * holds the lock. After this call, release() will need to be called one
  131. * additional time to release the lock.
  132. *
  133. * This method really performs the same function as acquire(), but it offers a
  134. * potential (slight) performance benefit when the calling thread knows that
  135. * it already holds the lock. It is an error to call this when the calling
  136. * thread does not hold the lock.
  137. */
  138. INLINE void ReMutexDirect::
  139. elevate_lock() const {
  140. TAU_PROFILE("void ReMutexDirect::elevate_lock()", " ", TAU_USER);
  141. #ifdef HAVE_REMUTEXTRUEIMPL
  142. _impl.lock();
  143. #else
  144. ((ReMutexDirect *)this)->do_elevate_lock();
  145. #endif // HAVE_REMUTEXTRUEIMPL
  146. }
  147. /**
  148. * Releases the reMutex. It is an error to call this if the reMutex was not
  149. * already locked.
  150. *
  151. * This method is considered const so that you can lock and unlock const
  152. * reMutexes, mainly to allow thread-safe access to otherwise const data.
  153. */
  154. INLINE void ReMutexDirect::
  155. release() const {
  156. TAU_PROFILE("void ReMutexDirect::release()", " ", TAU_USER);
  157. #ifdef HAVE_REMUTEXTRUEIMPL
  158. _impl.unlock();
  159. #else
  160. ((ReMutexDirect *)this)->do_unlock();
  161. #endif // HAVE_REMUTEXTRUEIMPL
  162. }
  163. /**
  164. * Returns true if the current thread has locked the ReMutex, false otherwise.
  165. * This method is only intended for use in debugging, hence the method name;
  166. * in the ReMutexDirect case, it always returns true, since there's not a
  167. * reliable way to determine this otherwise.
  168. */
  169. INLINE bool ReMutexDirect::
  170. debug_is_locked() const {
  171. return true;
  172. }
  173. /**
  174. * The mutex name is only defined when compiling in DEBUG_THREADS mode.
  175. */
  176. INLINE void ReMutexDirect::
  177. set_name(const std::string &) {
  178. }
  179. /**
  180. * The mutex name is only defined when compiling in DEBUG_THREADS mode.
  181. */
  182. INLINE void ReMutexDirect::
  183. clear_name() {
  184. }
  185. /**
  186. * The mutex name is only defined when compiling in DEBUG_THREADS mode.
  187. */
  188. INLINE bool ReMutexDirect::
  189. has_name() const {
  190. return false;
  191. }
  192. /**
  193. * The mutex name is only defined when compiling in DEBUG_THREADS mode.
  194. */
  195. INLINE std::string ReMutexDirect::
  196. get_name() const {
  197. return std::string();
  198. }
  199. #ifndef HAVE_REMUTEXTRUEIMPL
  200. /**
  201. * The private implementation of acquire(), for the case in which the
  202. * underlying lock system does not provide a reentrant mutex (and therefore we
  203. * have to build this functionality on top of the existing non-reentrant
  204. * mutex).
  205. */
  206. INLINE void ReMutexDirect::
  207. do_lock() {
  208. do_lock(Thread::get_current_thread());
  209. }
  210. #endif
  211. #ifndef HAVE_REMUTEXTRUEIMPL
  212. /**
  213. * The private implementation of acquire(false), for the case in which the
  214. * underlying lock system does not provide a reentrant mutex (and therefore we
  215. * have to build this functionality on top of the existing non-reentrant
  216. * mutex).
  217. */
  218. INLINE bool ReMutexDirect::
  219. do_try_lock() {
  220. return do_try_lock(Thread::get_current_thread());
  221. }
  222. #endif