| [0e76cf4f] | 1 | // | 
|---|
| [78b3f52] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
| [0e76cf4f] | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [75a17f1] | 7 | // thread -- | 
|---|
| [0e76cf4f] | 8 | // | 
|---|
| [78b3f52] | 9 | // Author           : Thierry Delisle | 
|---|
| [f07e037] | 10 | // Created On       : Tue Jan 17 12:27:26 2017 | 
|---|
| [91c389a] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [121be3e] | 12 | // Last Modified On : Wed Dec  4 09:18:14 2019 | 
|---|
|  | 13 | // Update Count     : 6 | 
|---|
| [0e76cf4f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [6b0b624] | 16 | #pragma once | 
|---|
| [0e76cf4f] | 17 |  | 
|---|
| [91c389a] | 18 | #include <assert.h> | 
|---|
| [8118303] | 19 | #include "invoke.h" | 
|---|
| [78b3f52] | 20 |  | 
|---|
| [58b6d1b] | 21 | #include "coroutine.hfa" | 
|---|
|  | 22 | #include "kernel.hfa" | 
|---|
|  | 23 | #include "monitor.hfa" | 
|---|
| [8118303] | 24 |  | 
|---|
|  | 25 | //----------------------------------------------------------------------------- | 
|---|
| [de6319f] | 26 | // thread trait | 
|---|
| [0c92c9f] | 27 | trait is_thread(dtype T) { | 
|---|
| [242a902] | 28 | void ^?{}(T& mutex this); | 
|---|
| [83a071f9] | 29 | void main(T& this); | 
|---|
| [ac2b598] | 30 | $thread* get_thread(T& this); | 
|---|
| [8118303] | 31 | }; | 
|---|
|  | 32 |  | 
|---|
| [8c50aed] | 33 | // define that satisfies the trait without using the thread keyword | 
|---|
| [ac2b598] | 34 | #define DECL_THREAD(X) $thread* get_thread(X& this) __attribute__((const)) { return &this.__thrd; } void main(X& this) | 
|---|
| [8f49a54] | 35 |  | 
|---|
| [8c50aed] | 36 | // Inline getters for threads/coroutines/monitors | 
|---|
| [0c92c9f] | 37 | forall( dtype T | is_thread(T) ) | 
|---|
| [ac2b598] | 38 | static inline $coroutine* get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; } | 
|---|
| [8118303] | 39 |  | 
|---|
| [cb0e6de] | 40 | forall( dtype T | is_thread(T) ) | 
|---|
| [ac2b598] | 41 | static inline $monitor  * get_monitor  (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; } | 
|---|
| [cb0e6de] | 42 |  | 
|---|
| [ac2b598] | 43 | static inline $coroutine* get_coroutine($thread * this) __attribute__((const)) { return &this->self_cor; } | 
|---|
|  | 44 | static inline $monitor  * get_monitor  ($thread * this) __attribute__((const)) { return &this->self_mon; } | 
|---|
| [cb0e6de] | 45 |  | 
|---|
| [8c50aed] | 46 | //----------------------------------------------------------------------------- | 
|---|
|  | 47 | // forward declarations needed for threads | 
|---|
| [de6319f] | 48 | extern struct cluster * mainCluster; | 
|---|
| [bd98b58] | 49 |  | 
|---|
| [bd4d011] | 50 | forall( dtype T | is_thread(T) ) | 
|---|
| [09f357ec] | 51 | void __thrd_start( T & this, void (*)(T &) ); | 
|---|
| [bd4d011] | 52 |  | 
|---|
| [8118303] | 53 | //----------------------------------------------------------------------------- | 
|---|
|  | 54 | // Ctors and dtors | 
|---|
| [ac2b598] | 55 | void ?{}($thread & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); | 
|---|
|  | 56 | void ^?{}($thread & this); | 
|---|
|  | 57 |  | 
|---|
|  | 58 | static inline void ?{}($thread & this)                                                                  { this{ "Anonymous Thread", *mainCluster, 0p, 65000 }; } | 
|---|
|  | 59 | static inline void ?{}($thread & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; } | 
|---|
|  | 60 | static inline void ?{}($thread & this, void * storage, size_t storageSize )                             { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } | 
|---|
|  | 61 | static inline void ?{}($thread & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, 0p, 65000 }; } | 
|---|
|  | 62 | static inline void ?{}($thread & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, 0p, stackSize }; } | 
|---|
|  | 63 | static inline void ?{}($thread & this, struct cluster & cl, void * storage, size_t storageSize )        { this{ "Anonymous Thread", cl, storage, storageSize }; } | 
|---|
|  | 64 | static inline void ?{}($thread & this, const char * const name)                                         { this{ name, *mainCluster, 0p, 65000 }; } | 
|---|
|  | 65 | static inline void ?{}($thread & this, const char * const name, struct cluster & cl )                   { this{ name, cl, 0p, 65000 }; } | 
|---|
|  | 66 | static inline void ?{}($thread & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; } | 
|---|
| [8118303] | 67 |  | 
|---|
|  | 68 | //----------------------------------------------------------------------------- | 
|---|
|  | 69 | // thread runner | 
|---|
|  | 70 | // Structure that actually start and stop threads | 
|---|
| [8def349] | 71 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| [e15df4c] | 72 | struct scoped { | 
|---|
| [8118303] | 73 | T handle; | 
|---|
|  | 74 | }; | 
|---|
|  | 75 |  | 
|---|
| [242a902] | 76 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) | 
|---|
|  | 77 | void ?{}( scoped(T)& this ); | 
|---|
| [8118303] | 78 |  | 
|---|
| [242a902] | 79 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) | 
|---|
|  | 80 | void ?{}( scoped(T)& this, P params ); | 
|---|
| [8118303] | 81 |  | 
|---|
| [9f1695b] | 82 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| [242a902] | 83 | void ^?{}( scoped(T)& this ); | 
|---|
| [8118303] | 84 |  | 
|---|
| [3381ed7] | 85 | //----------------------------------------------------------------------------- | 
|---|
|  | 86 | // Thread getters | 
|---|
| [ac2b598] | 87 | static inline struct $thread * active_thread () { return TL_GET( this_thread ); } | 
|---|
| [d4e68a6] | 88 |  | 
|---|
| [3381ed7] | 89 | //----------------------------------------------------------------------------- | 
|---|
|  | 90 | // Scheduler API | 
|---|
|  | 91 |  | 
|---|
|  | 92 | //---------- | 
|---|
|  | 93 | // Park thread: block until corresponding call to unpark, won't block if unpark is already called | 
|---|
| [ae66348] | 94 | void park( __cfaabi_dbg_ctx_param ); | 
|---|
| [3381ed7] | 95 |  | 
|---|
|  | 96 | //---------- | 
|---|
|  | 97 | // Unpark a thread, if the thread is already blocked, schedule it | 
|---|
| [b0c7419] | 98 | //                  if the thread is not yet block, signal that it should rerun immediately | 
|---|
| [ae66348] | 99 | void unpark( $thread * this __cfaabi_dbg_ctx_param2 ); | 
|---|
| [3381ed7] | 100 |  | 
|---|
|  | 101 | forall( dtype T | is_thread(T) ) | 
|---|
| [ae66348] | 102 | static inline void unpark( T & this __cfaabi_dbg_ctx_param2 ) { if(!&this) return; unpark( get_thread( this ) __cfaabi_dbg_ctx_fwd2 );} | 
|---|
| [3381ed7] | 103 |  | 
|---|
|  | 104 | //---------- | 
|---|
|  | 105 | // Yield: force thread to block and be rescheduled | 
|---|
| [b0c7419] | 106 | bool force_yield( enum __Preemption_Reason ); | 
|---|
|  | 107 |  | 
|---|
| [3381ed7] | 108 | static inline void yield() { | 
|---|
| [b0c7419] | 109 | force_yield(__MANUAL_PREEMPTION); | 
|---|
| [3381ed7] | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | // Yield: yield N times | 
|---|
|  | 113 | static inline void yield( unsigned times ) { | 
|---|
|  | 114 | for( times ) { | 
|---|
|  | 115 | yield(); | 
|---|
|  | 116 | } | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
| [2d8f7b0] | 119 | //---------- | 
|---|
|  | 120 | // sleep: force thread to block and be rescheduled after Duration duration | 
|---|
|  | 121 | void sleep( Duration duration ); | 
|---|
|  | 122 |  | 
|---|
| [78b3f52] | 123 | // Local Variables: // | 
|---|
|  | 124 | // mode: c // | 
|---|
|  | 125 | // tab-width: 4 // | 
|---|
|  | 126 | // End: // | 
|---|