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