-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.cpp
201 lines (185 loc) · 3.84 KB
/
go.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "go.h"
namespace go {
volatile unsigned int nmspinning = 0;
RWMutex nmspinning_lock;
const unsigned int MAX_PROCS = 8;
unsigned newP = 0;
unsigned newM = 0;
Queue<P*> gfpqueue;
Queue<std::thread*> tqueue;
std::vector<P*> gplist;
//std::mutex gplist_mutex;
//std::unique_lock<std::mutex> gplist_lock(gplist_mutex);
std::mutex gplist_lock;
template <class T> void debugPrint(T t) {
#ifdef DEBUG //Because the empty function will be optimized by compiler
std::cout << t << std::endl;
#endif
}
void addnmspinning() {
nmspinning_lock.wLock();
nmspinning++;
nmspinning_lock.wUnlock();
}
void minusnmspinning() {
nmspinning_lock.wLock();
nmspinning--;
nmspinning_lock.wUnlock();
}
bool steal(P* p, P* p2) {
auto ng = p2->gqueue.queue.size();
if (ng == 0)
return false;
else {
ng = p2->gqueue.queue.size();
if (ng == 0)
return false;
for (unsigned i = 0;i <= ng / 2;i++)
{
p->gqueue.push(p2->gqueue.get());
}
}
debugPrint("steal success");
return true;
}
void spin(M* m) {
debugPrint("spinning...");
addnmspinning();
nmspinning_lock.rLock();
while (nmspinning == 1)
{
nmspinning_lock.rUnLock(); //spinning
nmspinning_lock.rLock();
}
nmspinning_lock.rUnLock();
minusnmspinning();
debugPrint("a m spin -> nonspin");
while (1) {
start_while:
if (m->g != nullptr) {
debugPrint("working...");
m->g->func(*(m->g->args));
delete m->g;
m->g = nullptr;
continue;
}
if (m->p == nullptr) {
debugPrint("find a new p to m");
auto nfp = gfpqueue.queue.size();
if (nfp == 0) {
debugPrint("a m go park");
delete m;
break;
}
else {
auto p = gfpqueue.get();
m->p = p;
p->free = false;
continue;
}
}
if (m->g == nullptr) {
debugPrint("find a new g to m");
auto ng = m->p->gqueue.queue.size();
if (ng == 0) {
auto np = gplist.size();
if (np == 0) {
m->p = nullptr;
delete m;
debugPrint("a m go park");
break;
};
auto i = rand() % np;
if (steal(m->p, gplist[i])) {
continue;
}
else {
for (int j = 0;j < gplist.size();j++) {
i = (i + 1) % gplist.size();
if (steal(m->p, gplist[i])) {
goto start_while;
}
}
//gfpqueue.push(m->p);
gfpqueue.lock.lock();
gfpqueue.queue.push(m->p);
m->p->free = true;
gfpqueue.lock.unlock();
debugPrint("p steal faild and go free");
m->p = nullptr;
delete m;
debugPrint("a m go park");
break;
}
}
else {
auto g = m->p->gqueue.get();
m->g = g;
continue;
}
}
}
}
void gocommit(G* g) {
debugPrint("new g commit");
auto np = gplist.size();
P* p = nullptr;
if (np == 0) {
p = new P;
newP++;
debugPrint("new p");
gplist_lock.lock();
gplist.push_back(p);
gplist_lock.unlock();
p->gqueue.push(g);
gfpqueue.push(p);
debugPrint("a p go to gfpqueue");
}
else {
//TODO find a runnable p
auto np = gplist.size();
auto i = rand() % np;
while (gplist[i]->free)
i = (i + 1) % gplist.size();
gplist[i]->gqueue.queue.push(g);
}
np = gplist.size();
if (np < MAX_PROCS && nmspinning == 1) {
auto m = new M;
newM++;
debugPrint("new m");
if (p == nullptr) {
m->p = new P;
newP++;
}
else if (gfpqueue.queue.size() == 0) {
m->p = new P;
newP++;
}
else {
m->p = gfpqueue.get();
}
debugPrint("new p");
gplist_lock.lock();
gplist.push_back(m->p);
gplist_lock.unlock();
auto t = new std::thread (spin, m);
//t->detach();
tqueue.push(t);
}
}
void goinit() {
auto m = new M;
newM++;
debugPrint("new m");
auto t = new std::thread(spin, m);
//t->detach();
tqueue.push(t);
}
void goend() {
while (!tqueue.queue.empty()) {
auto t = tqueue.get();
t->join();
}
}
}