tc_lock.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef _TC_LOCK_H
  17. #define _TC_LOCK_H
  18. #include <string>
  19. #include <stdexcept>
  20. #include <cerrno>
  21. #include "util/tc_ex.h"
  22. using namespace std;
  23. namespace tars
  24. {
  25. /////////////////////////////////////////////////
  26. /**
  27. * @file tc_lock.h
  28. * @brief 锁类
  29. */
  30. /////////////////////////////////////////////////
  31. /**
  32. * @brief 锁异常
  33. */
  34. struct TC_Lock_Exception : public TC_Exception
  35. {
  36. TC_Lock_Exception(const string &buffer) : TC_Exception(buffer){};
  37. ~TC_Lock_Exception() throw() {};
  38. };
  39. /**
  40. * @brief 锁模板类其他具体锁配合使用,
  41. * 构造时候加锁,析够的时候解锁
  42. */
  43. template <typename T>
  44. class TC_LockT
  45. {
  46. public:
  47. /**
  48. * @brief 构造函数,构造时枷锁
  49. *
  50. * @param mutex 锁对象
  51. */
  52. TC_LockT(const T& mutex) : _mutex(mutex)
  53. {
  54. _mutex.lock();
  55. _acquired = true;
  56. }
  57. /**
  58. * @brief 析构,析构时解锁
  59. */
  60. virtual ~TC_LockT()
  61. {
  62. if (_acquired)
  63. {
  64. _mutex.unlock();
  65. }
  66. }
  67. /**
  68. * @brief 上锁, 如果已经上锁,则抛出异常
  69. */
  70. void acquire() const
  71. {
  72. if (_acquired)
  73. {
  74. throw TC_Lock_Exception("thread has locked!");
  75. }
  76. _mutex.lock();
  77. _acquired = true;
  78. }
  79. /**
  80. * @brief 尝试上锁.
  81. *
  82. * @return 成功返回true,否则返回false
  83. */
  84. bool tryAcquire() const
  85. {
  86. _acquired = _mutex.tryLock();
  87. return _acquired;
  88. }
  89. /**
  90. * @brief 释放锁, 如果没有上过锁, 则抛出异常
  91. */
  92. void release() const
  93. {
  94. if (!_acquired)
  95. {
  96. throw TC_Lock_Exception("thread hasn't been locked!");
  97. }
  98. _mutex.unlock();
  99. _acquired = false;
  100. }
  101. /**
  102. * @brief 是否已经上锁.
  103. *
  104. * @return 返回true已经上锁,否则返回false
  105. */
  106. bool acquired() const
  107. {
  108. return _acquired;
  109. }
  110. protected:
  111. /**
  112. * @brief 构造函数
  113. * 用于锁尝试操作,与TC_LockT相似
  114. *
  115. */
  116. TC_LockT(const T& mutex, bool) : _mutex(mutex)
  117. {
  118. _acquired = _mutex.tryLock();
  119. }
  120. private:
  121. // Not implemented; prevents accidental use.
  122. TC_LockT(const TC_LockT&);
  123. TC_LockT& operator=(const TC_LockT&);
  124. protected:
  125. /**
  126. * 锁对象
  127. */
  128. const T& _mutex;
  129. /**
  130. * 是否已经上锁
  131. */
  132. mutable bool _acquired;
  133. };
  134. /**
  135. * @brief 尝试上锁
  136. */
  137. template <typename T>
  138. class TC_TryLockT : public TC_LockT<T>
  139. {
  140. public:
  141. TC_TryLockT(const T& mutex) : TC_LockT<T>(mutex, true)
  142. {
  143. }
  144. };
  145. /**
  146. * @brief 空锁, 不做任何锁动作
  147. */
  148. class TC_EmptyMutex
  149. {
  150. public:
  151. /**
  152. * @brief 写锁.
  153. *
  154. * @return int, 0 正确
  155. */
  156. int lock() const {return 0;}
  157. /**
  158. * @brief 解写锁
  159. */
  160. int unlock() const {return 0;}
  161. /**
  162. * @brief 尝试解锁.
  163. *
  164. * @return int, 0 正确
  165. */
  166. bool trylock() const {return true;}
  167. };
  168. /**
  169. * @brief 读写锁读锁模板类
  170. * 构造时候加锁,析够的时候解锁
  171. */
  172. template <typename T>
  173. class TC_RW_RLockT
  174. {
  175. public:
  176. /**
  177. * @brief 构造函数,构造时枷锁
  178. *
  179. * @param lock 锁对象
  180. */
  181. TC_RW_RLockT(T& lock)
  182. : _rwLock(lock),_acquired(false)
  183. {
  184. _rwLock.readLock();
  185. _acquired = true;
  186. }
  187. /**
  188. * @brief 析构时解锁
  189. */
  190. ~TC_RW_RLockT()
  191. {
  192. if (_acquired)
  193. {
  194. _rwLock.unReadLock();
  195. }
  196. }
  197. private:
  198. /**
  199. *锁对象
  200. */
  201. const T& _rwLock;
  202. /**
  203. * 是否已经上锁
  204. */
  205. mutable bool _acquired;
  206. TC_RW_RLockT(const TC_RW_RLockT&);
  207. TC_RW_RLockT& operator=(const TC_RW_RLockT&);
  208. };
  209. template <typename T>
  210. class TC_RW_WLockT
  211. {
  212. public:
  213. /**
  214. * @brief 构造函数,构造时枷锁
  215. *
  216. * @param lock 锁对象
  217. */
  218. TC_RW_WLockT(T& lock)
  219. : _rwLock(lock),_acquired(false)
  220. {
  221. _rwLock.writeLock();
  222. _acquired = true;
  223. }
  224. /**
  225. * @brief 析构时解锁
  226. */
  227. ~TC_RW_WLockT()
  228. {
  229. if(_acquired)
  230. {
  231. _rwLock.unWriteLock();
  232. }
  233. }
  234. private:
  235. /**
  236. *锁对象
  237. */
  238. const T& _rwLock;
  239. /**
  240. * 是否已经上锁
  241. */
  242. mutable bool _acquired;
  243. TC_RW_WLockT(const TC_RW_WLockT&);
  244. TC_RW_WLockT& operator=(const TC_RW_WLockT&);
  245. };
  246. };
  247. #endif