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 : Sat Jun 22 18:19:13 2019 |
---|
13 | // Update Count : 40 |
---|
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 thread_local struct KernelThreadData { |
---|
49 | struct thread_desc * volatile this_thread; |
---|
50 | struct processor * volatile this_processor; |
---|
51 | |
---|
52 | struct { |
---|
53 | volatile unsigned short disable_count; |
---|
54 | volatile bool enabled; |
---|
55 | volatile bool in_progress; |
---|
56 | } preemption_state; |
---|
57 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); |
---|
58 | } |
---|
59 | #endif |
---|
60 | |
---|
61 | struct __stack_context_t { |
---|
62 | void * SP; |
---|
63 | void * FP; |
---|
64 | }; |
---|
65 | |
---|
66 | // low adresses : +----------------------+ <- start of allocation |
---|
67 | // | optional guard page | |
---|
68 | // +----------------------+ <- __stack_t.limit |
---|
69 | // | | |
---|
70 | // | /\ /\ /\ | |
---|
71 | // | || || || | |
---|
72 | // | | |
---|
73 | // | program stack | |
---|
74 | // | | |
---|
75 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base |
---|
76 | // | __stack_t | |
---|
77 | // high adresses : +----------------------+ <- end of allocation |
---|
78 | |
---|
79 | struct __stack_t { |
---|
80 | // stack grows towards stack limit |
---|
81 | void * limit; |
---|
82 | |
---|
83 | // base of stack |
---|
84 | void * base; |
---|
85 | }; |
---|
86 | |
---|
87 | struct __stack_info_t { |
---|
88 | // pointer to stack |
---|
89 | struct __stack_t * storage; |
---|
90 | }; |
---|
91 | |
---|
92 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; |
---|
93 | |
---|
94 | struct coroutine_desc { |
---|
95 | // context that is switch during a CtxSwitch |
---|
96 | struct __stack_context_t context; |
---|
97 | |
---|
98 | // stack information of the coroutine |
---|
99 | struct __stack_info_t stack; |
---|
100 | |
---|
101 | // textual name for coroutine/task |
---|
102 | const char * name; |
---|
103 | |
---|
104 | // current execution status for coroutine |
---|
105 | enum coroutine_state state; |
---|
106 | |
---|
107 | // first coroutine to resume this one |
---|
108 | struct coroutine_desc * starter; |
---|
109 | |
---|
110 | // last coroutine to resume this one |
---|
111 | struct coroutine_desc * last; |
---|
112 | |
---|
113 | // If non-null stack must be unwound with this exception |
---|
114 | struct _Unwind_Exception * cancellation; |
---|
115 | |
---|
116 | }; |
---|
117 | |
---|
118 | // struct which calls the monitor is accepting |
---|
119 | struct __waitfor_mask_t { |
---|
120 | // the index of the accepted function, -1 if none |
---|
121 | short * accepted; |
---|
122 | |
---|
123 | // list of acceptable functions, null if any |
---|
124 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); |
---|
125 | }; |
---|
126 | |
---|
127 | struct monitor_desc { |
---|
128 | // spinlock to protect internal data |
---|
129 | struct __spinlock_t lock; |
---|
130 | |
---|
131 | // current owner of the monitor |
---|
132 | struct thread_desc * owner; |
---|
133 | |
---|
134 | // queue of threads that are blocked waiting for the monitor |
---|
135 | __queue_t(struct thread_desc) entry_queue; |
---|
136 | |
---|
137 | // stack of conditions to run next once we exit the monitor |
---|
138 | __stack_t(struct __condition_criterion_t) signal_stack; |
---|
139 | |
---|
140 | // monitor routines can be called recursively, we need to keep track of that |
---|
141 | unsigned int recursion; |
---|
142 | |
---|
143 | // mask used to know if some thread is waiting for something while holding the monitor |
---|
144 | struct __waitfor_mask_t mask; |
---|
145 | |
---|
146 | // node used to signal the dtor in a waitfor dtor |
---|
147 | struct __condition_node_t * dtor_node; |
---|
148 | }; |
---|
149 | |
---|
150 | struct __monitor_group_t { |
---|
151 | // currently held monitors |
---|
152 | __cfa_anonymous_object( __small_array_t(monitor_desc*) ); |
---|
153 | |
---|
154 | // last function that acquired monitors |
---|
155 | fptr_t func; |
---|
156 | }; |
---|
157 | |
---|
158 | struct thread_desc { |
---|
159 | // Core threading fields |
---|
160 | // context that is switch during a CtxSwitch |
---|
161 | struct __stack_context_t context; |
---|
162 | |
---|
163 | // current execution status for coroutine |
---|
164 | enum coroutine_state state; |
---|
165 | |
---|
166 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it |
---|
167 | |
---|
168 | // coroutine body used to store context |
---|
169 | struct coroutine_desc self_cor; |
---|
170 | |
---|
171 | // current active context |
---|
172 | struct coroutine_desc * curr_cor; |
---|
173 | |
---|
174 | // monitor body used for mutual exclusion |
---|
175 | struct monitor_desc self_mon; |
---|
176 | |
---|
177 | // pointer to monitor with sufficient lifetime for current monitors |
---|
178 | struct monitor_desc * self_mon_p; |
---|
179 | |
---|
180 | // pointer to the cluster on which the thread is running |
---|
181 | struct cluster * curr_cluster; |
---|
182 | |
---|
183 | // monitors currently held by this thread |
---|
184 | struct __monitor_group_t monitors; |
---|
185 | |
---|
186 | // Link lists fields |
---|
187 | // instrusive link field for threads |
---|
188 | struct thread_desc * next; |
---|
189 | |
---|
190 | struct { |
---|
191 | struct thread_desc * next; |
---|
192 | struct thread_desc * prev; |
---|
193 | } node; |
---|
194 | }; |
---|
195 | |
---|
196 | #ifdef __cforall |
---|
197 | extern "Cforall" { |
---|
198 | static inline thread_desc *& get_next( thread_desc & this ) { |
---|
199 | return this.next; |
---|
200 | } |
---|
201 | |
---|
202 | static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { |
---|
203 | return this.node.[next, prev]; |
---|
204 | } |
---|
205 | |
---|
206 | static inline void ?{}(__monitor_group_t & this) { |
---|
207 | (this.data){NULL}; |
---|
208 | (this.size){0}; |
---|
209 | (this.func){NULL}; |
---|
210 | } |
---|
211 | |
---|
212 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** 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 ) { |
---|
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 CtxInvokeStub( void ); |
---|
253 | extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch"); |
---|
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: // |
---|