blob: b3ae8f470dc264be3a9ff1010810098ee027f9fe (
plain) (
blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
#include "rq.h"
#include "lfqueue.h"
#include <library/cpp/threading/atomic/bool.h>
#include <util/system/tls.h>
#include <util/system/pipe.h>
#include <util/system/event.h>
#include <util/system/mutex.h>
#include <util/system/condvar.h>
#include <util/system/guard.h>
#include <util/network/socket.h>
#include <util/generic/deque.h>
using namespace NNeh;
namespace {
class TBaseLockFreeRequestQueue: public IRequestQueue {
public:
void Clear() override {
IRequestRef req;
while (Q_.Dequeue(&req)) {
}
}
protected:
NNeh::TAutoLockFreeQueue<IRequest> Q_;
};
class TFdRequestQueue: public TBaseLockFreeRequestQueue {
public:
inline TFdRequestQueue() {
TPipeHandle::Pipe(R_, W_);
SetNonBlock(W_);
}
void Schedule(IRequestRef req) override {
Q_.Enqueue(req);
char ch = 42;
W_.Write(&ch, 1);
}
IRequestRef Next() override {
IRequestRef ret;
#if 0
for (size_t i = 0; i < 20; ++i) {
if (Q_.Dequeue(&ret)) {
return ret;
}
//asm volatile ("pause;");
}
#endif
while (!Q_.Dequeue(&ret)) {
char ch;
R_.Read(&ch, 1);
}
return ret;
}
private:
TPipeHandle R_;
TPipeHandle W_;
};
struct TNehFdEvent {
inline TNehFdEvent() {
TPipeHandle::Pipe(R, W);
SetNonBlock(W);
}
inline void Signal() noexcept {
char ch = 21;
W.Write(&ch, 1);
}
inline void Wait() noexcept {
char buf[128];
R.Read(buf, sizeof(buf));
}
TPipeHandle R;
TPipeHandle W;
};
template <class TEvent>
class TEventRequestQueue: public TBaseLockFreeRequestQueue {
public:
void Schedule(IRequestRef req) override {
Q_.Enqueue(req);
E_.Signal();
}
IRequestRef Next() override {
IRequestRef ret;
while (!Q_.Dequeue(&ret)) {
E_.Wait();
}
E_.Signal();
return ret;
}
private:
TEvent E_;
};
template <class TEvent>
class TLazyEventRequestQueue: public TBaseLockFreeRequestQueue {
public:
void Schedule(IRequestRef req) override {
Q_.Enqueue(req);
if (C_.Val()) {
E_.Signal();
}
}
IRequestRef Next() override {
IRequestRef ret;
C_.Inc();
while (!Q_.Dequeue(&ret)) {
E_.Wait();
}
C_.Dec();
if (Q_.Size() && C_.Val()) {
E_.Signal();
}
return ret;
}
private:
TEvent E_;
TAtomicCounter C_;
};
class TCondVarRequestQueue: public IRequestQueue {
public:
void Clear() override {
TGuard<TMutex> g(M_);
Q_.clear();
}
void Schedule(IRequestRef req) override {
{
TGuard<TMutex> g(M_);
Q_.push_back(req);
}
C_.Signal();
}
IRequestRef Next() override {
TGuard<TMutex> g(M_);
while (Q_.empty()) {
C_.Wait(M_);
}
IRequestRef ret = *Q_.begin();
Q_.pop_front();
return ret;
}
private:
TDeque<IRequestRef> Q_;
TMutex M_;
TCondVar C_;
};
class TBusyRequestQueue: public TBaseLockFreeRequestQueue {
public:
void Schedule(IRequestRef req) override {
Q_.Enqueue(req);
}
IRequestRef Next() override {
IRequestRef ret;
while (!Q_.Dequeue(&ret)) {
}
return ret;
}
};
class TSleepRequestQueue: public TBaseLockFreeRequestQueue {
public:
void Schedule(IRequestRef req) override {
Q_.Enqueue(req);
}
IRequestRef Next() override {
IRequestRef ret;
while (!Q_.Dequeue(&ret)) {
usleep(1);
}
return ret;
}
};
struct TStupidEvent {
inline TStupidEvent()
: InWait(false)
{
}
inline bool Signal() noexcept {
const bool ret = InWait;
Ev.Signal();
return ret;
}
inline void Wait() noexcept {
InWait = true;
Ev.Wait();
InWait = false;
}
TAutoEvent Ev;
NAtomic::TBool InWait;
};
template <class TEvent>
class TLFRequestQueue: public TBaseLockFreeRequestQueue {
struct TLocalQueue: public TEvent {
};
public:
void Schedule(IRequestRef req) override {
Q_.Enqueue(req);
for (TLocalQueue* lq = 0; FQ_.Dequeue(&lq) && !lq->Signal();) {
}
}
IRequestRef Next() override {
while (true) {
IRequestRef ret;
if (Q_.Dequeue(&ret)) {
return ret;
}
TLocalQueue* lq = LocalQueue();
FQ_.Enqueue(lq);
if (Q_.Dequeue(&ret)) {
TLocalQueue* besttry;
if (FQ_.Dequeue(&besttry)) {
if (besttry == lq) {
//huraay, get rid of spurious wakeup
} else {
FQ_.Enqueue(besttry);
}
}
return ret;
}
lq->Wait();
}
}
private:
static inline TLocalQueue* LocalQueue() noexcept {
Y_POD_STATIC_THREAD(TLocalQueue*)
lq((TLocalQueue*)0);
if (!lq) {
Y_STATIC_THREAD(TLocalQueue)
slq;
lq = &(TLocalQueue&)slq;
}
return lq;
}
private:
TLockFreeStack<TLocalQueue*> FQ_;
};
}
IRequestQueueRef NNeh::CreateRequestQueue() {
//return new TCondVarRequestQueue();
//return new TSleepRequestQueue();
//return new TBusyRequestQueue();
//return new TLFRequestQueue<TStupidEvent>();
#if defined(_freebsd_)
return new TFdRequestQueue();
#endif
//return new TFdRequestQueue();
return new TLazyEventRequestQueue<TAutoEvent>();
//return new TEventRequestQueue<TAutoEvent>();
//return new TEventRequestQueue<TNehFdEvent>();
}
|