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 : Sun Sep 3 07:34:43 2023 |
---|
13 | // Update Count : 47 |
---|
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 | #include "bits/random.hfa" |
---|
26 | |
---|
27 | //----------------------------------------------------------------------------- |
---|
28 | // thread trait |
---|
29 | forall( T & ) |
---|
30 | trait is_basic_thread { |
---|
31 | void main(T& this); |
---|
32 | thread$ * get_thread(T& this); |
---|
33 | }; |
---|
34 | forall( T & | is_basic_thread(T) ) |
---|
35 | trait is_thread { |
---|
36 | void ^?{}(T& mutex this); |
---|
37 | }; |
---|
38 | |
---|
39 | forall(thread_t &) |
---|
40 | exception ThreadCancelled { |
---|
41 | thread_t * the_thread; |
---|
42 | exception_t * the_exception; |
---|
43 | }; |
---|
44 | |
---|
45 | forall(T &) |
---|
46 | void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src); |
---|
47 | |
---|
48 | forall(T &) |
---|
49 | const char * msg(ThreadCancelled(T) *); |
---|
50 | |
---|
51 | // Inline getters for threads/coroutines/monitors |
---|
52 | forall( T & | is_basic_thread(T) ) |
---|
53 | static inline coroutine$ * get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; } |
---|
54 | |
---|
55 | forall( T & | is_basic_thread(T) ) |
---|
56 | static inline monitor$ * get_monitor (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; } |
---|
57 | |
---|
58 | static inline coroutine$ * get_coroutine(thread$ * this) __attribute__((const)) { return &this->self_cor; } |
---|
59 | static inline monitor$ * get_monitor (thread$ * this) __attribute__((const)) { return &this->self_mon; } |
---|
60 | |
---|
61 | //----------------------------------------------------------------------------- |
---|
62 | // forward declarations needed for threads |
---|
63 | extern struct cluster * mainCluster; |
---|
64 | |
---|
65 | forall( T & | is_basic_thread(T) ) |
---|
66 | void __thrd_start( T & this, void (*)(T &) ); |
---|
67 | |
---|
68 | //----------------------------------------------------------------------------- |
---|
69 | // Ctors and dtors |
---|
70 | void ?{}(thread$ & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); |
---|
71 | void ^?{}(thread$ & this); |
---|
72 | |
---|
73 | static inline void ?{}(thread$ & this) { this{ "Anonymous Thread", *mainCluster, 0p, DEFAULT_STACK_SIZE }; } |
---|
74 | static inline void ?{}(thread$ & this, size_t stackSize ) { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; } |
---|
75 | static inline void ?{}(thread$ & this, void * storage, size_t storageSize ) { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } |
---|
76 | static inline void ?{}(thread$ & this, struct cluster & cl ) { this{ "Anonymous Thread", cl, 0p, DEFAULT_STACK_SIZE }; } |
---|
77 | static inline void ?{}(thread$ & this, struct cluster & cl, size_t stackSize ) { this{ "Anonymous Thread", cl, 0p, stackSize }; } |
---|
78 | static inline void ?{}(thread$ & this, struct cluster & cl, void * storage, size_t storageSize ) { this{ "Anonymous Thread", cl, storage, storageSize }; } |
---|
79 | static inline void ?{}(thread$ & this, const char * const name) { this{ name, *mainCluster, 0p, DEFAULT_STACK_SIZE }; } |
---|
80 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl ) { this{ name, cl, 0p, DEFAULT_STACK_SIZE }; } |
---|
81 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; } |
---|
82 | |
---|
83 | struct thread_dtor_guard_t { |
---|
84 | monitor_dtor_guard_t mg; |
---|
85 | }; |
---|
86 | |
---|
87 | forall( T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled(T)) |
---|
88 | | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); }) |
---|
89 | void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) ); |
---|
90 | void ^?{}( thread_dtor_guard_t & this ); |
---|
91 | |
---|
92 | //----------------------------------------------------------------------------- |
---|
93 | // thread runner |
---|
94 | // Structure that actually start and stop threads |
---|
95 | forall( T & | sized(T) | is_thread(T) ) |
---|
96 | struct scoped { |
---|
97 | T handle; |
---|
98 | }; |
---|
99 | |
---|
100 | forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
101 | void ?{}( scoped(T)& this ); |
---|
102 | |
---|
103 | forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
104 | void ?{}( scoped(T)& this, P params ); |
---|
105 | |
---|
106 | forall( T & | sized(T) | is_thread(T) ) |
---|
107 | void ^?{}( scoped(T)& this ); |
---|
108 | |
---|
109 | //----------------------------------------------------------------------------- |
---|
110 | // Scheduler API |
---|
111 | |
---|
112 | //---------- |
---|
113 | // Park thread: block until corresponding call to unpark, won't block if unpark is already called |
---|
114 | void park( void ); |
---|
115 | |
---|
116 | //---------- |
---|
117 | // Unpark a thread, if the thread is already blocked, schedule it |
---|
118 | // if the thread is not yet block, signal that it should rerun immediately |
---|
119 | void unpark( thread$ * this ); |
---|
120 | |
---|
121 | forall( T & | is_thread(T) ) |
---|
122 | static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );} |
---|
123 | |
---|
124 | //---------- |
---|
125 | // Yield: force thread to block and be rescheduled |
---|
126 | bool force_yield( enum __Preemption_Reason ); |
---|
127 | |
---|
128 | //---------- |
---|
129 | // sleep: force thread to block and be rescheduled after Duration duration |
---|
130 | void sleep( Duration duration ); |
---|
131 | |
---|
132 | //---------- |
---|
133 | // join |
---|
134 | forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled(T)) |
---|
135 | | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); }) |
---|
136 | T & join( T & this ); |
---|
137 | |
---|
138 | //---------- |
---|
139 | // misc |
---|
140 | bool migrate( thread$ * thrd, struct cluster & cl ); |
---|
141 | |
---|
142 | forall( T & | is_thread(T) ) |
---|
143 | static inline bool migrate( T & mutex thrd, struct cluster & cl ) { return migrate( &(thread&)thrd, cl ); } |
---|
144 | |
---|
145 | |
---|
146 | //---------- |
---|
147 | // prng |
---|
148 | void set_seed( size_t seed ); |
---|
149 | size_t get_seed( void ) __attribute__(( warn_unused_result )); |
---|
150 | static inline { |
---|
151 | size_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return PRNG_NAME( th.random_state ); } // [0,UINT_MAX] |
---|
152 | size_t prng( thread$ & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u) |
---|
153 | size_t prng( thread$ & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u] |
---|
154 | forall( T & | is_thread(T) ) { |
---|
155 | size_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX] |
---|
156 | size_t prng( T & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u) |
---|
157 | size_t prng( T & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u] |
---|
158 | } // distribution |
---|
159 | } // distribution |
---|
160 | |
---|
161 | // Local Variables: // |
---|
162 | // mode: c // |
---|
163 | // tab-width: 4 // |
---|
164 | // End: // |
---|