1 | #pragma once |
---|
2 | |
---|
3 | #include <locks.hfa> |
---|
4 | #include <list.hfa> |
---|
5 | #include <mutex_stmt.hfa> |
---|
6 | #include "select.hfa" |
---|
7 | |
---|
8 | // returns true if woken due to shutdown |
---|
9 | // blocks thread on list and releases passed lock |
---|
10 | static inline bool block( dlist( select_node ) & queue, void * elem_ptr, go_mutex & lock ) { |
---|
11 | select_node sn{ active_thread(), elem_ptr }; |
---|
12 | insert_last( queue, sn ); |
---|
13 | unlock( lock ); |
---|
14 | park(); |
---|
15 | return sn.extra == 0p; |
---|
16 | } |
---|
17 | |
---|
18 | // Waituntil support (un)register_select helper routine |
---|
19 | // Sets select node avail if not special OR case and then unlocks |
---|
20 | static inline void __set_avail_then_unlock( select_node & node, go_mutex & mutex_lock ) { |
---|
21 | if ( node.park_counter ) __make_select_node_available( node ); |
---|
22 | unlock( mutex_lock ); |
---|
23 | } |
---|
24 | |
---|
25 | // void * used for some fields since exceptions don't work with parametric polymorphism currently |
---|
26 | exception channel_closed { |
---|
27 | // on failed insert elem is a ptr to the element attempting to be inserted |
---|
28 | // on failed remove elem ptr is 0p |
---|
29 | // on resumption of a failed insert this elem will be inserted |
---|
30 | // so a user may modify it in the resumption handler |
---|
31 | void * elem; |
---|
32 | |
---|
33 | // pointer to chan that is closed |
---|
34 | void * closed_chan; |
---|
35 | }; |
---|
36 | vtable(channel_closed) channel_closed_vt; |
---|
37 | |
---|
38 | // #define CHAN_STATS // define this to get channel stats printed in dtor |
---|
39 | |
---|
40 | forall( T ) { |
---|
41 | |
---|
42 | struct __attribute__((aligned(128))) channel { |
---|
43 | size_t size, front, back, count; |
---|
44 | T * buffer; |
---|
45 | dlist( select_node ) prods, cons; // lists of blocked threads |
---|
46 | go_mutex mutex_lock; // MX lock |
---|
47 | bool closed; // indicates channel close/open |
---|
48 | #ifdef CHAN_STATS |
---|
49 | size_t blocks, operations; // counts total ops and ops resulting in a blocked thd |
---|
50 | #endif |
---|
51 | }; |
---|
52 | |
---|
53 | static inline void ?{}( channel(T) &c, size_t _size ) with(c) { |
---|
54 | size = _size; |
---|
55 | front = back = count = 0; |
---|
56 | if ( size != 0 ) buffer = aalloc( size ); |
---|
57 | prods{}; |
---|
58 | cons{}; |
---|
59 | mutex_lock{}; |
---|
60 | closed = false; |
---|
61 | #ifdef CHAN_STATS |
---|
62 | blocks = 0; |
---|
63 | operations = 0; |
---|
64 | #endif |
---|
65 | } |
---|
66 | |
---|
67 | static inline void ?{}( channel(T) &c ){ ((channel(T) &)c){ 0 }; } |
---|
68 | static inline void ^?{}( channel(T) &c ) with(c) { |
---|
69 | #ifdef CHAN_STATS |
---|
70 | printf("Channel %p Blocks: %lu, Operations: %lu, %.2f%% of ops blocked\n", &c, blocks, operations, ((double)blocks)/operations * 100); |
---|
71 | #endif |
---|
72 | verifyf( cons`isEmpty && prods`isEmpty, "Attempted to delete channel with waiting threads (Deadlock).\n" ); |
---|
73 | if ( size != 0 ) delete( buffer ); |
---|
74 | } |
---|
75 | static inline size_t get_count( channel(T) & chan ) with(chan) { return count; } |
---|
76 | static inline size_t get_size( channel(T) & chan ) with(chan) { return size; } |
---|
77 | static inline bool has_waiters( channel(T) & chan ) with(chan) { return !cons`isEmpty || !prods`isEmpty; } |
---|
78 | static inline bool has_waiting_consumers( channel(T) & chan ) with(chan) { return !cons`isEmpty; } |
---|
79 | static inline bool has_waiting_producers( channel(T) & chan ) with(chan) { return !prods`isEmpty; } |
---|
80 | |
---|
81 | // closes the channel and notifies all blocked threads |
---|
82 | static inline void close( channel(T) & chan ) with(chan) { |
---|
83 | lock( mutex_lock ); |
---|
84 | closed = true; |
---|
85 | |
---|
86 | // flush waiting consumers and producers |
---|
87 | while ( has_waiting_consumers( chan ) ) { |
---|
88 | if( !__handle_waituntil_OR( cons ) ) // ensure we only signal special OR case threads when they win the race |
---|
89 | break; // if __handle_waituntil_OR returns false cons is empty so break |
---|
90 | cons`first.extra = 0p; |
---|
91 | wake_one( cons ); |
---|
92 | } |
---|
93 | while ( has_waiting_producers( chan ) ) { |
---|
94 | if( !__handle_waituntil_OR( prods ) ) // ensure we only signal special OR case threads when they win the race |
---|
95 | break; // if __handle_waituntil_OR returns false prods is empty so break |
---|
96 | prods`first.extra = 0p; |
---|
97 | wake_one( prods ); |
---|
98 | } |
---|
99 | unlock(mutex_lock); |
---|
100 | } |
---|
101 | |
---|
102 | static inline void is_closed( channel(T) & chan ) with(chan) { return closed; } |
---|
103 | |
---|
104 | // used to hand an element to a blocked consumer and signal it |
---|
105 | static inline void __cons_handoff( channel(T) & chan, T & elem ) with(chan) { |
---|
106 | memcpy( cons`first.extra, (void *)&elem, sizeof(T) ); // do waiting consumer work |
---|
107 | wake_one( cons ); |
---|
108 | } |
---|
109 | |
---|
110 | // used to hand an element to a blocked producer and signal it |
---|
111 | static inline void __prods_handoff( channel(T) & chan, T & retval ) with(chan) { |
---|
112 | memcpy( (void *)&retval, prods`first.extra, sizeof(T) ); |
---|
113 | wake_one( prods ); |
---|
114 | } |
---|
115 | |
---|
116 | static inline void flush( channel(T) & chan, T elem ) with(chan) { |
---|
117 | lock( mutex_lock ); |
---|
118 | while ( count == 0 && !cons`isEmpty ) { |
---|
119 | __cons_handoff( chan, elem ); |
---|
120 | } |
---|
121 | unlock( mutex_lock ); |
---|
122 | } |
---|
123 | |
---|
124 | // handles buffer insert |
---|
125 | static inline void __buf_insert( channel(T) & chan, T & elem ) with(chan) { |
---|
126 | memcpy( (void *)&buffer[back], (void *)&elem, sizeof(T) ); |
---|
127 | count += 1; |
---|
128 | back++; |
---|
129 | if ( back == size ) back = 0; |
---|
130 | } |
---|
131 | |
---|
132 | // needed to avoid an extra copy in closed case |
---|
133 | static inline bool __internal_try_insert( channel(T) & chan, T & elem ) with(chan) { |
---|
134 | lock( mutex_lock ); |
---|
135 | #ifdef CHAN_STATS |
---|
136 | operations++; |
---|
137 | #endif |
---|
138 | |
---|
139 | ConsEmpty: if ( !cons`isEmpty ) { |
---|
140 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty; |
---|
141 | __cons_handoff( chan, elem ); |
---|
142 | unlock( mutex_lock ); |
---|
143 | return true; |
---|
144 | } |
---|
145 | |
---|
146 | if ( count == size ) { unlock( mutex_lock ); return false; } |
---|
147 | |
---|
148 | __buf_insert( chan, elem ); |
---|
149 | unlock( mutex_lock ); |
---|
150 | return true; |
---|
151 | } |
---|
152 | |
---|
153 | // attempts a nonblocking insert |
---|
154 | // returns true if insert was successful, false otherwise |
---|
155 | static inline bool try_insert( channel(T) & chan, T elem ) { return __internal_try_insert( chan, elem ); } |
---|
156 | |
---|
157 | // handles closed case of insert routine |
---|
158 | static inline void __closed_insert( channel(T) & chan, T & elem ) with(chan) { |
---|
159 | channel_closed except{ &channel_closed_vt, &elem, &chan }; |
---|
160 | throwResume except; // throw closed resumption |
---|
161 | if ( !__internal_try_insert( chan, elem ) ) throw except; // if try to insert fails (would block), throw termination |
---|
162 | } |
---|
163 | |
---|
164 | static inline void insert( channel(T) & chan, T elem ) with(chan) { |
---|
165 | // check for close before acquire mx |
---|
166 | if ( unlikely(closed) ) { |
---|
167 | __closed_insert( chan, elem ); |
---|
168 | return; |
---|
169 | } |
---|
170 | |
---|
171 | lock( mutex_lock ); |
---|
172 | |
---|
173 | #ifdef CHAN_STATS |
---|
174 | if ( !closed ) operations++; |
---|
175 | #endif |
---|
176 | |
---|
177 | // if closed handle |
---|
178 | if ( unlikely(closed) ) { |
---|
179 | unlock( mutex_lock ); |
---|
180 | __closed_insert( chan, elem ); |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | // buffer count must be zero if cons are blocked (also handles zero-size case) |
---|
185 | ConsEmpty: if ( !cons`isEmpty ) { |
---|
186 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty; |
---|
187 | __cons_handoff( chan, elem ); |
---|
188 | unlock( mutex_lock ); |
---|
189 | return; |
---|
190 | } |
---|
191 | |
---|
192 | // wait if buffer is full, work will be completed by someone else |
---|
193 | if ( count == size ) { |
---|
194 | #ifdef CHAN_STATS |
---|
195 | blocks++; |
---|
196 | #endif |
---|
197 | |
---|
198 | // check for if woken due to close |
---|
199 | if ( unlikely( block( prods, &elem, mutex_lock ) ) ) |
---|
200 | __closed_insert( chan, elem ); |
---|
201 | return; |
---|
202 | } // if |
---|
203 | |
---|
204 | __buf_insert( chan, elem ); |
---|
205 | unlock( mutex_lock ); |
---|
206 | } |
---|
207 | |
---|
208 | // does the buffer remove and potentially does waiting producer work |
---|
209 | static inline void __do_remove( channel(T) & chan, T & retval ) with(chan) { |
---|
210 | memcpy( (void *)&retval, (void *)&buffer[front], sizeof(T) ); |
---|
211 | count -= 1; |
---|
212 | front = (front + 1) % size; |
---|
213 | if (count == size - 1 && !prods`isEmpty ) { |
---|
214 | if ( !__handle_waituntil_OR( prods ) ) return; |
---|
215 | __buf_insert( chan, *(T *)prods`first.extra ); // do waiting producer work |
---|
216 | wake_one( prods ); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | // needed to avoid an extra copy in closed case and single return val case |
---|
221 | static inline bool __internal_try_remove( channel(T) & chan, T & retval ) with(chan) { |
---|
222 | lock( mutex_lock ); |
---|
223 | #ifdef CHAN_STATS |
---|
224 | operations++; |
---|
225 | #endif |
---|
226 | |
---|
227 | ZeroSize: if ( size == 0 && !prods`isEmpty ) { |
---|
228 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize; |
---|
229 | __prods_handoff( chan, retval ); |
---|
230 | unlock( mutex_lock ); |
---|
231 | return true; |
---|
232 | } |
---|
233 | |
---|
234 | if ( count == 0 ) { unlock( mutex_lock ); return false; } |
---|
235 | |
---|
236 | __do_remove( chan, retval ); |
---|
237 | unlock( mutex_lock ); |
---|
238 | return true; |
---|
239 | } |
---|
240 | |
---|
241 | // attempts a nonblocking remove |
---|
242 | // returns [T, true] if insert was successful |
---|
243 | // returns [T, false] if insert was successful (T uninit) |
---|
244 | static inline [T, bool] try_remove( channel(T) & chan ) { |
---|
245 | T retval; |
---|
246 | bool success = __internal_try_remove( chan, retval ); |
---|
247 | return [ retval, success ]; |
---|
248 | } |
---|
249 | |
---|
250 | static inline T try_remove( channel(T) & chan ) { |
---|
251 | T retval; |
---|
252 | __internal_try_remove( chan, retval ); |
---|
253 | return retval; |
---|
254 | } |
---|
255 | |
---|
256 | // handles closed case of insert routine |
---|
257 | static inline void __closed_remove( channel(T) & chan, T & retval ) with(chan) { |
---|
258 | channel_closed except{ &channel_closed_vt, 0p, &chan }; |
---|
259 | throwResume except; // throw resumption |
---|
260 | if ( !__internal_try_remove( chan, retval ) ) throw except; // if try to remove fails (would block), throw termination |
---|
261 | } |
---|
262 | |
---|
263 | static inline T remove( channel(T) & chan ) with(chan) { |
---|
264 | T retval; |
---|
265 | if ( unlikely(closed) ) { |
---|
266 | __closed_remove( chan, retval ); |
---|
267 | return retval; |
---|
268 | } |
---|
269 | lock( mutex_lock ); |
---|
270 | |
---|
271 | #ifdef CHAN_STATS |
---|
272 | if ( !closed ) operations++; |
---|
273 | #endif |
---|
274 | |
---|
275 | if ( unlikely(closed) ) { |
---|
276 | unlock( mutex_lock ); |
---|
277 | __closed_remove( chan, retval ); |
---|
278 | return retval; |
---|
279 | } |
---|
280 | |
---|
281 | // have to check for the zero size channel case |
---|
282 | ZeroSize: if ( size == 0 && !prods`isEmpty ) { |
---|
283 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize; |
---|
284 | __prods_handoff( chan, retval ); |
---|
285 | unlock( mutex_lock ); |
---|
286 | return retval; |
---|
287 | } |
---|
288 | |
---|
289 | // wait if buffer is empty, work will be completed by someone else |
---|
290 | if ( count == 0 ) { |
---|
291 | #ifdef CHAN_STATS |
---|
292 | blocks++; |
---|
293 | #endif |
---|
294 | // check for if woken due to close |
---|
295 | if ( unlikely( block( cons, &retval, mutex_lock ) ) ) |
---|
296 | __closed_remove( chan, retval ); |
---|
297 | return retval; |
---|
298 | } |
---|
299 | |
---|
300 | // Remove from buffer |
---|
301 | __do_remove( chan, retval ); |
---|
302 | unlock( mutex_lock ); |
---|
303 | return retval; |
---|
304 | } |
---|
305 | |
---|
306 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
307 | // The following is support for waituntil (select) statements |
---|
308 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
309 | static inline bool unregister_chan( channel(T) & chan, select_node & node ) with(chan) { |
---|
310 | // if ( !node`isListed && !node.park_counter ) return false; // handle special OR case |
---|
311 | lock( mutex_lock ); |
---|
312 | if ( node`isListed ) { // op wasn't performed |
---|
313 | #ifdef CHAN_STATS |
---|
314 | operations--; |
---|
315 | #endif |
---|
316 | remove( node ); |
---|
317 | unlock( mutex_lock ); |
---|
318 | return false; |
---|
319 | } |
---|
320 | unlock( mutex_lock ); |
---|
321 | |
---|
322 | // only return true when not special OR case, not exceptional calse and status is SAT |
---|
323 | return ( node.extra == 0p || !node.park_counter ) ? false : *node.clause_status == __SELECT_SAT; |
---|
324 | } |
---|
325 | |
---|
326 | // type used by select statement to capture a chan read as the selected operation |
---|
327 | struct chan_read { |
---|
328 | T & ret; |
---|
329 | channel(T) & chan; |
---|
330 | }; |
---|
331 | |
---|
332 | static inline void ?{}( chan_read(T) & cr, channel(T) & chan, T & ret ) { |
---|
333 | &cr.chan = &chan; |
---|
334 | &cr.ret = &ret; |
---|
335 | } |
---|
336 | static inline chan_read(T) ?<<?( T & ret, channel(T) & chan ) { chan_read(T) cr{ chan, ret }; return cr; } |
---|
337 | |
---|
338 | static inline void __handle_select_closed_read( chan_read(T) & this, select_node & node ) with(this.chan, this) { |
---|
339 | __closed_remove( chan, ret ); |
---|
340 | // if we get here then the insert succeeded |
---|
341 | __make_select_node_available( node ); |
---|
342 | } |
---|
343 | |
---|
344 | static inline bool register_select( chan_read(T) & this, select_node & node ) with(this.chan, this) { |
---|
345 | lock( mutex_lock ); |
---|
346 | node.extra = &ret; // set .extra so that if it == 0p later in on_selected it is due to channel close |
---|
347 | |
---|
348 | #ifdef CHAN_STATS |
---|
349 | if ( !closed ) operations++; |
---|
350 | #endif |
---|
351 | |
---|
352 | // check if we can complete operation. If so race to establish winner in special OR case |
---|
353 | if ( !node.park_counter && ( count != 0 || !prods`isEmpty || unlikely(closed) ) ) { |
---|
354 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering |
---|
355 | unlock( mutex_lock ); |
---|
356 | return false; |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | if ( unlikely(closed) ) { |
---|
361 | unlock( mutex_lock ); |
---|
362 | __handle_select_closed_read( this, node ); |
---|
363 | return true; |
---|
364 | } |
---|
365 | |
---|
366 | // have to check for the zero size channel case |
---|
367 | ZeroSize: if ( size == 0 && !prods`isEmpty ) { |
---|
368 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize; |
---|
369 | __prods_handoff( chan, ret ); |
---|
370 | __set_avail_then_unlock( node, mutex_lock ); |
---|
371 | return true; |
---|
372 | } |
---|
373 | |
---|
374 | // wait if buffer is empty, work will be completed by someone else |
---|
375 | if ( count == 0 ) { |
---|
376 | #ifdef CHAN_STATS |
---|
377 | blocks++; |
---|
378 | #endif |
---|
379 | |
---|
380 | insert_last( cons, node ); |
---|
381 | unlock( mutex_lock ); |
---|
382 | return false; |
---|
383 | } |
---|
384 | |
---|
385 | // Remove from buffer |
---|
386 | __do_remove( chan, ret ); |
---|
387 | __set_avail_then_unlock( node, mutex_lock ); |
---|
388 | return true; |
---|
389 | } |
---|
390 | static inline bool unregister_select( chan_read(T) & this, select_node & node ) { return unregister_chan( this.chan, node ); } |
---|
391 | static inline bool on_selected( chan_read(T) & this, select_node & node ) with(this) { |
---|
392 | if ( node.extra == 0p ) // check if woken up due to closed channel |
---|
393 | __closed_remove( chan, ret ); |
---|
394 | // This is only reachable if not closed or closed exception was handled |
---|
395 | return true; |
---|
396 | } |
---|
397 | |
---|
398 | // type used by select statement to capture a chan write as the selected operation |
---|
399 | struct chan_write { |
---|
400 | T elem; |
---|
401 | channel(T) & chan; |
---|
402 | }; |
---|
403 | |
---|
404 | static inline void ?{}( chan_write(T) & cw, channel(T) & chan, T elem ) { |
---|
405 | &cw.chan = &chan; |
---|
406 | memcpy( (void *)&cw.elem, (void *)&elem, sizeof(T) ); |
---|
407 | } |
---|
408 | static inline chan_write(T) ?>>?( T elem, channel(T) & chan ) { chan_write(T) cw{ chan, elem }; return cw; } |
---|
409 | |
---|
410 | static inline void __handle_select_closed_write( chan_write(T) & this, select_node & node ) with(this.chan, this) { |
---|
411 | __closed_insert( chan, elem ); |
---|
412 | // if we get here then the insert succeeded |
---|
413 | __make_select_node_available( node ); |
---|
414 | } |
---|
415 | |
---|
416 | static inline bool register_select( chan_write(T) & this, select_node & node ) with(this.chan, this) { |
---|
417 | lock( mutex_lock ); |
---|
418 | node.extra = &elem; // set .extra so that if it == 0p later in on_selected it is due to channel close |
---|
419 | |
---|
420 | #ifdef CHAN_STATS |
---|
421 | if ( !closed ) operations++; |
---|
422 | #endif |
---|
423 | |
---|
424 | // check if we can complete operation. If so race to establish winner in special OR case |
---|
425 | if ( !node.park_counter && ( count != size || !cons`isEmpty || unlikely(closed) ) ) { |
---|
426 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering |
---|
427 | unlock( mutex_lock ); |
---|
428 | return false; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | // if closed handle |
---|
433 | if ( unlikely(closed) ) { |
---|
434 | unlock( mutex_lock ); |
---|
435 | __handle_select_closed_write( this, node ); |
---|
436 | return true; |
---|
437 | } |
---|
438 | |
---|
439 | // handle blocked consumer case via handoff (buffer is implicitly empty) |
---|
440 | ConsEmpty: if ( !cons`isEmpty ) { |
---|
441 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty; |
---|
442 | __cons_handoff( chan, elem ); |
---|
443 | __set_avail_then_unlock( node, mutex_lock ); |
---|
444 | return true; |
---|
445 | } |
---|
446 | |
---|
447 | // insert node in list if buffer is full, work will be completed by someone else |
---|
448 | if ( count == size ) { |
---|
449 | #ifdef CHAN_STATS |
---|
450 | blocks++; |
---|
451 | #endif |
---|
452 | |
---|
453 | insert_last( prods, node ); |
---|
454 | unlock( mutex_lock ); |
---|
455 | return false; |
---|
456 | } // if |
---|
457 | |
---|
458 | // otherwise carry out write either via normal insert |
---|
459 | __buf_insert( chan, elem ); |
---|
460 | __set_avail_then_unlock( node, mutex_lock ); |
---|
461 | return true; |
---|
462 | } |
---|
463 | static inline bool unregister_select( chan_write(T) & this, select_node & node ) { return unregister_chan( this.chan, node ); } |
---|
464 | |
---|
465 | static inline bool on_selected( chan_write(T) & this, select_node & node ) with(this) { |
---|
466 | if ( node.extra == 0p ) // check if woken up due to closed channel |
---|
467 | __closed_insert( chan, elem ); |
---|
468 | |
---|
469 | // This is only reachable if not closed or closed exception was handled |
---|
470 | return true; |
---|
471 | } |
---|
472 | |
---|
473 | |
---|
474 | } // forall( T ) |
---|
475 | |
---|
476 | |
---|
477 | |
---|