-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.d
213 lines (197 loc) · 4.24 KB
/
channel.d
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
import core.sync.mutex : Mutex;
import goroutine : sleep;
import std.datetime : dur;
import std.traits : isInstanceOf;
//void select(T)(T...) if (isMadeOf(chan)) { go through each arg and first !arg.empty is winner }
/**
* chan allows messaging between threads without having to deal with locks, similar to how chan works in golang
*/
shared
class chan(T) {
Mutex lock;
private bool closed_; bool closed() {synchronized (lock) {return closed_;}} void close() { synchronized(lock) { closed_ = true; } }
struct Container(T) {
T value;
Container!T* next;
}
Container!T* current;
Container!T* last;
size_t length;
void insert(T v) {
Container!T* newItem = new Container!T();
newItem.value = v;
synchronized (lock) {
if (current is null) {
current = cast(shared)newItem;
last = cast(shared)newItem;
} else {
last.next = cast(shared)newItem;
last = cast(shared)newItem;
}
length++;
}
}
T getone() {
T ret;
synchronized (lock) {
ret = cast(T)current.value;
current = current.next;
length--;
}
return ret;
}
size_t maxItems;
bool blockOnFull = false;
this(int maxItems = 1024, bool blockOnFull = true) {
lock = cast(shared)new Mutex;
length = 0;
this.maxItems = maxItems;
this.blockOnFull = blockOnFull;
}
@property
void _(T value) {
bool done;
while(true) {
synchronized(lock) {
if (closed) {
throw new ChannelClosedException();
}
if (!done && length < maxItems) {
insert(value);
done = true;
} else if (!blockOnFull) {
throw new ChannelFullException("Channel Full");
}
if (length <= maxItems-1) {
break;
}
}
sleep(dur!"msecs"(1));
}
}
@property
T _() {
_startagain:
while(true) {
size_t len;
synchronized(lock) {
len = length;
if (len <= 0 && closed) {
throw new ChannelClosedException("on read");
}
}
if (len > 0) {
break;
}
sleep(dur!"msecs"(1));
};
T r;
synchronized(lock) {
auto len = length;
if (len <= 0) {
goto _startagain;
}
r = getone();
}
return r;
}
T popFront() {
return _();
}
T front() {
synchronized (lock) {
return cast(T)current.value;
}
}
void put(T v) {
_(v);
}
// check if there is something to read, chan will block if this returns false and you call _().
// NOTE: if another thread empties the chan between a call to this function and a call to _() the
// calling fiber/Thread will block
@property
bool empty() {
bool ret;
synchronized (lock) {
ret = length <= 0;
}
return ret;
}
// check if there is space in the chan to write to, chan will block if this returns false and you call _(T).
// NOTE: if another thread fills the chan between a call to this function and a call to _(T) the
// calling fiber/Thread will block
@property
bool writable() {
bool ret;
synchronized (lock) {
ret = length < maxItems;
}
return ret;
}
/+
void opAssign(T v) {
_(v);
}
@property
T get() {
return _();
}
alias get this;+/
}
shared(chan!T) makeChan(T)(int n, bool blockOnFull = true) {
return cast(shared)(new chan!T(n, blockOnFull));
}
int select(Args...)(Args args) if (allischan!Args()) {
import std.random : uniform;
while (true) {
int[] ready;
int closed;
ready.reserve(args.length);
foreach (i, arg; args) {
if (arg.closed)
closed++;
if (!arg.empty)
ready ~= i;
}
if (closed >= args.length) {
return -1;
}
if (ready.length > 0) {
auto idx = uniform(0,ready.length);
return ready[idx];
}
sleep();
}
}
int select(Args...)(Args args) if (!allischan!Args()) {
import std.conv;
foreach (i, arg; Args) {
static if (!isInstanceOf!(chan, arg)) {
static assert(0, "select(Args args) only accepts parameters of type: shared(chan) not argument "~ to!string(i+1) ~" of type: "~ arg.stringof);
}
}
assert(0, "should never reach here");
}
bool allischan(Args...)() {
foreach (arg; Args) {
static if (!isInstanceOf!(chan, arg)) {
return false;
}
}
return true;
}
class ChannelException : Exception {
this(string msg) {
super(msg,file,line,next);
}
}
class ChannelFullException : Exception {
this(string msg = "Channel Full") {
super(msg,file,line,next);
}
}
class ChannelClosedException : Exception {
this(string msg = "Channel Closed") {
super(msg,file,line,next);
}
}