| [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
|
|---|
| [20cf96d] | 12 | // Last Modified On : Tue Nov 22 22:18:34 2022
|
|---|
| 13 | // Update Count : 35
|
|---|
| [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"
|
|---|
| [d2ad151] | 25 | #include "bits/random.hfa"
|
|---|
| [8118303] | 26 |
|
|---|
| 27 | //-----------------------------------------------------------------------------
|
|---|
| [de6319f] | 28 | // thread trait
|
|---|
| [fd54fef] | 29 | trait is_thread(T &) {
|
|---|
| [ab8c6a6] | 30 | void ^?{}(T& mutex this);
|
|---|
| 31 | void main(T& this);
|
|---|
| [e84ab3d] | 32 | thread$ * get_thread(T& this);
|
|---|
| [8118303] | 33 | };
|
|---|
| 34 |
|
|---|
| [c715e5f] | 35 | forall(thread_t &)
|
|---|
| 36 | exception ThreadCancelled {
|
|---|
| [ab8c6a6] | 37 | thread_t * the_thread;
|
|---|
| 38 | exception_t * the_exception;
|
|---|
| [c715e5f] | 39 | };
|
|---|
| [ab8c6a6] | 40 |
|
|---|
| [fd54fef] | 41 | forall(T &)
|
|---|
| [ab8c6a6] | 42 | void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src);
|
|---|
| 43 |
|
|---|
| [fd54fef] | 44 | forall(T &)
|
|---|
| [ab8c6a6] | 45 | const char * msg(ThreadCancelled(T) *);
|
|---|
| 46 |
|
|---|
| [8c50aed] | 47 | // Inline getters for threads/coroutines/monitors
|
|---|
| [fd54fef] | 48 | forall( T & | is_thread(T) )
|
|---|
| [e84ab3d] | 49 | static inline coroutine$ * get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; }
|
|---|
| [8118303] | 50 |
|
|---|
| [fd54fef] | 51 | forall( T & | is_thread(T) )
|
|---|
| [e84ab3d] | 52 | static inline monitor$ * get_monitor (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; }
|
|---|
| [cb0e6de] | 53 |
|
|---|
| [e84ab3d] | 54 | static inline coroutine$ * get_coroutine(thread$ * this) __attribute__((const)) { return &this->self_cor; }
|
|---|
| 55 | static inline monitor$ * get_monitor (thread$ * this) __attribute__((const)) { return &this->self_mon; }
|
|---|
| [cb0e6de] | 56 |
|
|---|
| [8c50aed] | 57 | //-----------------------------------------------------------------------------
|
|---|
| 58 | // forward declarations needed for threads
|
|---|
| [de6319f] | 59 | extern struct cluster * mainCluster;
|
|---|
| [bd98b58] | 60 |
|
|---|
| [fd54fef] | 61 | forall( T & | is_thread(T) )
|
|---|
| [09f357ec] | 62 | void __thrd_start( T & this, void (*)(T &) );
|
|---|
| [bd4d011] | 63 |
|
|---|
| [8118303] | 64 | //-----------------------------------------------------------------------------
|
|---|
| 65 | // Ctors and dtors
|
|---|
| [e84ab3d] | 66 | void ?{}(thread$ & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize );
|
|---|
| 67 | void ^?{}(thread$ & this);
|
|---|
| 68 |
|
|---|
| [eaf269d] | 69 | static inline void ?{}(thread$ & this) { this{ "Anonymous Thread", *mainCluster, 0p, DEFAULT_STACK_SIZE }; }
|
|---|
| [e84ab3d] | 70 | static inline void ?{}(thread$ & this, size_t stackSize ) { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; }
|
|---|
| 71 | static inline void ?{}(thread$ & this, void * storage, size_t storageSize ) { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; }
|
|---|
| [eaf269d] | 72 | static inline void ?{}(thread$ & this, struct cluster & cl ) { this{ "Anonymous Thread", cl, 0p, DEFAULT_STACK_SIZE }; }
|
|---|
| [e84ab3d] | 73 | static inline void ?{}(thread$ & this, struct cluster & cl, size_t stackSize ) { this{ "Anonymous Thread", cl, 0p, stackSize }; }
|
|---|
| 74 | static inline void ?{}(thread$ & this, struct cluster & cl, void * storage, size_t storageSize ) { this{ "Anonymous Thread", cl, storage, storageSize }; }
|
|---|
| [eaf269d] | 75 | static inline void ?{}(thread$ & this, const char * const name) { this{ name, *mainCluster, 0p, DEFAULT_STACK_SIZE }; }
|
|---|
| 76 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl ) { this{ name, cl, 0p, DEFAULT_STACK_SIZE }; }
|
|---|
| [e84ab3d] | 77 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; }
|
|---|
| [8118303] | 78 |
|
|---|
| [ab8c6a6] | 79 | struct thread_dtor_guard_t {
|
|---|
| 80 | monitor_dtor_guard_t mg;
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| [c3b9d639] | 83 | forall( T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled(T))
|
|---|
| 84 | | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); })
|
|---|
| [8edbe40] | 85 | void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) );
|
|---|
| [ab8c6a6] | 86 | void ^?{}( thread_dtor_guard_t & this );
|
|---|
| 87 |
|
|---|
| [8118303] | 88 | //-----------------------------------------------------------------------------
|
|---|
| 89 | // thread runner
|
|---|
| 90 | // Structure that actually start and stop threads
|
|---|
| [fd54fef] | 91 | forall( T & | sized(T) | is_thread(T) )
|
|---|
| [e15df4c] | 92 | struct scoped {
|
|---|
| [8118303] | 93 | T handle;
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| [fd54fef] | 96 | forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
|
|---|
| [242a902] | 97 | void ?{}( scoped(T)& this );
|
|---|
| [8118303] | 98 |
|
|---|
| [fd54fef] | 99 | forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
|
|---|
| [242a902] | 100 | void ?{}( scoped(T)& this, P params );
|
|---|
| [8118303] | 101 |
|
|---|
| [fd54fef] | 102 | forall( 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
|
|---|
| [e84ab3d] | 115 | void unpark( thread$ * this );
|
|---|
| [3381ed7] | 116 |
|
|---|
| [fd54fef] | 117 | forall( 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
|
|---|
| [c3b9d639] | 130 | forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled(T))
|
|---|
| 131 | | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); })
|
|---|
| [77a2994] | 132 | T & join( T & this );
|
|---|
| 133 |
|
|---|
| [a167c70c] | 134 | //----------
|
|---|
| 135 | // misc
|
|---|
| 136 | bool migrate( thread$ * thrd, struct cluster & cl );
|
|---|
| 137 |
|
|---|
| 138 | forall( T & | is_thread(T) )
|
|---|
| 139 | static inline bool migrate( T & mutex thrd, struct cluster & cl ) { return migrate( &(thread&)thrd, cl ); }
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| [12b5e94a] | 142 | //----------
|
|---|
| [c655650] | 143 | // prng
|
|---|
| [20cf96d] | 144 | void set_seed( size_t seed );
|
|---|
| [12b5e94a] | 145 | static inline {
|
|---|
| [20cf96d] | 146 | size_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return PRNG_NAME( th.random_state ); } // [0,UINT_MAX]
|
|---|
| 147 | size_t prng( thread$ & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
|
|---|
| 148 | size_t prng( thread$ & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
|
|---|
| [c655650] | 149 | forall( T & | is_thread(T) ) {
|
|---|
| [20cf96d] | 150 | size_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX]
|
|---|
| 151 | size_t prng( T & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
|
|---|
| 152 | size_t prng( T & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
|
|---|
| [c655650] | 153 | } // distribution
|
|---|
| 154 | } // distribution
|
|---|
| [12b5e94a] | 155 |
|
|---|
| [78b3f52] | 156 | // Local Variables: //
|
|---|
| 157 | // mode: c //
|
|---|
| 158 | // tab-width: 4 //
|
|---|
| 159 | // End: //
|
|---|