forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.cpp
167 lines (132 loc) · 2.48 KB
/
mutex.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "mutex.h"
#include "util/asm.hpp"
void shared_mutex::imp_lock_shared(u32 val)
{
ensure(val < c_err); // "shared_mutex underflow"
for (int i = 0; i < 10; i++)
{
busy_wait();
if (try_lock_shared())
{
return;
}
}
// Acquire writer lock and downgrade
const u32 old = m_value.fetch_add(c_one);
if (old == 0)
{
lock_downgrade();
return;
}
ensure((old % c_sig) + c_one < c_sig); // "shared_mutex overflow"
imp_wait();
lock_downgrade();
}
void shared_mutex::imp_unlock_shared(u32 old)
{
ensure(old - 1 < c_err); // "shared_mutex underflow"
// Check reader count, notify the writer if necessary
if ((old - 1) % c_one == 0)
{
imp_signal();
}
}
void shared_mutex::imp_wait()
{
while (true)
{
const auto [old, ok] = m_value.fetch_op([](u32& value)
{
if (value >= c_sig)
{
value -= c_sig;
return true;
}
return false;
});
if (ok)
{
break;
}
m_value.wait(old, c_sig);
}
}
void shared_mutex::imp_signal()
{
m_value += c_sig;
m_value.notify_one(c_sig);
}
void shared_mutex::imp_lock(u32 val)
{
ensure(val < c_err); // "shared_mutex underflow"
for (int i = 0; i < 10; i++)
{
busy_wait();
if (!m_value && try_lock())
{
return;
}
}
const u32 old = m_value.fetch_add(c_one);
if (old == 0)
{
return;
}
ensure((old % c_sig) + c_one < c_sig); // "shared_mutex overflow"
imp_wait();
}
void shared_mutex::imp_unlock(u32 old)
{
ensure(old - c_one < c_err); // "shared_mutex underflow"
// 1) Notify the next writer if necessary
// 2) Notify all readers otherwise if necessary (currently indistinguishable from writers)
if (old - c_one)
{
imp_signal();
}
}
void shared_mutex::imp_lock_upgrade()
{
for (int i = 0; i < 10; i++)
{
busy_wait();
if (try_lock_upgrade())
{
return;
}
}
// Convert to writer lock
const u32 old = m_value.fetch_add(c_one - 1);
ensure((old % c_sig) + c_one - 1 < c_sig); // "shared_mutex overflow"
if (old % c_one == 1)
{
return;
}
imp_wait();
}
void shared_mutex::imp_lock_unlock()
{
u32 _max = 1;
for (int i = 0; i < 30; i++)
{
const u32 val = m_value;
if (val % c_one == 0 && (val / c_one < _max || val >= c_sig))
{
// Return if have cought a state where:
// 1) Mutex is free
// 2) Total number of waiters decreased since last check
// 3) Signal bit is set (if used on the platform)
return;
}
_max = val / c_one;
busy_wait(1500);
}
// Lock and unlock
if (!m_value.fetch_add(c_one))
{
unlock();
return;
}
imp_wait();
unlock();
}