// // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // channel.hfa -- LIBCFATHREAD // Runtime locks that used with the runtime thread system. // // Author : Colby Alexander Parsons // Created On : Thu Jan 21 19:46:50 2022 // Last Modified By : // Last Modified On : // Update Count : // #pragma once #include #include #include "select.hfa" // returns true if woken due to shutdown // blocks thread on list and releases passed lock static inline bool block( dlist( select_node ) & queue, void * elem_ptr, go_mutex & lock ) { select_node sn{ active_thread(), elem_ptr }; insert_last( queue, sn ); unlock( lock ); park(); __atomic_thread_fence( __ATOMIC_SEQ_CST ); return sn.extra == 0p; } // Waituntil support (un)register_select helper routine // Sets select node avail if not special OR case and then unlocks static inline void __set_avail_then_unlock( select_node & node, go_mutex & mutex_lock ) { if ( node.park_counter ) __make_select_node_available( node ); unlock( mutex_lock ); } // void * used for some fields since exceptions don't work with parametric polymorphism currently exception channel_closed { // on failed insert elem is a ptr to the element attempting to be inserted // on failed remove elem ptr is 0p // on resumption of a failed insert this elem will be inserted // so a user may modify it in the resumption handler void * elem; // pointer to chan that is closed void * closed_chan; }; vtable(channel_closed) channel_closed_vt; static inline bool is_insert( channel_closed & e ) { return e.elem != 0p; } static inline bool is_remove( channel_closed & e ) { return e.elem == 0p; } // #define CHAN_STATS // define this to get channel stats printed in dtor forall( T ) { struct __attribute__((aligned(128))) channel { size_t size, front, back, count; T * buffer; dlist( select_node ) prods, cons; // lists of blocked threads go_mutex mutex_lock; // MX lock bool closed; // indicates channel close/open #ifdef CHAN_STATS size_t p_blocks, p_ops, c_blocks, c_ops; // counts total ops and ops resulting in a blocked thd #endif }; static inline void ?{}( channel(T) & this, channel(T) this2 ) = void; static inline void ?=?( channel(T) & this, channel(T) this2 ) = void; static inline void ?{}( channel(T) &c, size_t _size ) with(c) { size = _size; front = back = count = 0; if ( size != 0 ) buffer = aalloc( size ); prods{}; cons{}; mutex_lock{}; closed = false; #ifdef CHAN_STATS p_blocks = 0; p_ops = 0; c_blocks = 0; c_ops = 0; #endif } static inline void ?{}( channel(T) &c ){ ((channel(T) &)c){ 0 }; } static inline void ^?{}( channel(T) &c ) with(c) { #ifdef CHAN_STATS printf("Channel %p Blocks: %lu,\t\tOperations: %lu,\t%.2f%% of ops blocked\n", &c, p_blocks + c_blocks, p_ops + c_ops, ((double)p_blocks + c_blocks)/(p_ops + c_ops) * 100); printf("Channel %p Consumer Blocks: %lu,\tConsumer Ops: %lu,\t%.2f%% of Consumer ops blocked\n", &c, p_blocks, p_ops, ((double)p_blocks)/p_ops * 100); printf("Channel %p Producer Blocks: %lu,\tProducer Ops: %lu,\t%.2f%% of Producer ops blocked\n", &c, c_blocks, c_ops, ((double)c_blocks)/c_ops * 100); #endif verifyf( __handle_waituntil_OR( cons ) || __handle_waituntil_OR( prods ) || cons`isEmpty && prods`isEmpty, "Attempted to delete channel with waiting threads (Deadlock).\n" ); if ( size != 0 ) delete( buffer ); } static inline size_t get_count( channel(T) & chan ) with(chan) { return __atomic_load_n( &count, __ATOMIC_RELAXED ); } static inline size_t get_size( channel(T) & chan ) with(chan) { return __atomic_load_n( &size, __ATOMIC_RELAXED ); } static inline bool has_waiters( channel(T) & chan ) with(chan) { return !cons`isEmpty || !prods`isEmpty; } static inline bool has_waiting_consumers( channel(T) & chan ) with(chan) { return !cons`isEmpty; } static inline bool has_waiting_producers( channel(T) & chan ) with(chan) { return !prods`isEmpty; } // closes the channel and notifies all blocked threads static inline void close( channel(T) & chan ) with(chan) { lock( mutex_lock ); closed = true; // flush waiting consumers and producers while ( has_waiting_consumers( chan ) ) { if( !__handle_waituntil_OR( cons ) ) // ensure we only signal special OR case threads when they win the race break; // if __handle_waituntil_OR returns false cons is empty so break cons`first.extra = 0p; wake_one( cons ); } while ( has_waiting_producers( chan ) ) { if( !__handle_waituntil_OR( prods ) ) // ensure we only signal special OR case threads when they win the race break; // if __handle_waituntil_OR returns false prods is empty so break prods`first.extra = 0p; wake_one( prods ); } unlock(mutex_lock); } static inline void is_closed( channel(T) & chan ) with(chan) { return closed; } // used to hand an element to a blocked consumer and signal it static inline void __cons_handoff( channel(T) & chan, T & elem ) with(chan) { memcpy( cons`first.extra, (void *)&elem, sizeof(T) ); // do waiting consumer work __atomic_thread_fence( __ATOMIC_SEQ_CST ); wake_one( cons ); } // used to hand an element to a blocked producer and signal it static inline void __prods_handoff( channel(T) & chan, T & retval ) with(chan) { memcpy( (void *)&retval, prods`first.extra, sizeof(T) ); __atomic_thread_fence( __ATOMIC_SEQ_CST ); wake_one( prods ); } static inline void flush( channel(T) & chan, T elem ) with(chan) { lock( mutex_lock ); while ( count == 0 && !cons`isEmpty ) { __cons_handoff( chan, elem ); } unlock( mutex_lock ); } // handles buffer insert static inline void __buf_insert( channel(T) & chan, T & elem ) with(chan) { memcpy( (void *)&buffer[back], (void *)&elem, sizeof(T) ); count += 1; back++; if ( back == size ) back = 0; } // needed to avoid an extra copy in closed case static inline bool __internal_try_insert( channel(T) & chan, T & elem ) with(chan) { lock( mutex_lock ); #ifdef CHAN_STATS p_ops++; #endif ConsEmpty: if ( !cons`isEmpty ) { if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty; __cons_handoff( chan, elem ); unlock( mutex_lock ); return true; } if ( count == size ) { unlock( mutex_lock ); return false; } __buf_insert( chan, elem ); unlock( mutex_lock ); return true; } // attempts a nonblocking insert // returns true if insert was successful, false otherwise static inline bool try_insert( channel(T) & chan, T elem ) { return __internal_try_insert( chan, elem ); } // handles closed case of insert routine static inline void __closed_insert( channel(T) & chan, T & elem ) with(chan) { channel_closed except{ &channel_closed_vt, &elem, &chan }; throwResume except; // throw closed resumption if ( !__internal_try_insert( chan, elem ) ) throw except; // if try to insert fails (would block), throw termination } static inline void insert( channel(T) & chan, T elem ) with(chan) { // check for close before acquire mx if ( unlikely(closed) ) { __closed_insert( chan, elem ); return; } lock( mutex_lock ); #ifdef CHAN_STATS if ( !closed ) p_ops++; #endif // if closed handle if ( unlikely(closed) ) { unlock( mutex_lock ); __closed_insert( chan, elem ); return; } // buffer count must be zero if cons are blocked (also handles zero-size case) ConsEmpty: if ( !cons`isEmpty ) { if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty; __cons_handoff( chan, elem ); unlock( mutex_lock ); return; } // wait if buffer is full, work will be completed by someone else if ( count == size ) { #ifdef CHAN_STATS p_blocks++; #endif // check for if woken due to close if ( unlikely( block( prods, &elem, mutex_lock ) ) ) __closed_insert( chan, elem ); return; } // if __buf_insert( chan, elem ); unlock( mutex_lock ); } // does the buffer remove and potentially does waiting producer work static inline void __do_remove( channel(T) & chan, T & retval ) with(chan) { memcpy( (void *)&retval, (void *)&buffer[front], sizeof(T) ); count -= 1; front = (front + 1) % size; if (count == size - 1 && !prods`isEmpty ) { if ( !__handle_waituntil_OR( prods ) ) return; __buf_insert( chan, *(T *)prods`first.extra ); // do waiting producer work wake_one( prods ); } } // needed to avoid an extra copy in closed case and single return val case static inline bool __internal_try_remove( channel(T) & chan, T & retval ) with(chan) { lock( mutex_lock ); #ifdef CHAN_STATS c_ops++; #endif ZeroSize: if ( size == 0 && !prods`isEmpty ) { if ( !__handle_waituntil_OR( prods ) ) break ZeroSize; __prods_handoff( chan, retval ); unlock( mutex_lock ); return true; } if ( count == 0 ) { unlock( mutex_lock ); return false; } __do_remove( chan, retval ); unlock( mutex_lock ); return true; } // attempts a nonblocking remove // returns [T, true] if insert was successful // returns [T, false] if insert was successful (T uninit) static inline [T, bool] try_remove( channel(T) & chan ) { T retval; bool success = __internal_try_remove( chan, retval ); return [ retval, success ]; } static inline T try_remove( channel(T) & chan ) { T retval; __internal_try_remove( chan, retval ); return retval; } // handles closed case of insert routine static inline void __closed_remove( channel(T) & chan, T & retval ) with(chan) { channel_closed except{ &channel_closed_vt, 0p, &chan }; throwResume except; // throw resumption if ( !__internal_try_remove( chan, retval ) ) throw except; // if try to remove fails (would block), throw termination } static inline T remove( channel(T) & chan ) with(chan) { T retval; if ( unlikely(closed) ) { __closed_remove( chan, retval ); return retval; } lock( mutex_lock ); #ifdef CHAN_STATS if ( !closed ) c_ops++; #endif if ( unlikely(closed) ) { unlock( mutex_lock ); __closed_remove( chan, retval ); return retval; } // have to check for the zero size channel case ZeroSize: if ( size == 0 && !prods`isEmpty ) { if ( !__handle_waituntil_OR( prods ) ) break ZeroSize; __prods_handoff( chan, retval ); unlock( mutex_lock ); return retval; } // wait if buffer is empty, work will be completed by someone else if ( count == 0 ) { #ifdef CHAN_STATS c_blocks++; #endif // check for if woken due to close if ( unlikely( block( cons, &retval, mutex_lock ) ) ) __closed_remove( chan, retval ); return retval; } // Remove from buffer __do_remove( chan, retval ); unlock( mutex_lock ); return retval; } static inline void remove( channel(T) & chan ) { T elem = (T)remove( chan ); } /////////////////////////////////////////////////////////////////////////////////////////// // The following is Go-style operator support for channels /////////////////////////////////////////////////////////////////////////////////////////// static inline void ?<