source: libcfa/src/concurrency/channel.hfa@ beeff61e

ADT ast-experimental
Last change on this file since beeff61e was beeff61e, checked in by caparsons <caparson@…>, 2 years ago

some cleanup and a bunch of changes to support waituntil statement

  • Property mode set to 100644
File size: 15.8 KB
RevLine 
[5e4a830]1#pragma once
2
[4a962d8]3#include <locks.hfa>
[d30e3eb]4#include <list.hfa>
[a45e21c]5#include <mutex_stmt.hfa>
[beeff61e]6#include "select.hfa"
[a45e21c]7
8// returns true if woken due to shutdown
9// blocks thread on list and releases passed lock
[beeff61e]10static 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 );
[a45e21c]13 unlock( lock );
14 park();
[beeff61e]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
20static 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 );
[a45e21c]23}
24
25// void * used for some fields since exceptions don't work with parametric polymorphism currently
26exception 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};
36vtable(channel_closed) channel_closed_vt;
37
38// #define CHAN_STATS // define this to get channel stats printed in dtor
39
[4a962d8]40forall( T ) {
[d30e3eb]41
[a45e21c]42struct __attribute__((aligned(128))) channel {
43 size_t size, front, back, count;
[4a962d8]44 T * buffer;
[beeff61e]45 dlist( select_node ) prods, cons; // lists of blocked threads
[a45e21c]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
[4a962d8]51};
52
53static inline void ?{}( channel(T) &c, size_t _size ) with(c) {
54 size = _size;
55 front = back = count = 0;
[beeff61e]56 if ( size != 0 ) buffer = aalloc( size );
[4a962d8]57 prods{};
58 cons{};
59 mutex_lock{};
[a45e21c]60 closed = false;
61 #ifdef CHAN_STATS
62 blocks = 0;
63 operations = 0;
64 #endif
[4a962d8]65}
66
67static inline void ?{}( channel(T) &c ){ ((channel(T) &)c){ 0 }; }
[a45e21c]68static 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" );
[beeff61e]73 if ( size != 0 ) delete( buffer );
[a45e21c]74}
[42b739d7]75static inline size_t get_count( channel(T) & chan ) with(chan) { return count; }
76static inline size_t get_size( channel(T) & chan ) with(chan) { return size; }
[d30e3eb]77static inline bool has_waiters( channel(T) & chan ) with(chan) { return !cons`isEmpty || !prods`isEmpty; }
78static inline bool has_waiting_consumers( channel(T) & chan ) with(chan) { return !cons`isEmpty; }
79static inline bool has_waiting_producers( channel(T) & chan ) with(chan) { return !prods`isEmpty; }
[4a962d8]80
[a45e21c]81// closes the channel and notifies all blocked threads
82static 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 ) ) {
[beeff61e]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;
[a45e21c]91 wake_one( cons );
92 }
93 while ( has_waiting_producers( chan ) ) {
[beeff61e]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;
[a45e21c]97 wake_one( prods );
98 }
99 unlock(mutex_lock);
100}
101
102static inline void is_closed( channel(T) & chan ) with(chan) { return closed; }
103
[beeff61e]104// used to hand an element to a blocked consumer and signal it
105static 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
111static 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
[a45e21c]116static inline void flush( channel(T) & chan, T elem ) with(chan) {
117 lock( mutex_lock );
118 while ( count == 0 && !cons`isEmpty ) {
[beeff61e]119 __cons_handoff( chan, elem );
[a45e21c]120 }
121 unlock( mutex_lock );
122}
123
124// handles buffer insert
125static inline void __buf_insert( channel(T) & chan, T & elem ) with(chan) {
[beeff61e]126 memcpy( (void *)&buffer[back], (void *)&elem, sizeof(T) );
[4a962d8]127 count += 1;
128 back++;
129 if ( back == size ) back = 0;
130}
131
[a45e21c]132// needed to avoid an extra copy in closed case
133static inline bool __internal_try_insert( channel(T) & chan, T & elem ) with(chan) {
134 lock( mutex_lock );
135 #ifdef CHAN_STATS
136 operations++;
137 #endif
[beeff61e]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
[a45e21c]146 if ( count == size ) { unlock( mutex_lock ); return false; }
[beeff61e]147
148 __buf_insert( chan, elem );
[a45e21c]149 unlock( mutex_lock );
150 return true;
151}
152
153// attempts a nonblocking insert
154// returns true if insert was successful, false otherwise
155static inline bool try_insert( channel(T) & chan, T elem ) { return __internal_try_insert( chan, elem ); }
156
157// handles closed case of insert routine
158static inline void __closed_insert( channel(T) & chan, T & elem ) with(chan) {
[beeff61e]159 channel_closed except{ &channel_closed_vt, &elem, &chan };
[a45e21c]160 throwResume except; // throw closed resumption
161 if ( !__internal_try_insert( chan, elem ) ) throw except; // if try to insert fails (would block), throw termination
[d30e3eb]162}
[4a962d8]163
[42b739d7]164static inline void insert( channel(T) & chan, T elem ) with(chan) {
[a45e21c]165 // check for close before acquire mx
166 if ( unlikely(closed) ) {
167 __closed_insert( chan, elem );
168 return;
169 }
170
[4a962d8]171 lock( mutex_lock );
172
[a45e21c]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
[beeff61e]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 );
[5c931e0]188 unlock( mutex_lock );
[beeff61e]189 return;
[5c931e0]190 }
191
[4a962d8]192 // wait if buffer is full, work will be completed by someone else
[d30e3eb]193 if ( count == size ) {
[a45e21c]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 );
[4a962d8]201 return;
202 } // if
203
[beeff61e]204 __buf_insert( chan, elem );
[4a962d8]205 unlock( mutex_lock );
[a45e21c]206}
[4a962d8]207
[a45e21c]208// does the buffer remove and potentially does waiting producer work
209static inline void __do_remove( channel(T) & chan, T & retval ) with(chan) {
[beeff61e]210 memcpy( (void *)&retval, (void *)&buffer[front], sizeof(T) );
211 count -= 1;
212 front = (front + 1) % size;
[d30e3eb]213 if (count == size - 1 && !prods`isEmpty ) {
[beeff61e]214 if ( !__handle_waituntil_OR( prods ) ) return;
215 __buf_insert( chan, *(T *)prods`first.extra ); // do waiting producer work
[d30e3eb]216 wake_one( prods );
217 }
[4a962d8]218}
[0e16a2d]219
[a45e21c]220// needed to avoid an extra copy in closed case and single return val case
221static inline bool __internal_try_remove( channel(T) & chan, T & retval ) with(chan) {
[0e16a2d]222 lock( mutex_lock );
[a45e21c]223 #ifdef CHAN_STATS
224 operations++;
225 #endif
[beeff61e]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
[a45e21c]234 if ( count == 0 ) { unlock( mutex_lock ); return false; }
[beeff61e]235
[a45e21c]236 __do_remove( chan, retval );
[0e16a2d]237 unlock( mutex_lock );
[a45e21c]238 return true;
[0e16a2d]239}
240
[a45e21c]241// attempts a nonblocking remove
242// returns [T, true] if insert was successful
243// returns [T, false] if insert was successful (T uninit)
244static inline [T, bool] try_remove( channel(T) & chan ) {
[0e16a2d]245 T retval;
[beeff61e]246 bool success = __internal_try_remove( chan, retval );
247 return [ retval, success ];
[a45e21c]248}
[0e16a2d]249
[beeff61e]250static inline T try_remove( channel(T) & chan ) {
[a45e21c]251 T retval;
252 __internal_try_remove( chan, retval );
[0e16a2d]253 return retval;
254}
255
[a45e21c]256// handles closed case of insert routine
257static inline void __closed_remove( channel(T) & chan, T & retval ) with(chan) {
[beeff61e]258 channel_closed except{ &channel_closed_vt, 0p, &chan };
[a45e21c]259 throwResume except; // throw resumption
260 if ( !__internal_try_remove( chan, retval ) ) throw except; // if try to remove fails (would block), throw termination
[0e16a2d]261}
262
[a45e21c]263static 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 );
[0e16a2d]270
[a45e21c]271 #ifdef CHAN_STATS
272 if ( !closed ) operations++;
273 #endif
[0e16a2d]274
[a45e21c]275 if ( unlikely(closed) ) {
276 unlock( mutex_lock );
277 __closed_remove( chan, retval );
278 return retval;
279 }
[0e16a2d]280
[a45e21c]281 // have to check for the zero size channel case
[beeff61e]282 ZeroSize: if ( size == 0 && !prods`isEmpty ) {
283 if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
284 __prods_handoff( chan, retval );
[a45e21c]285 unlock( mutex_lock );
286 return retval;
287 }
[0e16a2d]288
[a45e21c]289 // wait if buffer is empty, work will be completed by someone else
[beeff61e]290 if ( count == 0 ) {
[a45e21c]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 }
[0e16a2d]299
300 // Remove from buffer
[a45e21c]301 __do_remove( chan, retval );
[0e16a2d]302 unlock( mutex_lock );
303 return retval;
304}
[beeff61e]305
306///////////////////////////////////////////////////////////////////////////////////////////
307// The following is support for waituntil (select) statements
308///////////////////////////////////////////////////////////////////////////////////////////
309static 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
327struct chan_read {
328 channel(T) & chan;
329 T & ret;
330};
331
332static inline void ?{}( chan_read(T) & cr, channel(T) & chan, T & ret ) {
333 &cr.chan = &chan;
334 &cr.ret = &ret;
335}
336static inline chan_read(T) ?<<?( T & ret, channel(T) & chan ) { chan_read(T) cr{ chan, ret }; return cr; }
337
338static 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
344static inline bool register_select( chan_read(T) & this, select_node & node ) with(this.chan, this) {
345 // mutex(sout) sout | "register_read";
346 lock( mutex_lock );
347 node.extra = &ret; // set .extra so that if it == 0p later in on_selected it is due to channel close
348
349 #ifdef CHAN_STATS
350 if ( !closed ) operations++;
351 #endif
352
353 // check if we can complete operation. If so race to establish winner in special OR case
354 if ( !node.park_counter && ( count != 0 || !prods`isEmpty || unlikely(closed) ) ) {
355 if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
356 unlock( mutex_lock );
357 return false;
358 }
359 }
360
361 if ( unlikely(closed) ) {
362 unlock( mutex_lock );
363 __handle_select_closed_read( this, node );
364 return true;
365 }
366
367 // have to check for the zero size channel case
368 ZeroSize: if ( size == 0 && !prods`isEmpty ) {
369 if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
370 __prods_handoff( chan, ret );
371 __set_avail_then_unlock( node, mutex_lock );
372 return true;
373 }
374
375 // wait if buffer is empty, work will be completed by someone else
376 if ( count == 0 ) {
377 #ifdef CHAN_STATS
378 blocks++;
379 #endif
380
381 insert_last( cons, node );
382 unlock( mutex_lock );
383 return false;
384 }
385
386 // Remove from buffer
387 __do_remove( chan, ret );
388 __set_avail_then_unlock( node, mutex_lock );
389 return true;
390}
391static inline bool unregister_select( chan_read(T) & this, select_node & node ) { return unregister_chan( this.chan, node ); }
392static inline bool on_selected( chan_read(T) & this, select_node & node ) with(this) {
393 if ( node.extra == 0p ) // check if woken up due to closed channel
394 __closed_remove( chan, ret );
395 // This is only reachable if not closed or closed exception was handled
396 return true;
397}
398
399// type used by select statement to capture a chan write as the selected operation
400struct chan_write {
401 channel(T) & chan;
402 T elem;
403};
404
405static inline void ?{}( chan_write(T) & cw, channel(T) & chan, T elem ) {
406 &cw.chan = &chan;
407 memcpy( (void *)&cw.elem, (void *)&elem, sizeof(T) );
408}
409static inline chan_write(T) ?>>?( T elem, channel(T) & chan ) { chan_write(T) cw{ chan, elem }; return cw; }
410
411static inline void __handle_select_closed_write( chan_write(T) & this, select_node & node ) with(this.chan, this) {
412 __closed_insert( chan, elem );
413 // if we get here then the insert succeeded
414 __make_select_node_available( node );
415}
416
417static inline bool register_select( chan_write(T) & this, select_node & node ) with(this.chan, this) {
418 // mutex(sout) sout | "register_write";
419 lock( mutex_lock );
420 node.extra = &elem; // set .extra so that if it == 0p later in on_selected it is due to channel close
421
422 #ifdef CHAN_STATS
423 if ( !closed ) operations++;
424 #endif
425
426 // check if we can complete operation. If so race to establish winner in special OR case
427 if ( !node.park_counter && ( count != size || !cons`isEmpty || unlikely(closed) ) ) {
428 if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
429 unlock( mutex_lock );
430 return false;
431 }
432 }
433
434 // if closed handle
435 if ( unlikely(closed) ) {
436 unlock( mutex_lock );
437 __handle_select_closed_write( this, node );
438 return true;
439 }
440
441 // handle blocked consumer case via handoff (buffer is implicitly empty)
442 ConsEmpty: if ( !cons`isEmpty ) {
443 if ( !__handle_waituntil_OR( cons ) ) {
444 // mutex(sout) sout | "empty";
445 break ConsEmpty;
446 }
447 // mutex(sout) sout | "signal";
448 __cons_handoff( chan, elem );
449 __set_avail_then_unlock( node, mutex_lock );
450 return true;
451 }
452
453 // insert node in list if buffer is full, work will be completed by someone else
454 if ( count == size ) {
455 #ifdef CHAN_STATS
456 blocks++;
457 #endif
458
459 insert_last( prods, node );
460 unlock( mutex_lock );
461 return false;
462 } // if
463
464 // otherwise carry out write either via normal insert
465 __buf_insert( chan, elem );
466 __set_avail_then_unlock( node, mutex_lock );
467 return true;
468}
469static inline bool unregister_select( chan_write(T) & this, select_node & node ) { return unregister_chan( this.chan, node ); }
470
471static inline bool on_selected( chan_write(T) & this, select_node & node ) with(this) {
472 if ( node.extra == 0p ) // check if woken up due to closed channel
473 __closed_insert( chan, elem );
474
475 // This is only reachable if not closed or closed exception was handled
476 return true;
477}
478
479
[0e16a2d]480} // forall( T )
[beeff61e]481
482
483
Note: See TracBrowser for help on using the repository browser.