// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // thread -- // // Author : Thierry Delisle // Created On : Tue Jan 17 12:27:26 2017 // Last Modified By : Peter A. Buhr // Last Modified On : Wed Dec 4 09:18:14 2019 // Update Count : 6 // #pragma once #include #include "invoke.h" #include "coroutine.hfa" #include "kernel.hfa" #include "monitor.hfa" //----------------------------------------------------------------------------- // thread trait trait is_thread(dtype T) { void ^?{}(T& mutex this); void main(T& this); $thread* get_thread(T& this); }; // define that satisfies the trait without using the thread keyword #define DECL_THREAD(X) $thread* get_thread(X& this) __attribute__((const)) { return &this.__thrd; } void main(X& this) // Inline getters for threads/coroutines/monitors forall( dtype T | is_thread(T) ) static inline $coroutine* get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; } forall( dtype T | is_thread(T) ) static inline $monitor * get_monitor (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; } static inline $coroutine* get_coroutine($thread * this) __attribute__((const)) { return &this->self_cor; } static inline $monitor * get_monitor ($thread * this) __attribute__((const)) { return &this->self_mon; } //----------------------------------------------------------------------------- // forward declarations needed for threads extern struct cluster * mainCluster; forall( dtype T | is_thread(T) ) void __thrd_start( T & this, void (*)(T &) ); //----------------------------------------------------------------------------- // Ctors and dtors void ?{}($thread & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); void ^?{}($thread & this); static inline void ?{}($thread & this) { this{ "Anonymous Thread", *mainCluster, 0p, 65000 }; } static inline void ?{}($thread & this, size_t stackSize ) { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; } static inline void ?{}($thread & this, void * storage, size_t storageSize ) { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } static inline void ?{}($thread & this, struct cluster & cl ) { this{ "Anonymous Thread", cl, 0p, 65000 }; } static inline void ?{}($thread & this, struct cluster & cl, size_t stackSize ) { this{ "Anonymous Thread", cl, 0p, stackSize }; } static inline void ?{}($thread & this, struct cluster & cl, void * storage, size_t storageSize ) { this{ "Anonymous Thread", cl, storage, storageSize }; } static inline void ?{}($thread & this, const char * const name) { this{ name, *mainCluster, 0p, 65000 }; } static inline void ?{}($thread & this, const char * const name, struct cluster & cl ) { this{ name, cl, 0p, 65000 }; } static inline void ?{}($thread & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; } //----------------------------------------------------------------------------- // thread runner // Structure that actually start and stop threads forall( dtype T | sized(T) | is_thread(T) ) struct scoped { T handle; }; forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) void ?{}( scoped(T)& this ); forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) void ?{}( scoped(T)& this, P params ); forall( dtype T | sized(T) | is_thread(T) ) void ^?{}( scoped(T)& this ); //----------------------------------------------------------------------------- // Thread getters static inline struct $thread * active_thread () { return TL_GET( this_thread ); } //----------------------------------------------------------------------------- // Scheduler API //---------- // Park thread: block until corresponding call to unpark, won't block if unpark is already called void park( __cfaabi_dbg_ctx_param ); //---------- // Unpark a thread, if the thread is already blocked, schedule it // if the thread is not yet block, signal that it should rerun immediately void unpark( $thread * this __cfaabi_dbg_ctx_param2 ); forall( dtype T | is_thread(T) ) static inline void unpark( T & this __cfaabi_dbg_ctx_param2 ) { if(!&this) return; unpark( get_thread( this ) __cfaabi_dbg_ctx_fwd2 );} //---------- // Yield: force thread to block and be rescheduled bool force_yield( enum __Preemption_Reason ); static inline void yield() { force_yield(__MANUAL_PREEMPTION); } // Yield: yield N times static inline void yield( unsigned times ) { for( times ) { yield(); } } //---------- // sleep: force thread to block and be rescheduled after Duration duration void sleep( Duration duration ); // Local Variables: // // mode: c // // tab-width: 4 // // End: //