| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // thread -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Tue Jan 17 12:27:26 2017 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Feb 11 16:34:07 2022 | 
|---|
| 13 | // Update Count     : 20 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <assert.h> | 
|---|
| 19 | #include "invoke.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "coroutine.hfa" | 
|---|
| 22 | #include "kernel.hfa" | 
|---|
| 23 | #include "monitor.hfa" | 
|---|
| 24 | #include "exception.hfa" | 
|---|
| 25 |  | 
|---|
| 26 | //----------------------------------------------------------------------------- | 
|---|
| 27 | // thread trait | 
|---|
| 28 | trait is_thread(T &) { | 
|---|
| 29 | void ^?{}(T& mutex this); | 
|---|
| 30 | void main(T& this); | 
|---|
| 31 | thread$ * get_thread(T& this); | 
|---|
| 32 | }; | 
|---|
| 33 |  | 
|---|
| 34 | forall(thread_t &) | 
|---|
| 35 | exception ThreadCancelled { | 
|---|
| 36 | thread_t * the_thread; | 
|---|
| 37 | exception_t * the_exception; | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | forall(T &) | 
|---|
| 41 | void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src); | 
|---|
| 42 |  | 
|---|
| 43 | forall(T &) | 
|---|
| 44 | const char * msg(ThreadCancelled(T) *); | 
|---|
| 45 |  | 
|---|
| 46 | // Inline getters for threads/coroutines/monitors | 
|---|
| 47 | forall( T & | is_thread(T) ) | 
|---|
| 48 | static inline coroutine$ * get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; } | 
|---|
| 49 |  | 
|---|
| 50 | forall( T & | is_thread(T) ) | 
|---|
| 51 | static inline monitor$   * get_monitor  (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; } | 
|---|
| 52 |  | 
|---|
| 53 | static inline coroutine$ * get_coroutine(thread$ * this) __attribute__((const)) { return &this->self_cor; } | 
|---|
| 54 | static inline monitor$   * get_monitor  (thread$ * this) __attribute__((const)) { return &this->self_mon; } | 
|---|
| 55 |  | 
|---|
| 56 | //----------------------------------------------------------------------------- | 
|---|
| 57 | // forward declarations needed for threads | 
|---|
| 58 | extern struct cluster * mainCluster; | 
|---|
| 59 |  | 
|---|
| 60 | forall( T & | is_thread(T) ) | 
|---|
| 61 | void __thrd_start( T & this, void (*)(T &) ); | 
|---|
| 62 |  | 
|---|
| 63 | //----------------------------------------------------------------------------- | 
|---|
| 64 | // Ctors and dtors | 
|---|
| 65 | void ?{}(thread$ & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); | 
|---|
| 66 | void ^?{}(thread$ & this); | 
|---|
| 67 |  | 
|---|
| 68 | static inline void ?{}(thread$ & this)                                                                  { this{ "Anonymous Thread", *mainCluster, 0p, DEFAULT_STACK_SIZE }; } | 
|---|
| 69 | static inline void ?{}(thread$ & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; } | 
|---|
| 70 | static inline void ?{}(thread$ & this, void * storage, size_t storageSize )                             { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } | 
|---|
| 71 | static inline void ?{}(thread$ & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, 0p, DEFAULT_STACK_SIZE }; } | 
|---|
| 72 | static inline void ?{}(thread$ & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, 0p, stackSize }; } | 
|---|
| 73 | static inline void ?{}(thread$ & this, struct cluster & cl, void * storage, size_t storageSize )        { this{ "Anonymous Thread", cl, storage, storageSize }; } | 
|---|
| 74 | static inline void ?{}(thread$ & this, const char * const name)                                         { this{ name, *mainCluster, 0p, DEFAULT_STACK_SIZE }; } | 
|---|
| 75 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl )                   { this{ name, cl, 0p, DEFAULT_STACK_SIZE }; } | 
|---|
| 76 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; } | 
|---|
| 77 |  | 
|---|
| 78 | struct thread_dtor_guard_t { | 
|---|
| 79 | monitor_dtor_guard_t mg; | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 | forall( T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled(T)) | 
|---|
| 83 | | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); }) | 
|---|
| 84 | void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) ); | 
|---|
| 85 | void ^?{}( thread_dtor_guard_t & this ); | 
|---|
| 86 |  | 
|---|
| 87 | //----------------------------------------------------------------------------- | 
|---|
| 88 | // thread runner | 
|---|
| 89 | // Structure that actually start and stop threads | 
|---|
| 90 | forall( T & | sized(T) | is_thread(T) ) | 
|---|
| 91 | struct scoped { | 
|---|
| 92 | T handle; | 
|---|
| 93 | }; | 
|---|
| 94 |  | 
|---|
| 95 | forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } ) | 
|---|
| 96 | void ?{}( scoped(T)& this ); | 
|---|
| 97 |  | 
|---|
| 98 | forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) | 
|---|
| 99 | void ?{}( scoped(T)& this, P params ); | 
|---|
| 100 |  | 
|---|
| 101 | forall( T & | sized(T) | is_thread(T) ) | 
|---|
| 102 | void ^?{}( scoped(T)& this ); | 
|---|
| 103 |  | 
|---|
| 104 | //----------------------------------------------------------------------------- | 
|---|
| 105 | // Scheduler API | 
|---|
| 106 |  | 
|---|
| 107 | //---------- | 
|---|
| 108 | // Park thread: block until corresponding call to unpark, won't block if unpark is already called | 
|---|
| 109 | void park( void ); | 
|---|
| 110 |  | 
|---|
| 111 | //---------- | 
|---|
| 112 | // Unpark a thread, if the thread is already blocked, schedule it | 
|---|
| 113 | //                  if the thread is not yet block, signal that it should rerun immediately | 
|---|
| 114 | void unpark( thread$ * this ); | 
|---|
| 115 |  | 
|---|
| 116 | forall( T & | is_thread(T) ) | 
|---|
| 117 | static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );} | 
|---|
| 118 |  | 
|---|
| 119 | //---------- | 
|---|
| 120 | // Yield: force thread to block and be rescheduled | 
|---|
| 121 | bool force_yield( enum __Preemption_Reason ); | 
|---|
| 122 |  | 
|---|
| 123 | //---------- | 
|---|
| 124 | // sleep: force thread to block and be rescheduled after Duration duration | 
|---|
| 125 | void sleep( Duration duration ); | 
|---|
| 126 |  | 
|---|
| 127 | //---------- | 
|---|
| 128 | // join | 
|---|
| 129 | forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled(T)) | 
|---|
| 130 | | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); }) | 
|---|
| 131 | T & join( T & this ); | 
|---|
| 132 |  | 
|---|
| 133 | //---------- | 
|---|
| 134 | // prng | 
|---|
| 135 | static inline { | 
|---|
| 136 | uint32_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return LCG( th.random_state ); } // [0,UINT_MAX] | 
|---|
| 137 | uint32_t prng( thread$ & th, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u) | 
|---|
| 138 | uint32_t prng( thread$ & th, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u] | 
|---|
| 139 | forall( T & | is_thread(T) ) { | 
|---|
| 140 | uint32_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX] | 
|---|
| 141 | uint32_t prng( T & th, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u) | 
|---|
| 142 | uint32_t prng( T & th, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u] | 
|---|
| 143 | } // distribution | 
|---|
| 144 | } // distribution | 
|---|
| 145 |  | 
|---|
| 146 | // Local Variables: // | 
|---|
| 147 | // mode: c // | 
|---|
| 148 | // tab-width: 4 // | 
|---|
| 149 | // End: // | 
|---|