Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

497 lines
14KB

  1. /// \file mingw.shared_mutex.h
  2. /// \brief Standard-compliant shared_mutex for MinGW
  3. ///
  4. /// (c) 2017 by Nathaniel J. McClatchey, Athens OH, United States
  5. /// \author Nathaniel J. McClatchey
  6. ///
  7. /// \copyright Simplified (2-clause) BSD License.
  8. ///
  9. /// \note This file may become part of the mingw-w64 runtime package. If/when
  10. /// this happens, the appropriate license will be added, i.e. this code will
  11. /// become dual-licensed, and the current BSD 2-clause license will stay.
  12. /// \note Target Windows version is determined by WINVER, which is determined in
  13. /// <windows.h> from _WIN32_WINNT, which can itself be set by the user.
  14. // Notes on the namespaces:
  15. // - The implementation can be accessed directly in the namespace
  16. // mingw_stdthread.
  17. // - Objects will be brought into namespace std by a using directive. This
  18. // will cause objects declared in std (such as MinGW's implementation) to
  19. // hide this implementation's definitions.
  20. // - To avoid poluting the namespace with implementation details, all objects
  21. // to be pushed into std will be placed in mingw_stdthread::visible.
  22. // The end result is that if MinGW supplies an object, it is automatically
  23. // used. If MinGW does not supply an object, this implementation's version will
  24. // instead be used.
  25. #ifndef MINGW_SHARED_MUTEX_H_
  26. #define MINGW_SHARED_MUTEX_H_
  27. #if !defined(__cplusplus) || (__cplusplus < 201103L)
  28. #error A C++11 compiler is required!
  29. #endif
  30. #include <cassert>
  31. // For descriptive errors.
  32. #include <system_error>
  33. // Implementing a shared_mutex without OS support will require atomic read-
  34. // modify-write capacity.
  35. #include <atomic>
  36. // For timing in shared_lock and shared_timed_mutex.
  37. #include <chrono>
  38. #include <limits>
  39. // Use MinGW's shared_lock class template, if it's available. Requires C++14.
  40. // If unavailable (eg. because this library is being used in C++11), then an
  41. // implementation of shared_lock is provided by this header.
  42. #if (__cplusplus >= 201402L)
  43. #include <shared_mutex>
  44. #endif
  45. // For defer_lock_t, adopt_lock_t, and try_to_lock_t
  46. #include "mingw.mutex.h"
  47. // For this_thread::yield.
  48. //#include "mingw.thread.h"
  49. // Might be able to use native Slim Reader-Writer (SRW) locks.
  50. #ifdef _WIN32
  51. #include <sdkddkver.h> // Detect Windows version.
  52. #include <synchapi.h>
  53. #endif
  54. namespace mingw_stdthread
  55. {
  56. // Define a portable atomics-based shared_mutex
  57. namespace portable
  58. {
  59. class shared_mutex
  60. {
  61. typedef uint_fast16_t counter_type;
  62. std::atomic<counter_type> mCounter {0};
  63. static constexpr counter_type kWriteBit = 1 << (std::numeric_limits<counter_type>::digits - 1);
  64. #if STDMUTEX_RECURSION_CHECKS
  65. // Runtime checker for verifying owner threads. Note: Exclusive mode only.
  66. _OwnerThread mOwnerThread {};
  67. #endif
  68. public:
  69. typedef shared_mutex * native_handle_type;
  70. shared_mutex () = default;
  71. // No form of copying or moving should be allowed.
  72. shared_mutex (const shared_mutex&) = delete;
  73. shared_mutex & operator= (const shared_mutex&) = delete;
  74. ~shared_mutex ()
  75. {
  76. // Terminate if someone tries to destroy an owned mutex.
  77. assert(mCounter.load(std::memory_order_relaxed) == 0);
  78. }
  79. void lock_shared (void)
  80. {
  81. counter_type expected = mCounter.load(std::memory_order_relaxed);
  82. do
  83. {
  84. // Delay if writing or if too many readers are attempting to read.
  85. if (expected >= kWriteBit - 1)
  86. {
  87. using namespace std;
  88. expected = mCounter.load(std::memory_order_relaxed);
  89. continue;
  90. }
  91. if (mCounter.compare_exchange_weak(expected,
  92. static_cast<counter_type>(expected + 1),
  93. std::memory_order_acquire,
  94. std::memory_order_relaxed))
  95. break;
  96. }
  97. while (true);
  98. }
  99. bool try_lock_shared (void)
  100. {
  101. counter_type expected = mCounter.load(std::memory_order_relaxed) & static_cast<counter_type>(~kWriteBit);
  102. if (expected + 1 == kWriteBit)
  103. return false;
  104. else
  105. return mCounter.compare_exchange_strong( expected,
  106. static_cast<counter_type>(expected + 1),
  107. std::memory_order_acquire,
  108. std::memory_order_relaxed);
  109. }
  110. void unlock_shared (void)
  111. {
  112. using namespace std;
  113. #ifndef NDEBUG
  114. if (!(mCounter.fetch_sub(1, memory_order_release) & static_cast<counter_type>(~kWriteBit)))
  115. throw system_error(make_error_code(errc::operation_not_permitted));
  116. #else
  117. mCounter.fetch_sub(1, memory_order_release);
  118. #endif
  119. }
  120. // Behavior is undefined if a lock was previously acquired.
  121. void lock (void)
  122. {
  123. #if STDMUTEX_RECURSION_CHECKS
  124. DWORD self = mOwnerThread.checkOwnerBeforeLock();
  125. #endif
  126. using namespace std;
  127. // Might be able to use relaxed memory order...
  128. // Wait for the write-lock to be unlocked, then claim the write slot.
  129. counter_type current;
  130. while ((current = mCounter.fetch_or(kWriteBit, std::memory_order_acquire)) & kWriteBit);
  131. //this_thread::yield();
  132. // Wait for readers to finish up.
  133. while (current != kWriteBit)
  134. {
  135. //this_thread::yield();
  136. current = mCounter.load(std::memory_order_acquire);
  137. }
  138. #if STDMUTEX_RECURSION_CHECKS
  139. mOwnerThread.setOwnerAfterLock(self);
  140. #endif
  141. }
  142. bool try_lock (void)
  143. {
  144. #if STDMUTEX_RECURSION_CHECKS
  145. DWORD self = mOwnerThread.checkOwnerBeforeLock();
  146. #endif
  147. counter_type expected = 0;
  148. bool ret = mCounter.compare_exchange_strong(expected, kWriteBit,
  149. std::memory_order_acquire,
  150. std::memory_order_relaxed);
  151. #if STDMUTEX_RECURSION_CHECKS
  152. if (ret)
  153. mOwnerThread.setOwnerAfterLock(self);
  154. #endif
  155. return ret;
  156. }
  157. void unlock (void)
  158. {
  159. #if STDMUTEX_RECURSION_CHECKS
  160. mOwnerThread.checkSetOwnerBeforeUnlock();
  161. #endif
  162. using namespace std;
  163. #ifndef NDEBUG
  164. if (mCounter.load(memory_order_relaxed) != kWriteBit)
  165. throw system_error(make_error_code(errc::operation_not_permitted));
  166. #endif
  167. mCounter.store(0, memory_order_release);
  168. }
  169. native_handle_type native_handle (void)
  170. {
  171. return this;
  172. }
  173. };
  174. } // Namespace portable
  175. // The native shared_mutex implementation primarily uses features of Windows
  176. // Vista, but the features used for try_lock and try_lock_shared were not
  177. // introduced until Windows 7. To allow limited use while compiling for Vista,
  178. // I define the class without try_* functions in that case.
  179. // Only fully-featured implementations will be placed into namespace std.
  180. #if defined(_WIN32) && (WINVER >= _WIN32_WINNT_VISTA)
  181. namespace vista
  182. {
  183. class condition_variable_any;
  184. }
  185. namespace windows7
  186. {
  187. // We already #include "mingw.mutex.h". May as well reduce redundancy.
  188. class shared_mutex : windows7::mutex
  189. {
  190. // Allow condition_variable_any (and only condition_variable_any) to treat a
  191. // shared_mutex as its base class.
  192. friend class vista::condition_variable_any;
  193. public:
  194. using windows7::mutex::native_handle_type;
  195. using windows7::mutex::lock;
  196. using windows7::mutex::unlock;
  197. using windows7::mutex::native_handle;
  198. void lock_shared (void)
  199. {
  200. AcquireSRWLockShared(native_handle());
  201. }
  202. void unlock_shared (void)
  203. {
  204. ReleaseSRWLockShared(native_handle());
  205. }
  206. // TryAcquireSRW functions are a Windows 7 feature.
  207. #if (WINVER >= _WIN32_WINNT_WIN7)
  208. bool try_lock_shared (void)
  209. {
  210. return TryAcquireSRWLockShared(native_handle()) != 0;
  211. }
  212. using windows7::mutex::try_lock;
  213. #endif
  214. };
  215. } // Namespace windows7
  216. #endif // Compiling for Vista
  217. #if (defined(_WIN32) && (WINVER >= _WIN32_WINNT_WIN7))
  218. using windows7::shared_mutex;
  219. #else
  220. using portable::shared_mutex;
  221. #endif
  222. class shared_timed_mutex : shared_mutex
  223. {
  224. typedef shared_mutex Base;
  225. public:
  226. using Base::lock;
  227. using Base::try_lock;
  228. using Base::unlock;
  229. using Base::lock_shared;
  230. using Base::try_lock_shared;
  231. using Base::unlock_shared;
  232. template< class Clock, class Duration >
  233. bool try_lock_until ( const std::chrono::time_point<Clock,Duration>& cutoff )
  234. {
  235. do
  236. {
  237. if (try_lock())
  238. return true;
  239. }
  240. while (std::chrono::steady_clock::now() < cutoff);
  241. return false;
  242. }
  243. template< class Rep, class Period >
  244. bool try_lock_for (const std::chrono::duration<Rep,Period>& rel_time)
  245. {
  246. return try_lock_until(std::chrono::steady_clock::now() + rel_time);
  247. }
  248. template< class Clock, class Duration >
  249. bool try_lock_shared_until ( const std::chrono::time_point<Clock,Duration>& cutoff )
  250. {
  251. do
  252. {
  253. if (try_lock_shared())
  254. return true;
  255. }
  256. while (std::chrono::steady_clock::now() < cutoff);
  257. return false;
  258. }
  259. template< class Rep, class Period >
  260. bool try_lock_shared_for (const std::chrono::duration<Rep,Period>& rel_time)
  261. {
  262. return try_lock_shared_until(std::chrono::steady_clock::now() + rel_time);
  263. }
  264. };
  265. #if __cplusplus >= 201402L
  266. using std::shared_lock;
  267. #else
  268. // If not supplied by shared_mutex (eg. because C++14 is not supported), I
  269. // supply the various helper classes that the header should have defined.
  270. template<class Mutex>
  271. class shared_lock
  272. {
  273. Mutex * mMutex;
  274. bool mOwns;
  275. // Reduce code redundancy
  276. void verify_lockable (void)
  277. {
  278. using namespace std;
  279. if (mMutex == nullptr)
  280. throw system_error(make_error_code(errc::operation_not_permitted));
  281. if (mOwns)
  282. throw system_error(make_error_code(errc::resource_deadlock_would_occur));
  283. }
  284. public:
  285. typedef Mutex mutex_type;
  286. shared_lock (void) noexcept
  287. : mMutex(nullptr), mOwns(false)
  288. {
  289. }
  290. shared_lock (shared_lock<Mutex> && other) noexcept
  291. : mMutex(other.mutex_), mOwns(other.owns_)
  292. {
  293. other.mMutex = nullptr;
  294. other.mOwns = false;
  295. }
  296. explicit shared_lock (mutex_type & m)
  297. : mMutex(&m), mOwns(true)
  298. {
  299. mMutex->lock_shared();
  300. }
  301. shared_lock (mutex_type & m, defer_lock_t) noexcept
  302. : mMutex(&m), mOwns(false)
  303. {
  304. }
  305. shared_lock (mutex_type & m, adopt_lock_t)
  306. : mMutex(&m), mOwns(true)
  307. {
  308. }
  309. shared_lock (mutex_type & m, try_to_lock_t)
  310. : mMutex(&m), mOwns(m.try_lock_shared())
  311. {
  312. }
  313. template< class Rep, class Period >
  314. shared_lock( mutex_type& m, const std::chrono::duration<Rep,Period>& timeout_duration )
  315. : mMutex(&m), mOwns(m.try_lock_shared_for(timeout_duration))
  316. {
  317. }
  318. template< class Clock, class Duration >
  319. shared_lock( mutex_type& m, const std::chrono::time_point<Clock,Duration>& timeout_time )
  320. : mMutex(&m), mOwns(m.try_lock_shared_until(timeout_time))
  321. {
  322. }
  323. shared_lock& operator= (shared_lock<Mutex> && other) noexcept
  324. {
  325. if (&other != this)
  326. {
  327. if (mOwns)
  328. mMutex->unlock_shared();
  329. mMutex = other.mMutex;
  330. mOwns = other.mOwns;
  331. other.mMutex = nullptr;
  332. other.mOwns = false;
  333. }
  334. return *this;
  335. }
  336. ~shared_lock (void)
  337. {
  338. if (mOwns)
  339. mMutex->unlock_shared();
  340. }
  341. shared_lock (const shared_lock<Mutex> &) = delete;
  342. shared_lock& operator= (const shared_lock<Mutex> &) = delete;
  343. // Shared locking
  344. void lock (void)
  345. {
  346. verify_lockable();
  347. mMutex->lock_shared();
  348. mOwns = true;
  349. }
  350. bool try_lock (void)
  351. {
  352. verify_lockable();
  353. mOwns = mMutex->try_lock_shared();
  354. return mOwns;
  355. }
  356. template< class Clock, class Duration >
  357. bool try_lock_until( const std::chrono::time_point<Clock,Duration>& cutoff )
  358. {
  359. verify_lockable();
  360. do
  361. {
  362. mOwns = mMutex->try_lock_shared();
  363. if (mOwns)
  364. return mOwns;
  365. }
  366. while (std::chrono::steady_clock::now() < cutoff);
  367. return false;
  368. }
  369. template< class Rep, class Period >
  370. bool try_lock_for (const std::chrono::duration<Rep,Period>& rel_time)
  371. {
  372. return try_lock_until(std::chrono::steady_clock::now() + rel_time);
  373. }
  374. void unlock (void)
  375. {
  376. using namespace std;
  377. if (!mOwns)
  378. throw system_error(make_error_code(errc::operation_not_permitted));
  379. mMutex->unlock_shared();
  380. mOwns = false;
  381. }
  382. // Modifiers
  383. void swap (shared_lock<Mutex> & other) noexcept
  384. {
  385. using namespace std;
  386. swap(mMutex, other.mMutex);
  387. swap(mOwns, other.mOwns);
  388. }
  389. mutex_type * release (void) noexcept
  390. {
  391. mutex_type * ptr = mMutex;
  392. mMutex = nullptr;
  393. mOwns = false;
  394. return ptr;
  395. }
  396. // Observers
  397. mutex_type * mutex (void) const noexcept
  398. {
  399. return mMutex;
  400. }
  401. bool owns_lock (void) const noexcept
  402. {
  403. return mOwns;
  404. }
  405. explicit operator bool () const noexcept
  406. {
  407. return owns_lock();
  408. }
  409. };
  410. template< class Mutex >
  411. void swap( shared_lock<Mutex>& lhs, shared_lock<Mutex>& rhs ) noexcept
  412. {
  413. lhs.swap(rhs);
  414. }
  415. #endif // C++11
  416. } // Namespace mingw_stdthread
  417. namespace std
  418. {
  419. // Because of quirks of the compiler, the common "using namespace std;"
  420. // directive would flatten the namespaces and introduce ambiguity where there
  421. // was none. Direct specification (std::), however, would be unaffected.
  422. // Take the safe option, and include only in the presence of MinGW's win32
  423. // implementation.
  424. #if (__cplusplus < 201703L) || (defined(__MINGW32__ ) && !defined(_GLIBCXX_HAS_GTHREADS))
  425. using mingw_stdthread::shared_mutex;
  426. #endif
  427. #if (__cplusplus < 201402L) || (defined(__MINGW32__ ) && !defined(_GLIBCXX_HAS_GTHREADS))
  428. using mingw_stdthread::shared_timed_mutex;
  429. using mingw_stdthread::shared_lock;
  430. #elif !defined(MINGW_STDTHREAD_REDUNDANCY_WARNING) // Skip repetition
  431. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  432. #pragma message "This version of MinGW seems to include a win32 port of\
  433. pthreads, and probably already has C++ std threading classes implemented,\
  434. based on pthreads. These classes, found in namespace std, are not overridden\
  435. by the mingw-std-thread library. If you would still like to use this\
  436. implementation (as it is more lightweight), use the classes provided in\
  437. namespace mingw_stdthread."
  438. #endif
  439. } // Namespace std
  440. #endif // MINGW_SHARED_MUTEX_H_