[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" |
---|
[ab8c6a6] | 24 | #include "exception.hfa" |
---|
[8118303] | 25 | |
---|
| 26 | //----------------------------------------------------------------------------- |
---|
[de6319f] | 27 | // thread trait |
---|
[0c92c9f] | 28 | trait is_thread(dtype T) { |
---|
[ab8c6a6] | 29 | void ^?{}(T& mutex this); |
---|
| 30 | void main(T& this); |
---|
| 31 | $thread* get_thread(T& this); |
---|
[8118303] | 32 | }; |
---|
| 33 | |
---|
[ab8c6a6] | 34 | FORALL_DATA_EXCEPTION(ThreadCancelled, (dtype thread_t), (thread_t)) ( |
---|
| 35 | thread_t * the_thread; |
---|
| 36 | exception_t * the_exception; |
---|
| 37 | ); |
---|
| 38 | |
---|
| 39 | forall(dtype T) |
---|
| 40 | void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src); |
---|
| 41 | |
---|
| 42 | forall(dtype T) |
---|
| 43 | const char * msg(ThreadCancelled(T) *); |
---|
| 44 | |
---|
[8c50aed] | 45 | // define that satisfies the trait without using the thread keyword |
---|
[ac2b598] | 46 | #define DECL_THREAD(X) $thread* get_thread(X& this) __attribute__((const)) { return &this.__thrd; } void main(X& this) |
---|
[8f49a54] | 47 | |
---|
[8c50aed] | 48 | // Inline getters for threads/coroutines/monitors |
---|
[0c92c9f] | 49 | forall( dtype T | is_thread(T) ) |
---|
[ac2b598] | 50 | static inline $coroutine* get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; } |
---|
[8118303] | 51 | |
---|
[cb0e6de] | 52 | forall( dtype T | is_thread(T) ) |
---|
[ac2b598] | 53 | static inline $monitor * get_monitor (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; } |
---|
[cb0e6de] | 54 | |
---|
[ac2b598] | 55 | static inline $coroutine* get_coroutine($thread * this) __attribute__((const)) { return &this->self_cor; } |
---|
| 56 | static inline $monitor * get_monitor ($thread * this) __attribute__((const)) { return &this->self_mon; } |
---|
[cb0e6de] | 57 | |
---|
[8c50aed] | 58 | //----------------------------------------------------------------------------- |
---|
| 59 | // forward declarations needed for threads |
---|
[de6319f] | 60 | extern struct cluster * mainCluster; |
---|
[bd98b58] | 61 | |
---|
[bd4d011] | 62 | forall( dtype T | is_thread(T) ) |
---|
[09f357ec] | 63 | void __thrd_start( T & this, void (*)(T &) ); |
---|
[bd4d011] | 64 | |
---|
[8118303] | 65 | //----------------------------------------------------------------------------- |
---|
| 66 | // Ctors and dtors |
---|
[ac2b598] | 67 | void ?{}($thread & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); |
---|
| 68 | void ^?{}($thread & this); |
---|
| 69 | |
---|
| 70 | static inline void ?{}($thread & this) { this{ "Anonymous Thread", *mainCluster, 0p, 65000 }; } |
---|
| 71 | static inline void ?{}($thread & this, size_t stackSize ) { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; } |
---|
| 72 | static inline void ?{}($thread & this, void * storage, size_t storageSize ) { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } |
---|
| 73 | static inline void ?{}($thread & this, struct cluster & cl ) { this{ "Anonymous Thread", cl, 0p, 65000 }; } |
---|
| 74 | static inline void ?{}($thread & this, struct cluster & cl, size_t stackSize ) { this{ "Anonymous Thread", cl, 0p, stackSize }; } |
---|
| 75 | static inline void ?{}($thread & this, struct cluster & cl, void * storage, size_t storageSize ) { this{ "Anonymous Thread", cl, storage, storageSize }; } |
---|
| 76 | static inline void ?{}($thread & this, const char * const name) { this{ name, *mainCluster, 0p, 65000 }; } |
---|
| 77 | static inline void ?{}($thread & this, const char * const name, struct cluster & cl ) { this{ name, cl, 0p, 65000 }; } |
---|
| 78 | static inline void ?{}($thread & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; } |
---|
[8118303] | 79 | |
---|
[ab8c6a6] | 80 | struct thread_dtor_guard_t { |
---|
| 81 | monitor_dtor_guard_t mg; |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | forall( dtype T | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)) ) |
---|
| 85 | void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) ); |
---|
| 86 | void ^?{}( thread_dtor_guard_t & this ); |
---|
| 87 | |
---|
[8118303] | 88 | //----------------------------------------------------------------------------- |
---|
| 89 | // thread runner |
---|
| 90 | // Structure that actually start and stop threads |
---|
[8def349] | 91 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[e15df4c] | 92 | struct scoped { |
---|
[8118303] | 93 | T handle; |
---|
| 94 | }; |
---|
| 95 | |
---|
[242a902] | 96 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
| 97 | void ?{}( scoped(T)& this ); |
---|
[8118303] | 98 | |
---|
[242a902] | 99 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
| 100 | void ?{}( scoped(T)& this, P params ); |
---|
[8118303] | 101 | |
---|
[9f1695b] | 102 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[242a902] | 103 | void ^?{}( scoped(T)& this ); |
---|
[8118303] | 104 | |
---|
[3381ed7] | 105 | //----------------------------------------------------------------------------- |
---|
| 106 | // Scheduler API |
---|
| 107 | |
---|
| 108 | //---------- |
---|
| 109 | // Park thread: block until corresponding call to unpark, won't block if unpark is already called |
---|
[e235429] | 110 | void park( void ); |
---|
[3381ed7] | 111 | |
---|
| 112 | //---------- |
---|
| 113 | // Unpark a thread, if the thread is already blocked, schedule it |
---|
[b0c7419] | 114 | // if the thread is not yet block, signal that it should rerun immediately |
---|
[e235429] | 115 | void unpark( $thread * this ); |
---|
[3381ed7] | 116 | |
---|
| 117 | forall( dtype T | is_thread(T) ) |
---|
[e235429] | 118 | static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );} |
---|
[3381ed7] | 119 | |
---|
| 120 | //---------- |
---|
| 121 | // Yield: force thread to block and be rescheduled |
---|
[b0c7419] | 122 | bool force_yield( enum __Preemption_Reason ); |
---|
| 123 | |
---|
[2d8f7b0] | 124 | //---------- |
---|
| 125 | // sleep: force thread to block and be rescheduled after Duration duration |
---|
| 126 | void sleep( Duration duration ); |
---|
| 127 | |
---|
[77a2994] | 128 | //---------- |
---|
| 129 | // join |
---|
[ab8c6a6] | 130 | forall( dtype T | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)) ) |
---|
[77a2994] | 131 | T & join( T & this ); |
---|
| 132 | |
---|
[78b3f52] | 133 | // Local Variables: // |
---|
| 134 | // mode: c // |
---|
| 135 | // tab-width: 4 // |
---|
| 136 | // End: // |
---|