-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rw_lock.h
67 lines (56 loc) · 1.11 KB
/
rw_lock.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
66
67
#pragma once
#ifndef _RWLockImpl_Header
#define _RWLockImpl_Header
#include <Windows.h>
#include <assert.h>
#include <iostream>
#include <process.h>
using namespace std;
class RWLockImpl {
protected:
RWLockImpl();
~RWLockImpl();
void ReadLockImpl();
bool TryReadLockImpl();
void WriteLockImpl();
bool TryWriteLockImpl();
void UnlockImpl();
private:
void AddWriter();
void RemoveWriter();
DWORD TryReadLockOnce();
HANDLE m_mutex;
HANDLE m_readEvent;
HANDLE m_writeEvent;
unsigned m_readers;
unsigned m_writersWaiting;
unsigned m_writers;
};
class Lock : private RWLockImpl {
public:
Lock(){};
~Lock(){};
void ReadLock();
bool TryReadLock();
void WriteLock();
bool TryWriteLock();
void Unlock();
private:
Lock(const Lock &);
};
inline void Lock::ReadLock() {
ReadLockImpl();
}
inline bool Lock::TryReadLock() {
return TryReadLockImpl();
}
inline void Lock::WriteLock() {
WriteLockImpl();
}
inline bool Lock::TryWriteLock() {
return TryWriteLockImpl();
}
inline void Lock::Unlock() {
UnlockImpl();
}
#endif