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 | // invoke.h -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Dec 5 16:26:03 2019 |
---|
13 | // Update Count : 44 |
---|
14 | // |
---|
15 | |
---|
16 | #include "bits/containers.hfa" |
---|
17 | #include "bits/defs.hfa" |
---|
18 | #include "bits/locks.hfa" |
---|
19 | #include "kernel/fwd.hfa" |
---|
20 | |
---|
21 | #ifdef __cforall |
---|
22 | extern "C" { |
---|
23 | #endif |
---|
24 | |
---|
25 | #if ! defined(__CFA_INVOKE_PRIVATE__) |
---|
26 | #ifndef _INVOKE_H_ |
---|
27 | #define _INVOKE_H_ |
---|
28 | |
---|
29 | struct __stack_context_t { |
---|
30 | void * SP; |
---|
31 | void * FP; |
---|
32 | }; |
---|
33 | |
---|
34 | // low adresses : +----------------------+ <- start of allocation |
---|
35 | // | optional guard page | |
---|
36 | // +----------------------+ <- __stack_t.limit |
---|
37 | // | | |
---|
38 | // | /\ /\ /\ | |
---|
39 | // | || || || | |
---|
40 | // | | |
---|
41 | // | program stack | |
---|
42 | // | | |
---|
43 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base |
---|
44 | // | __stack_t | |
---|
45 | // high adresses : +----------------------+ <- end of allocation |
---|
46 | |
---|
47 | struct __stack_t { |
---|
48 | // stack grows towards stack limit |
---|
49 | void * limit; |
---|
50 | |
---|
51 | // base of stack |
---|
52 | void * base; |
---|
53 | }; |
---|
54 | |
---|
55 | struct __stack_info_t { |
---|
56 | // pointer to stack |
---|
57 | struct __stack_t * storage; |
---|
58 | }; |
---|
59 | |
---|
60 | enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active }; |
---|
61 | |
---|
62 | struct $coroutine { |
---|
63 | // context that is switch during a __cfactx_switch |
---|
64 | struct __stack_context_t context; |
---|
65 | |
---|
66 | // stack information of the coroutine |
---|
67 | struct __stack_info_t stack; |
---|
68 | |
---|
69 | // textual name for coroutine/task |
---|
70 | const char * name; |
---|
71 | |
---|
72 | // current execution status for coroutine |
---|
73 | enum __Coroutine_State state; |
---|
74 | |
---|
75 | // first coroutine to resume this one |
---|
76 | struct $coroutine * starter; |
---|
77 | |
---|
78 | // last coroutine to resume this one |
---|
79 | struct $coroutine * last; |
---|
80 | |
---|
81 | // If non-null stack must be unwound with this exception |
---|
82 | struct _Unwind_Exception * cancellation; |
---|
83 | |
---|
84 | }; |
---|
85 | |
---|
86 | static inline struct __stack_t * __get_stack( struct $coroutine * cor ) { return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); } |
---|
87 | |
---|
88 | // struct which calls the monitor is accepting |
---|
89 | struct __waitfor_mask_t { |
---|
90 | // the index of the accepted function, -1 if none |
---|
91 | short * accepted; |
---|
92 | |
---|
93 | // list of acceptable functions, null if any |
---|
94 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); |
---|
95 | }; |
---|
96 | |
---|
97 | struct $monitor { |
---|
98 | // spinlock to protect internal data |
---|
99 | struct __spinlock_t lock; |
---|
100 | |
---|
101 | // current owner of the monitor |
---|
102 | struct $thread * owner; |
---|
103 | |
---|
104 | // queue of threads that are blocked waiting for the monitor |
---|
105 | __queue_t(struct $thread) entry_queue; |
---|
106 | |
---|
107 | // stack of conditions to run next once we exit the monitor |
---|
108 | __stack_t(struct __condition_criterion_t) signal_stack; |
---|
109 | |
---|
110 | // monitor routines can be called recursively, we need to keep track of that |
---|
111 | unsigned int recursion; |
---|
112 | |
---|
113 | // mask used to know if some thread is waiting for something while holding the monitor |
---|
114 | struct __waitfor_mask_t mask; |
---|
115 | |
---|
116 | // node used to signal the dtor in a waitfor dtor |
---|
117 | struct __condition_node_t * dtor_node; |
---|
118 | }; |
---|
119 | |
---|
120 | struct __monitor_group_t { |
---|
121 | // currently held monitors |
---|
122 | __cfa_anonymous_object( __small_array_t($monitor*) ); |
---|
123 | |
---|
124 | // last function that acquired monitors |
---|
125 | fptr_t func; |
---|
126 | }; |
---|
127 | |
---|
128 | // Link lists fields |
---|
129 | // instrusive link field for threads |
---|
130 | struct __thread_desc_link { |
---|
131 | struct $thread * next; |
---|
132 | struct $thread * prev; |
---|
133 | volatile unsigned long long ts; |
---|
134 | int preferred; |
---|
135 | }; |
---|
136 | |
---|
137 | struct $thread { |
---|
138 | // Core threading fields |
---|
139 | // context that is switch during a __cfactx_switch |
---|
140 | struct __stack_context_t context; |
---|
141 | |
---|
142 | // current execution status for coroutine |
---|
143 | volatile int ticket; |
---|
144 | enum __Coroutine_State state:8; |
---|
145 | enum __Preemption_Reason preempted:8; |
---|
146 | |
---|
147 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it |
---|
148 | |
---|
149 | // pointer to the cluster on which the thread is running |
---|
150 | struct cluster * curr_cluster; |
---|
151 | |
---|
152 | // Link lists fields |
---|
153 | // instrusive link field for threads |
---|
154 | struct __thread_desc_link link; |
---|
155 | |
---|
156 | // coroutine body used to store context |
---|
157 | struct $coroutine self_cor; |
---|
158 | |
---|
159 | // current active context |
---|
160 | struct $coroutine * curr_cor; |
---|
161 | |
---|
162 | // monitor body used for mutual exclusion |
---|
163 | struct $monitor self_mon; |
---|
164 | |
---|
165 | // pointer to monitor with sufficient lifetime for current monitors |
---|
166 | struct $monitor * self_mon_p; |
---|
167 | |
---|
168 | // monitors currently held by this thread |
---|
169 | struct __monitor_group_t monitors; |
---|
170 | |
---|
171 | struct { |
---|
172 | struct $thread * next; |
---|
173 | struct $thread * prev; |
---|
174 | } node; |
---|
175 | |
---|
176 | #ifdef __CFA_DEBUG__ |
---|
177 | // previous function to park/unpark the thread |
---|
178 | const char * park_caller; |
---|
179 | int park_result; |
---|
180 | enum __Coroutine_State park_state; |
---|
181 | bool park_stale; |
---|
182 | const char * unpark_caller; |
---|
183 | int unpark_result; |
---|
184 | enum __Coroutine_State unpark_state; |
---|
185 | bool unpark_stale; |
---|
186 | #endif |
---|
187 | }; |
---|
188 | |
---|
189 | #ifdef __CFA_DEBUG__ |
---|
190 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]); |
---|
191 | #else |
---|
192 | #define __cfaabi_dbg_record_thrd(x, y, z) |
---|
193 | #endif |
---|
194 | |
---|
195 | #ifdef __cforall |
---|
196 | extern "Cforall" { |
---|
197 | |
---|
198 | static inline $thread *& get_next( $thread & this ) __attribute__((const)) { |
---|
199 | return this.link.next; |
---|
200 | } |
---|
201 | |
---|
202 | static inline [$thread *&, $thread *& ] __get( $thread & this ) __attribute__((const)) { |
---|
203 | return this.node.[next, prev]; |
---|
204 | } |
---|
205 | |
---|
206 | static inline void ?{}(__monitor_group_t & this) { |
---|
207 | (this.data){0p}; |
---|
208 | (this.size){0}; |
---|
209 | (this.func){NULL}; |
---|
210 | } |
---|
211 | |
---|
212 | static inline void ?{}(__monitor_group_t & this, struct $monitor ** data, __lock_size_t size, fptr_t func) { |
---|
213 | (this.data){data}; |
---|
214 | (this.size){size}; |
---|
215 | (this.func){func}; |
---|
216 | } |
---|
217 | |
---|
218 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) { |
---|
219 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; |
---|
220 | if( lhs.size != rhs.size ) return false; |
---|
221 | if( lhs.func != rhs.func ) return false; |
---|
222 | |
---|
223 | // Check that all the monitors match |
---|
224 | for( int i = 0; i < lhs.size; i++ ) { |
---|
225 | // If not a match, check next function |
---|
226 | if( lhs[i] != rhs[i] ) return false; |
---|
227 | } |
---|
228 | |
---|
229 | return true; |
---|
230 | } |
---|
231 | |
---|
232 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { |
---|
233 | lhs.data = rhs.data; |
---|
234 | lhs.size = rhs.size; |
---|
235 | lhs.func = rhs.func; |
---|
236 | } |
---|
237 | } |
---|
238 | #endif |
---|
239 | |
---|
240 | #endif //_INVOKE_H_ |
---|
241 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
242 | #ifndef _INVOKE_PRIVATE_H_ |
---|
243 | #define _INVOKE_PRIVATE_H_ |
---|
244 | |
---|
245 | struct machine_context_t { |
---|
246 | void *SP; |
---|
247 | void *FP; |
---|
248 | void *PC; |
---|
249 | }; |
---|
250 | |
---|
251 | // assembler routines that performs the context switch |
---|
252 | extern void __cfactx_invoke_stub( void ); |
---|
253 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); |
---|
254 | // void CtxStore ( void * this ) asm ("CtxStore"); |
---|
255 | // void CtxRet ( void * dst ) asm ("CtxRet"); |
---|
256 | |
---|
257 | #endif //_INVOKE_PRIVATE_H_ |
---|
258 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
259 | #ifdef __cforall |
---|
260 | } |
---|
261 | #endif |
---|
262 | |
---|
263 | // Local Variables: // |
---|
264 | // mode: c // |
---|
265 | // tab-width: 4 // |
---|
266 | // End: // |
---|