-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathSharedMutex_std.h
65 lines (51 loc) · 1.17 KB
/
SharedMutex_std.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <shared_mutex>
#include "Interface/SharedMutex.h"
#include <memory>
#if defined(_DEBUG) || (!defined(_WIN32) && !defined(NDEBUG))
#define SHARED_MUTEX_CHECK
#define SHARED_MUTEX_CHECK_P(x) ,x
#include <mutex>
#include <set>
#include <thread>
#else
#define SHARED_MUTEX_CHECK_P(x)
#endif
class SharedMutex : public ISharedMutex
{
public:
virtual ILock* readLock();
virtual ILock* writeLock();
#ifdef SHARED_MUTEX_CHECK
void rmLockCheck();
#endif
private:
std::shared_timed_mutex mutex;
#ifdef SHARED_MUTEX_CHECK
std::mutex check_mutex;
std::set<std::thread::id> check_threads;
#endif
};
class ReadLock : public ILock
{
public:
ReadLock(std::shared_lock<std::shared_timed_mutex>* read_lock
SHARED_MUTEX_CHECK_P(SharedMutex* shared_mutex));
#ifdef SHARED_MUTEX_CHECK
~ReadLock() {
shared_mutex->rmLockCheck();
}
#endif
private:
std::unique_ptr<std::shared_lock<std::shared_timed_mutex> > read_lock;
#ifdef SHARED_MUTEX_CHECK
SharedMutex* shared_mutex;
#endif
};
class WriteLock : public ILock
{
public:
WriteLock(std::unique_lock<std::shared_timed_mutex>* write_lock);
private:
std::unique_ptr<std::unique_lock<std::shared_timed_mutex> > write_lock;
};