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 __cfaehm_try_resume_node;
|
---|
30 | struct __cfaehm_base_exception_t;
|
---|
31 | struct exception_context_t {
|
---|
32 | struct __cfaehm_try_resume_node * top_resume;
|
---|
33 | struct __cfaehm_base_exception_t * current_exception;
|
---|
34 | };
|
---|
35 |
|
---|
36 | struct __stack_context_t {
|
---|
37 | void * SP;
|
---|
38 | void * FP;
|
---|
39 | };
|
---|
40 |
|
---|
41 | // low adresses : +----------------------+ <- start of allocation
|
---|
42 | // | optional guard page |
|
---|
43 | // +----------------------+ <- __stack_t.limit
|
---|
44 | // | |
|
---|
45 | // | /\ /\ /\ |
|
---|
46 | // | || || || |
|
---|
47 | // | |
|
---|
48 | // | program stack |
|
---|
49 | // | |
|
---|
50 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base
|
---|
51 | // | __stack_t |
|
---|
52 | // high adresses : +----------------------+ <- end of allocation
|
---|
53 |
|
---|
54 | struct __stack_t {
|
---|
55 | // stack grows towards stack limit
|
---|
56 | void * limit;
|
---|
57 |
|
---|
58 | // base of stack
|
---|
59 | void * base;
|
---|
60 |
|
---|
61 | // Information for exception handling.
|
---|
62 | struct exception_context_t exception_context;
|
---|
63 | };
|
---|
64 |
|
---|
65 | struct __stack_info_t {
|
---|
66 | // pointer to stack
|
---|
67 | struct __stack_t * storage;
|
---|
68 | };
|
---|
69 |
|
---|
70 | enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active, Cancelled, Halting };
|
---|
71 |
|
---|
72 | struct $coroutine {
|
---|
73 | // context that is switch during a __cfactx_switch
|
---|
74 | struct __stack_context_t context;
|
---|
75 |
|
---|
76 | // stack information of the coroutine
|
---|
77 | struct __stack_info_t stack;
|
---|
78 |
|
---|
79 | // textual name for coroutine/task
|
---|
80 | const char * name;
|
---|
81 |
|
---|
82 | // current execution status for coroutine
|
---|
83 | enum __Coroutine_State state;
|
---|
84 |
|
---|
85 | // first coroutine to resume this one
|
---|
86 | struct $coroutine * starter;
|
---|
87 |
|
---|
88 | // last coroutine to resume this one
|
---|
89 | struct $coroutine * last;
|
---|
90 |
|
---|
91 | // If non-null stack must be unwound with this exception
|
---|
92 | struct _Unwind_Exception * cancellation;
|
---|
93 |
|
---|
94 | };
|
---|
95 | // Wrapper for gdb
|
---|
96 | struct cfathread_coroutine_t { struct $coroutine debug; };
|
---|
97 |
|
---|
98 | static inline struct __stack_t * __get_stack( struct $coroutine * cor ) {
|
---|
99 | return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2));
|
---|
100 | }
|
---|
101 |
|
---|
102 | // struct which calls the monitor is accepting
|
---|
103 | struct __waitfor_mask_t {
|
---|
104 | // the index of the accepted function, -1 if none
|
---|
105 | short * accepted;
|
---|
106 |
|
---|
107 | // list of acceptable functions, null if any
|
---|
108 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
|
---|
109 | };
|
---|
110 |
|
---|
111 | struct $monitor {
|
---|
112 | // spinlock to protect internal data
|
---|
113 | struct __spinlock_t lock;
|
---|
114 |
|
---|
115 | // current owner of the monitor
|
---|
116 | struct $thread * owner;
|
---|
117 |
|
---|
118 | // queue of threads that are blocked waiting for the monitor
|
---|
119 | __queue_t(struct $thread) entry_queue;
|
---|
120 |
|
---|
121 | // stack of conditions to run next once we exit the monitor
|
---|
122 | __stack_t(struct __condition_criterion_t) signal_stack;
|
---|
123 |
|
---|
124 | // monitor routines can be called recursively, we need to keep track of that
|
---|
125 | unsigned int recursion;
|
---|
126 |
|
---|
127 | // mask used to know if some thread is waiting for something while holding the monitor
|
---|
128 | struct __waitfor_mask_t mask;
|
---|
129 |
|
---|
130 | // node used to signal the dtor in a waitfor dtor
|
---|
131 | struct __condition_node_t * dtor_node;
|
---|
132 | };
|
---|
133 | // Wrapper for gdb
|
---|
134 | struct cfathread_monitor_t { struct $monitor debug; };
|
---|
135 |
|
---|
136 | struct __monitor_group_t {
|
---|
137 | // currently held monitors
|
---|
138 | __cfa_anonymous_object( __small_array_t($monitor*) );
|
---|
139 |
|
---|
140 | // last function that acquired monitors
|
---|
141 | fptr_t func;
|
---|
142 | };
|
---|
143 |
|
---|
144 | // Link lists fields
|
---|
145 | // instrusive link field for threads
|
---|
146 | struct __thread_desc_link {
|
---|
147 | struct $thread * next;
|
---|
148 | struct $thread * prev;
|
---|
149 | volatile unsigned long long ts;
|
---|
150 | int preferred;
|
---|
151 | };
|
---|
152 |
|
---|
153 | struct $thread {
|
---|
154 | // Core threading fields
|
---|
155 | // context that is switch during a __cfactx_switch
|
---|
156 | struct __stack_context_t context;
|
---|
157 |
|
---|
158 | // current execution status for coroutine
|
---|
159 | // Possible values are:
|
---|
160 | // - TICKET_BLOCKED (-1) thread is blocked
|
---|
161 | // - TICKET_RUNNING ( 0) thread is running
|
---|
162 | // - TICKET_UNBLOCK ( 1) thread should ignore next block
|
---|
163 | volatile int ticket;
|
---|
164 | enum __Coroutine_State state:8;
|
---|
165 | enum __Preemption_Reason preempted:8;
|
---|
166 |
|
---|
167 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it
|
---|
168 |
|
---|
169 | // pointer to the cluster on which the thread is running
|
---|
170 | struct cluster * curr_cluster;
|
---|
171 |
|
---|
172 | // Link lists fields
|
---|
173 | // instrusive link field for threads
|
---|
174 | struct __thread_desc_link link;
|
---|
175 |
|
---|
176 | // coroutine body used to store context
|
---|
177 | struct $coroutine self_cor;
|
---|
178 |
|
---|
179 | // current active context
|
---|
180 | struct $coroutine * curr_cor;
|
---|
181 |
|
---|
182 | // monitor body used for mutual exclusion
|
---|
183 | struct $monitor self_mon;
|
---|
184 |
|
---|
185 | // pointer to monitor with sufficient lifetime for current monitors
|
---|
186 | struct $monitor * self_mon_p;
|
---|
187 |
|
---|
188 | // monitors currently held by this thread
|
---|
189 | struct __monitor_group_t monitors;
|
---|
190 |
|
---|
191 | // used to put threads on user data structures
|
---|
192 | struct {
|
---|
193 | struct $thread * next;
|
---|
194 | struct $thread * back;
|
---|
195 | } seqable;
|
---|
196 |
|
---|
197 | struct {
|
---|
198 | struct $thread * next;
|
---|
199 | struct $thread * prev;
|
---|
200 | } node;
|
---|
201 |
|
---|
202 | #if defined( __CFA_WITH_VERIFY__ )
|
---|
203 | void * canary;
|
---|
204 | #endif
|
---|
205 | };
|
---|
206 | // Wrapper for gdb
|
---|
207 | struct cfathread_thread_t { struct $thread debug; };
|
---|
208 |
|
---|
209 | #ifdef __CFA_DEBUG__
|
---|
210 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]);
|
---|
211 | #else
|
---|
212 | #define __cfaabi_dbg_record_thrd(x, y, z)
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | #ifdef __cforall
|
---|
216 | extern "Cforall" {
|
---|
217 |
|
---|
218 | static inline $thread *& get_next( $thread & this ) __attribute__((const)) {
|
---|
219 | return this.link.next;
|
---|
220 | }
|
---|
221 |
|
---|
222 | static inline [$thread *&, $thread *& ] __get( $thread & this ) __attribute__((const)) {
|
---|
223 | return this.node.[next, prev];
|
---|
224 | }
|
---|
225 |
|
---|
226 | static inline $thread * volatile & ?`next ( $thread * this ) __attribute__((const)) {
|
---|
227 | return this->seqable.next;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static inline $thread *& Back( $thread * this ) __attribute__((const)) {
|
---|
231 | return this->seqable.back;
|
---|
232 | }
|
---|
233 |
|
---|
234 | static inline $thread *& Next( $thread * this ) __attribute__((const)) {
|
---|
235 | return this->seqable.next;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static inline bool listed( $thread * this ) {
|
---|
239 | return this->seqable.next != 0p;
|
---|
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: //
|
---|