shared_recursive_mutex_wrapper.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "libbinom/include/utils/shared_recursive_mutex_wrapper.hxx"
  2. using namespace shared_recursive_mtx;
  3. thread_local std::map<std::shared_mutex*, Counters> SharedRecursiveMutexWrapper::counter_storage;
  4. std::map<std::shared_mutex*, Counters>::iterator SharedRecursiveMutexWrapper::createCountersRef(const std::shared_mutex *mtx) noexcept {
  5. if(!mtx) return counter_storage.end();
  6. auto it = counter_storage.find(const_cast<std::shared_mutex*>(mtx));
  7. if(it == counter_storage.cend())
  8. return counter_storage.emplace(std::make_pair(const_cast<std::shared_mutex*>(mtx), Counters{})).first;
  9. ++it->second.wrapper_count;
  10. return it;
  11. }
  12. void SharedRecursiveMutexWrapper::deleteCounterRef(std::map<std::shared_mutex*, Counters>::iterator it) noexcept {
  13. if(it != counter_storage.cend()) {
  14. if(!--it->second.wrapper_count) {
  15. if(it->second.unique_lock_counter) it->first->unlock();
  16. if(it->second.shared_lock_counter) it->first->unlock_shared();
  17. counter_storage.erase(it);
  18. }
  19. }
  20. it = counter_storage.end();
  21. }
  22. SharedRecursiveMutexWrapper::SharedRecursiveMutexWrapper(const std::shared_mutex *mtx, MtxLockType lock_type) noexcept
  23. : mtx_data(createCountersRef(mtx)) {
  24. if(!mtx) std::terminate();
  25. switch (lock_type) {
  26. case MtxLockType::unlocked: return;
  27. case MtxLockType::shared_locked: lockShared(); return;
  28. case MtxLockType::unique_locked: lock(); return;
  29. }
  30. }
  31. SharedRecursiveMutexWrapper::SharedRecursiveMutexWrapper(const SharedRecursiveMutexWrapper &other, MtxLockType lock_type) noexcept
  32. : SharedRecursiveMutexWrapper(other.mtx_data->first) {
  33. switch (lock_type) {
  34. case MtxLockType::unlocked: return;
  35. case MtxLockType::shared_locked: lockShared(); return;
  36. case MtxLockType::unique_locked: lock(); return;
  37. }
  38. }
  39. SharedRecursiveMutexWrapper::SharedRecursiveMutexWrapper(SharedRecursiveMutexWrapper &&other, MtxLockType lock_type) noexcept
  40. : mtx_data(other.mtx_data) {
  41. other.mtx_data = counter_storage.end();
  42. switch (lock_type) {
  43. case MtxLockType::unlocked: return;
  44. case MtxLockType::shared_locked: lockShared(); return;
  45. case MtxLockType::unique_locked: lock(); return;
  46. }
  47. }
  48. SharedRecursiveMutexWrapper::~SharedRecursiveMutexWrapper() { deleteCounterRef(mtx_data); mtx_data = counter_storage.end(); }
  49. ui64 SharedRecursiveMutexWrapper::getWrappedMutexCount() noexcept {return counter_storage.size();}
  50. ui64 SharedRecursiveMutexWrapper::getUniqueLockCount() const noexcept {return mtx_data->second.unique_lock_counter;}
  51. ui64 SharedRecursiveMutexWrapper::getSheredLockCount() const noexcept {return mtx_data->second.shared_lock_counter;}
  52. ui64 SharedRecursiveMutexWrapper::getThisMutexWrapperCount() const noexcept {return mtx_data->second.wrapper_count;}
  53. std::shared_mutex &SharedRecursiveMutexWrapper::getSharedMutex() const noexcept {return *mtx_data->first;}
  54. MtxLockType SharedRecursiveMutexWrapper::getLockType() const noexcept {
  55. if(mtx_data->second.unique_lock_counter) return MtxLockType::unique_locked;
  56. if(mtx_data->second.shared_lock_counter) return MtxLockType::shared_locked;
  57. return MtxLockType::unlocked;
  58. }
  59. void SharedRecursiveMutexWrapper::lock() noexcept {
  60. switch (getLockType()) {
  61. case MtxLockType::unlocked:
  62. mtx_data->first->lock();
  63. ++mtx_data->second.unique_lock_counter;
  64. return;
  65. case MtxLockType::shared_locked:
  66. mtx_data->first->unlock_shared();
  67. mtx_data->first->lock();
  68. ++mtx_data->second.unique_lock_counter;
  69. return;
  70. case MtxLockType::unique_locked:
  71. ++mtx_data->second.unique_lock_counter;
  72. return;
  73. }
  74. }
  75. void SharedRecursiveMutexWrapper::lockShared() noexcept {
  76. switch (getLockType()) {
  77. case MtxLockType::unlocked:
  78. mtx_data->first->lock_shared();
  79. ++mtx_data->second.shared_lock_counter;
  80. return;
  81. case MtxLockType::shared_locked:
  82. ++mtx_data->second.shared_lock_counter;
  83. return;
  84. case MtxLockType::unique_locked:
  85. ++mtx_data->second.shared_lock_counter;
  86. return;
  87. }
  88. }
  89. void SharedRecursiveMutexWrapper::unlock() noexcept {
  90. switch (getLockType()) {
  91. case MtxLockType::unlocked:
  92. case MtxLockType::shared_locked: return;
  93. case MtxLockType::unique_locked:
  94. if(!--mtx_data->second.unique_lock_counter) {
  95. if(mtx_data->second.shared_lock_counter) {
  96. // "A prior unlock() operation on the same mutex synchronizes-with (as defined in std::memory_order) this operation."
  97. // - (C) [https://en.cppreference.com/w/cpp/thread/shared_mutex/lock_shared] 3.04.2022
  98. mtx_data->first->unlock(); mtx_data->first->lock_shared();
  99. } else mtx_data->first->unlock();
  100. }
  101. return;
  102. }
  103. }
  104. void SharedRecursiveMutexWrapper::unlockShared() noexcept {
  105. switch (getLockType()) {
  106. case MtxLockType::unlocked: return;
  107. case MtxLockType::shared_locked:
  108. if(!--mtx_data->second.shared_lock_counter) mtx_data->first->unlock_shared();
  109. return;
  110. case MtxLockType::unique_locked:
  111. if(mtx_data->second.shared_lock_counter) --mtx_data->second.shared_lock_counter;
  112. return;
  113. }
  114. }
  115. bool SharedRecursiveMutexWrapper::tryLock() noexcept {
  116. switch (getLockType()) {
  117. case MtxLockType::unlocked: {
  118. if(mtx_data->first->try_lock()) {
  119. ++mtx_data->second.unique_lock_counter;
  120. return true;
  121. } else return false;
  122. }
  123. case MtxLockType::shared_locked:
  124. mtx_data->first->unlock_shared();
  125. mtx_data->first->lock();
  126. ++mtx_data->second.unique_lock_counter;
  127. return true;
  128. case MtxLockType::unique_locked:
  129. ++mtx_data->second.unique_lock_counter;
  130. return true;
  131. default: return false;
  132. }
  133. }
  134. bool SharedRecursiveMutexWrapper::tryLockShared() noexcept {
  135. switch (getLockType()) {
  136. case MtxLockType::unlocked:
  137. if(mtx_data->first->try_lock_shared()) {
  138. ++mtx_data->second.shared_lock_counter;
  139. return true;
  140. } else return false;
  141. case MtxLockType::shared_locked:
  142. ++mtx_data->second.shared_lock_counter;
  143. return true;
  144. case MtxLockType::unique_locked:
  145. ++mtx_data->second.shared_lock_counter;
  146. return true;
  147. default: return false;
  148. }
  149. }
  150. SharedRecursiveMutexWrapper &SharedRecursiveMutexWrapper::operator =(const SharedRecursiveMutexWrapper &other) noexcept {
  151. this->~SharedRecursiveMutexWrapper();
  152. return *new(this) SharedRecursiveMutexWrapper(other);
  153. }
  154. SharedRecursiveMutexWrapper &SharedRecursiveMutexWrapper::operator =(SharedRecursiveMutexWrapper &&other) noexcept {
  155. this->~SharedRecursiveMutexWrapper();
  156. return *new(this) SharedRecursiveMutexWrapper(std::move(other));
  157. }
  158. SharedRecursiveMutexWrapper &SharedRecursiveMutexWrapper::operator =(const SharedRecursiveLock &other) noexcept {
  159. this->~SharedRecursiveMutexWrapper();
  160. return *new(this) SharedRecursiveMutexWrapper(other);
  161. }
  162. SharedRecursiveMutexWrapper &SharedRecursiveMutexWrapper::operator =(SharedRecursiveLock &&other) noexcept {
  163. this->~SharedRecursiveMutexWrapper();
  164. return *new(this) SharedRecursiveMutexWrapper(std::move(other));
  165. }
  166. SharedRecursiveLock::SharedRecursiveLock(const std::shared_mutex *mtx, MtxLockType lock_type)
  167. : SharedRecursiveMutexWrapper(const_cast<std::shared_mutex*>(mtx), lock_type), lock_type(lock_type) {}
  168. SharedRecursiveLock::SharedRecursiveLock(const SharedRecursiveMutexWrapper &other, MtxLockType lock_type)
  169. : SharedRecursiveMutexWrapper(other, lock_type),
  170. lock_type(lock_type) {}
  171. SharedRecursiveLock::SharedRecursiveLock(SharedRecursiveMutexWrapper &&other, MtxLockType lock_type)
  172. : SharedRecursiveMutexWrapper(std::move(other), lock_type),
  173. lock_type(lock_type) {}
  174. SharedRecursiveLock::SharedRecursiveLock(const SharedRecursiveLock &other, MtxLockType lock_type)
  175. : SharedRecursiveMutexWrapper(other, lock_type),
  176. lock_type(lock_type) {}
  177. SharedRecursiveLock::SharedRecursiveLock(SharedRecursiveLock &&other)
  178. : SharedRecursiveMutexWrapper(std::move(other)),
  179. lock_type(other.lock_type) {
  180. other.lock_type = MtxLockType::unlocked;
  181. }
  182. SharedRecursiveLock::~SharedRecursiveLock() {
  183. switch (lock_type) {
  184. case MtxLockType::unlocked: return;
  185. case MtxLockType::shared_locked: unlockShared(); return;
  186. case MtxLockType::unique_locked: unlock(); return;
  187. }
  188. }
  189. TransactionLock::TransactionLock(std::set<std::shared_mutex *> mtx_set, MtxLockType lock_type)
  190. : lock_type(lock_type) {
  191. if(mtx_set.empty()) {
  192. this->lock_type = MtxLockType::unlocked;
  193. return;
  194. }
  195. switch (lock_type) {
  196. case shared_recursive_mtx::MtxLockType::unlocked: return;
  197. case shared_recursive_mtx::MtxLockType::shared_locked:
  198. forever {
  199. bool is_own_lock = true;
  200. auto it = mtx_set.begin();
  201. mtx_stack.emplace(*it).lockShared();
  202. ++it;
  203. for(auto end = mtx_set.cend(); it != end; ++it) {
  204. if(mtx_stack.emplace(*it).tryLockShared()) continue;
  205. is_own_lock = false;
  206. mtx_stack.pop();
  207. while(!mtx_stack.empty()) {
  208. mtx_stack.top().unlockShared();
  209. mtx_stack.pop();
  210. }
  211. break;
  212. }
  213. if(is_own_lock) break;
  214. }
  215. return;
  216. case shared_recursive_mtx::MtxLockType::unique_locked:
  217. forever {
  218. bool is_own_lock = true;
  219. auto it = mtx_set.begin();
  220. mtx_stack.emplace(*it).lock();
  221. ++it;
  222. for(auto end = mtx_set.cend(); it != end; ++it) {
  223. if(mtx_stack.emplace(*it).tryLock()) continue;
  224. is_own_lock = false;
  225. mtx_stack.pop();
  226. while(!mtx_stack.empty()) {
  227. mtx_stack.top().unlock();
  228. mtx_stack.pop();
  229. }
  230. break;
  231. }
  232. if(is_own_lock) break;
  233. }
  234. return;
  235. }
  236. }
  237. TransactionLock::TransactionLock(TransactionLock &&other)
  238. : mtx_stack(std::move(other.mtx_stack)), lock_type(other.lock_type) {
  239. other.lock_type = MtxLockType::unlocked;
  240. }
  241. TransactionLock::~TransactionLock(){
  242. switch (lock_type) {
  243. case shared_recursive_mtx::MtxLockType::unlocked: return;
  244. case shared_recursive_mtx::MtxLockType::shared_locked:
  245. while (!mtx_stack.empty()) { mtx_stack.top().unlockShared(); mtx_stack.pop(); }
  246. return;
  247. case shared_recursive_mtx::MtxLockType::unique_locked:
  248. while (!mtx_stack.empty()) { mtx_stack.top().unlock(); mtx_stack.pop(); }
  249. return;
  250. }
  251. }