1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2020 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 | // kernel/fwd.hfa -- PUBLIC |
---|
8 | // Fundamental code needed to implement threading M.E.S. algorithms. |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Thu Jul 30 16:46:41 2020 |
---|
12 | // Last Modified By : |
---|
13 | // Last Modified On : |
---|
14 | // Update Count : |
---|
15 | // |
---|
16 | |
---|
17 | #pragma once |
---|
18 | |
---|
19 | #include "bits/defs.hfa" |
---|
20 | #include "bits/debug.hfa" |
---|
21 | |
---|
22 | #ifdef __cforall |
---|
23 | #include "bits/random.hfa" |
---|
24 | #endif |
---|
25 | |
---|
26 | struct thread$; |
---|
27 | struct processor; |
---|
28 | struct cluster; |
---|
29 | |
---|
30 | enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION }; |
---|
31 | |
---|
32 | #define KERNEL_STORAGE(T,X) __attribute((aligned(__alignof__(T)))) static char storage_##X[sizeof(T)] |
---|
33 | |
---|
34 | #ifdef __cforall |
---|
35 | extern "C" { |
---|
36 | extern "Cforall" { |
---|
37 | extern __attribute__((aligned(128))) thread_local struct KernelThreadData { |
---|
38 | struct thread$ * volatile this_thread; |
---|
39 | struct processor * volatile this_processor; |
---|
40 | volatile bool sched_lock; |
---|
41 | |
---|
42 | struct { |
---|
43 | volatile unsigned short disable_count; |
---|
44 | volatile bool enabled; |
---|
45 | volatile bool in_progress; |
---|
46 | } preemption_state; |
---|
47 | |
---|
48 | #if defined(__SIZEOF_INT128__) |
---|
49 | __uint128_t rand_seed; |
---|
50 | #else |
---|
51 | uint64_t rand_seed; |
---|
52 | #endif |
---|
53 | struct { |
---|
54 | uint64_t fwd_seed; |
---|
55 | uint64_t bck_seed; |
---|
56 | } ready_rng; |
---|
57 | |
---|
58 | struct __stats_t * volatile this_stats; |
---|
59 | |
---|
60 | |
---|
61 | #ifdef __CFA_WITH_VERIFY__ |
---|
62 | // Debug, check if the rwlock is owned for reading |
---|
63 | bool in_sched_lock; |
---|
64 | unsigned sched_id; |
---|
65 | #endif |
---|
66 | } __cfaabi_tls __attribute__ ((tls_model ( "initial-exec" ))); |
---|
67 | |
---|
68 | extern bool __preemption_enabled(); |
---|
69 | |
---|
70 | static inline KernelThreadData & kernelTLS( void ) { |
---|
71 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
72 | return __cfaabi_tls; |
---|
73 | } |
---|
74 | |
---|
75 | extern uintptr_t __cfatls_get( unsigned long int member ); |
---|
76 | #define publicTLS_get( member ) ((typeof(__cfaabi_tls.member))__cfatls_get( __builtin_offsetof(KernelThreadData, member) )) |
---|
77 | |
---|
78 | static inline uint64_t __tls_rand() { |
---|
79 | return |
---|
80 | #if defined(__SIZEOF_INT128__) |
---|
81 | __lehmer64( kernelTLS().rand_seed ); |
---|
82 | #else |
---|
83 | __xorshift64( kernelTLS().rand_seed ); |
---|
84 | #endif |
---|
85 | } |
---|
86 | |
---|
87 | #define M (1_l64u << 48_l64u) |
---|
88 | #define A (25214903917_l64u) |
---|
89 | #define AI (18446708753438544741_l64u) |
---|
90 | #define C (11_l64u) |
---|
91 | #define D (16_l64u) |
---|
92 | |
---|
93 | static inline unsigned __tls_rand_fwd() { |
---|
94 | kernelTLS().ready_rng.fwd_seed = (A * kernelTLS().ready_rng.fwd_seed + C) & (M - 1); |
---|
95 | return kernelTLS().ready_rng.fwd_seed >> D; |
---|
96 | } |
---|
97 | |
---|
98 | static inline unsigned __tls_rand_bck() { |
---|
99 | unsigned int r = kernelTLS().ready_rng.bck_seed >> D; |
---|
100 | kernelTLS().ready_rng.bck_seed = AI * (kernelTLS().ready_rng.bck_seed - C) & (M - 1); |
---|
101 | return r; |
---|
102 | } |
---|
103 | |
---|
104 | #undef M |
---|
105 | #undef A |
---|
106 | #undef AI |
---|
107 | #undef C |
---|
108 | #undef D |
---|
109 | |
---|
110 | static inline void __tls_rand_advance_bck(void) { |
---|
111 | kernelTLS().ready_rng.bck_seed = kernelTLS().ready_rng.fwd_seed; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | extern void disable_interrupts(); |
---|
116 | extern void enable_interrupts( bool poll = false ); |
---|
117 | |
---|
118 | extern "Cforall" { |
---|
119 | enum unpark_hint { UNPARK_LOCAL, UNPARK_REMOTE }; |
---|
120 | |
---|
121 | extern void park( void ); |
---|
122 | extern void unpark( struct thread$ *, unpark_hint ); |
---|
123 | static inline void unpark( struct thread$ * thrd ) { unpark(thrd, UNPARK_LOCAL); } |
---|
124 | static inline struct thread$ * active_thread () { |
---|
125 | struct thread$ * t = publicTLS_get( this_thread ); |
---|
126 | /* paranoid */ verify( t ); |
---|
127 | return t; |
---|
128 | } |
---|
129 | |
---|
130 | extern bool force_yield( enum __Preemption_Reason ); |
---|
131 | |
---|
132 | static inline void yield() { |
---|
133 | force_yield(__MANUAL_PREEMPTION); |
---|
134 | } |
---|
135 | |
---|
136 | // Yield: yield N times |
---|
137 | static inline void yield( unsigned times ) { |
---|
138 | for( times ) { |
---|
139 | yield(); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | extern uint64_t thread_rand(); |
---|
144 | |
---|
145 | // Semaphore which only supports a single thread |
---|
146 | struct single_sem { |
---|
147 | struct thread$ * volatile ptr; |
---|
148 | }; |
---|
149 | |
---|
150 | static inline { |
---|
151 | void ?{}(single_sem & this) { |
---|
152 | this.ptr = 0p; |
---|
153 | } |
---|
154 | |
---|
155 | void ^?{}(single_sem &) {} |
---|
156 | |
---|
157 | bool wait(single_sem & this) { |
---|
158 | for() { |
---|
159 | struct thread$ * expected = this.ptr; |
---|
160 | if(expected == 1p) { |
---|
161 | if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
162 | return false; |
---|
163 | } |
---|
164 | } |
---|
165 | else { |
---|
166 | /* paranoid */ verify( expected == 0p ); |
---|
167 | if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
168 | park(); |
---|
169 | return true; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | bool post(single_sem & this) { |
---|
177 | for() { |
---|
178 | struct thread$ * expected = this.ptr; |
---|
179 | if(expected == 1p) return false; |
---|
180 | if(expected == 0p) { |
---|
181 | if(__atomic_compare_exchange_n(&this.ptr, &expected, 1p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
182 | return false; |
---|
183 | } |
---|
184 | } |
---|
185 | else { |
---|
186 | if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
187 | unpark( expected ); |
---|
188 | return true; |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | // Synchronozation primitive which only supports a single thread and one post |
---|
196 | // Similar to a binary semaphore with a 'one shot' semantic |
---|
197 | // is expected to be discarded after each party call their side |
---|
198 | struct oneshot { |
---|
199 | // Internal state : |
---|
200 | // 0p : is initial state (wait will block) |
---|
201 | // 1p : fulfilled (wait won't block) |
---|
202 | // any thread : a thread is currently waiting |
---|
203 | struct thread$ * volatile ptr; |
---|
204 | }; |
---|
205 | |
---|
206 | static inline { |
---|
207 | void ?{}(oneshot & this) { |
---|
208 | this.ptr = 0p; |
---|
209 | } |
---|
210 | |
---|
211 | void ^?{}(oneshot &) {} |
---|
212 | |
---|
213 | // Wait for the post, return immidiately if it already happened. |
---|
214 | // return true if the thread was parked |
---|
215 | bool wait(oneshot & this) { |
---|
216 | for() { |
---|
217 | struct thread$ * expected = this.ptr; |
---|
218 | if(expected == 1p) return false; |
---|
219 | /* paranoid */ verify( expected == 0p ); |
---|
220 | if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
221 | park(); |
---|
222 | /* paranoid */ verify( this.ptr == 1p ); |
---|
223 | return true; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | // Mark as fulfilled, wake thread if needed |
---|
229 | // return true if a thread was unparked |
---|
230 | thread$ * post(oneshot & this, bool do_unpark = true) { |
---|
231 | struct thread$ * got = __atomic_exchange_n( &this.ptr, 1p, __ATOMIC_SEQ_CST); |
---|
232 | if( got == 0p ) return 0p; |
---|
233 | if(do_unpark) unpark( got ); |
---|
234 | return got; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | // base types for future to build upon |
---|
239 | // It is based on the 'oneshot' type to allow multiple futures |
---|
240 | // to block on the same instance, permitting users to block a single |
---|
241 | // thread on "any of" [a given set of] futures. |
---|
242 | // does not support multiple threads waiting on the same future |
---|
243 | struct future_t { |
---|
244 | // Internal state : |
---|
245 | // 0p : is initial state (wait will block) |
---|
246 | // 1p : fulfilled (wait won't block) |
---|
247 | // 2p : in progress () |
---|
248 | // 3p : abandoned, server should delete |
---|
249 | // any oneshot : a context has been setup to wait, a thread could wait on it |
---|
250 | struct oneshot * volatile ptr; |
---|
251 | }; |
---|
252 | |
---|
253 | static inline { |
---|
254 | void ?{}(future_t & this) { |
---|
255 | this.ptr = 0p; |
---|
256 | } |
---|
257 | |
---|
258 | void ^?{}(future_t &) {} |
---|
259 | |
---|
260 | void reset(future_t & this) { |
---|
261 | // needs to be in 0p or 1p |
---|
262 | __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST); |
---|
263 | } |
---|
264 | |
---|
265 | // check if the future is available |
---|
266 | bool available( future_t & this ) { |
---|
267 | return this.ptr == 1p; |
---|
268 | } |
---|
269 | |
---|
270 | // Prepare the future to be waited on |
---|
271 | // intented to be use by wait, wait_any, waitfor, etc. rather than used directly |
---|
272 | bool setup( future_t & this, oneshot & wait_ctx ) { |
---|
273 | /* paranoid */ verify( wait_ctx.ptr == 0p ); |
---|
274 | // The future needs to set the wait context |
---|
275 | for() { |
---|
276 | struct oneshot * expected = this.ptr; |
---|
277 | // Is the future already fulfilled? |
---|
278 | if(expected == 1p) return false; // Yes, just return false (didn't block) |
---|
279 | |
---|
280 | // The future is not fulfilled, try to setup the wait context |
---|
281 | /* paranoid */ verify( expected == 0p ); |
---|
282 | if(__atomic_compare_exchange_n(&this.ptr, &expected, &wait_ctx, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
283 | return true; |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | // Stop waiting on a future |
---|
289 | // When multiple futures are waited for together in "any of" pattern |
---|
290 | // futures that weren't fulfilled before the thread woke up |
---|
291 | // should retract the wait ctx |
---|
292 | // intented to be use by wait, wait_any, waitfor, etc. rather than used directly |
---|
293 | void retract( future_t & this, oneshot & wait_ctx ) { |
---|
294 | // Remove the wait context |
---|
295 | struct oneshot * got = __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST); |
---|
296 | |
---|
297 | // got == 0p: future was never actually setup, just return |
---|
298 | if( got == 0p ) return; |
---|
299 | |
---|
300 | // got == wait_ctx: since fulfil does an atomic_swap, |
---|
301 | // if we got back the original then no one else saw context |
---|
302 | // It is safe to delete (which could happen after the return) |
---|
303 | if( got == &wait_ctx ) return; |
---|
304 | |
---|
305 | // got == 1p: the future is ready and the context was fully consumed |
---|
306 | // the server won't use the pointer again |
---|
307 | // It is safe to delete (which could happen after the return) |
---|
308 | if( got == 1p ) return; |
---|
309 | |
---|
310 | // got == 2p: the future is ready but the context hasn't fully been consumed |
---|
311 | // spin until it is safe to move on |
---|
312 | if( got == 2p ) { |
---|
313 | while( this.ptr != 1p ) Pause(); |
---|
314 | return; |
---|
315 | } |
---|
316 | |
---|
317 | // got == any thing else, something wen't wrong here, abort |
---|
318 | abort("Future in unexpected state"); |
---|
319 | } |
---|
320 | |
---|
321 | // Mark the future as abandoned, meaning it will be deleted by the server |
---|
322 | bool abandon( future_t & this ) { |
---|
323 | /* paranoid */ verify( this.ptr != 3p ); |
---|
324 | |
---|
325 | // Mark the future as abandonned |
---|
326 | struct oneshot * got = __atomic_exchange_n( &this.ptr, 3p, __ATOMIC_SEQ_CST); |
---|
327 | |
---|
328 | // If the future isn't already fulfilled, let the server delete it |
---|
329 | if( got == 0p ) return false; |
---|
330 | |
---|
331 | // got == 2p: the future is ready but the context hasn't fully been consumed |
---|
332 | // spin until it is safe to move on |
---|
333 | if( got == 2p ) { |
---|
334 | while( this.ptr != 1p ) Pause(); |
---|
335 | got = 1p; |
---|
336 | } |
---|
337 | |
---|
338 | // The future is completed delete it now |
---|
339 | /* paranoid */ verify( this.ptr != 1p ); |
---|
340 | free( &this ); |
---|
341 | return true; |
---|
342 | } |
---|
343 | |
---|
344 | // from the server side, mark the future as fulfilled |
---|
345 | // delete it if needed |
---|
346 | thread$ * fulfil( future_t & this, bool do_unpark = true ) { |
---|
347 | for() { |
---|
348 | struct oneshot * expected = this.ptr; |
---|
349 | // was this abandoned? |
---|
350 | #if defined(__GNUC__) && __GNUC__ >= 7 |
---|
351 | #pragma GCC diagnostic push |
---|
352 | #pragma GCC diagnostic ignored "-Wfree-nonheap-object" |
---|
353 | #endif |
---|
354 | if( expected == 3p ) { free( &this ); return 0p; } |
---|
355 | #if defined(__GNUC__) && __GNUC__ >= 7 |
---|
356 | #pragma GCC diagnostic pop |
---|
357 | #endif |
---|
358 | |
---|
359 | /* paranoid */ verify( expected != 1p ); // Future is already fulfilled, should not happen |
---|
360 | /* paranoid */ verify( expected != 2p ); // Future is bein fulfilled by someone else, this is even less supported then the previous case. |
---|
361 | |
---|
362 | // If there is a wait context, we need to consume it and mark it as consumed after |
---|
363 | // If there is no context then we can skip the in progress phase |
---|
364 | struct oneshot * want = expected == 0p ? 1p : 2p; |
---|
365 | if(__atomic_compare_exchange_n(&this.ptr, &expected, want, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
366 | if( expected == 0p ) { /* paranoid */ verify( this.ptr == 1p); return 0p; } |
---|
367 | thread$ * ret = post( *expected, do_unpark ); |
---|
368 | __atomic_store_n( &this.ptr, 1p, __ATOMIC_SEQ_CST); |
---|
369 | return ret; |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | } |
---|
374 | |
---|
375 | // Wait for the future to be fulfilled |
---|
376 | bool wait( future_t & this ) { |
---|
377 | oneshot temp; |
---|
378 | if( !setup(this, temp) ) return false; |
---|
379 | |
---|
380 | // Wait context is setup, just wait on it |
---|
381 | bool ret = wait( temp ); |
---|
382 | |
---|
383 | // Wait for the future to tru |
---|
384 | while( this.ptr == 2p ) Pause(); |
---|
385 | // Make sure the state makes sense |
---|
386 | // Should be fulfilled, could be in progress but it's out of date if so |
---|
387 | // since if that is the case, the oneshot was fulfilled (unparking this thread) |
---|
388 | // and the oneshot should not be needed any more |
---|
389 | __attribute__((unused)) struct oneshot * was = this.ptr; |
---|
390 | /* paranoid */ verifyf( was == 1p, "Expected this.ptr to be 1p, was %p\n", was ); |
---|
391 | |
---|
392 | // Mark the future as fulfilled, to be consistent |
---|
393 | // with potential calls to avail |
---|
394 | // this.ptr = 1p; |
---|
395 | return ret; |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | //----------------------------------------------------------------------- |
---|
400 | // Statics call at the end of each thread to register statistics |
---|
401 | #if !defined(__CFA_NO_STATISTICS__) |
---|
402 | static inline struct __stats_t * __tls_stats() { |
---|
403 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
404 | /* paranoid */ verify( kernelTLS().this_stats ); |
---|
405 | return kernelTLS().this_stats; |
---|
406 | } |
---|
407 | |
---|
408 | #define __STATS__(in_kernel, ...) { \ |
---|
409 | if( !(in_kernel) ) disable_interrupts(); \ |
---|
410 | with( *__tls_stats() ) { \ |
---|
411 | __VA_ARGS__ \ |
---|
412 | } \ |
---|
413 | if( !(in_kernel) ) enable_interrupts(); \ |
---|
414 | } |
---|
415 | #else |
---|
416 | #define __STATS__(in_kernel, ...) |
---|
417 | #endif |
---|
418 | } |
---|
419 | } |
---|
420 | #endif |
---|