// // 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 2023 // Last Modified By : // Last Modified On : // Update Count : // #pragma once #include "containers/list.hfa" #include "alarm.hfa" #include "kernel.hfa" #include "time.hfa" struct select_node; // node status static const unsigned long int __SELECT_UNSAT = 0; static const unsigned long int __SELECT_PENDING = 1; // used only by special OR case static const unsigned long int __SELECT_SAT = 2; static const unsigned long int __SELECT_RUN = 3; // these are used inside the compiler to aid in code generation static inline bool __CFA_has_clause_run( unsigned long int status ) { return status == __SELECT_RUN; } static inline void __CFA_maybe_park( int * park_counter ) { if ( __atomic_sub_fetch( park_counter, 1, __ATOMIC_SEQ_CST) < 0 ) park(); } // node used for coordinating waituntil synchronization struct select_node { int * park_counter; // If this is 0p then the node is in a special OR case waituntil unsigned long int * clause_status; // needs to point at ptr sized location, if this is 0p then node is not part of a waituntil void * extra; // used to store arbitrary data needed by some primitives thread$ * blocked_thread; inline dlink(select_node); }; P9_EMBEDDED( select_node, dlink(select_node) ) static inline void ?{}( select_node & this ) { this.blocked_thread = active_thread(); this.clause_status = 0p; this.park_counter = 0p; this.extra = 0p; } static inline void ?{}( select_node & this, thread$ * blocked_thread ) { this.blocked_thread = blocked_thread; this.clause_status = 0p; this.park_counter = 0p; this.extra = 0p; } static inline void ?{}( select_node & this, thread$ * blocked_thread, void * extra ) { this.blocked_thread = blocked_thread; this.clause_status = 0p; this.park_counter = 0p; this.extra = extra; } static inline void ^?{}( select_node & this ) {} // this is used inside the compiler to aid in code generation static inline unsigned long int * __get_clause_status( select_node & s ) { return s.clause_status; } // this is used inside the compiler to attempt to establish an else clause as a winner in the OR special case race static inline bool __select_node_else_race( select_node & this ) with( this ) { unsigned long int cmp_status = __SELECT_UNSAT; return *clause_status == 0 && __atomic_compare_exchange_n( clause_status, &cmp_status, __SELECT_SAT, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); } //----------------------------------------------------------------------------- // is_selectable forall(T & | sized(T)) trait is_selectable { // For registering a select stmt on a selectable concurrency primitive // Returns bool that indicates if operation is already SAT bool register_select( T &, select_node & ); // For unregistering a select stmt on a selectable concurrency primitive // If true is returned then the corresponding code block is run (only in non-special OR case and only if node status is not RUN) bool unregister_select( T &, select_node & ); // This routine is run on the selecting thread prior to executing the statement corresponding to the select_node // passed as an arg to this routine. If true is returned proceed as normal, if false is returned the statement is skipped bool on_selected( T &, select_node & ); }; // Used inside the compiler to allow for overloading on return type for operations such as '?<