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 -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Thu Jul 30 16:46:41 2020 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "bits/defs.hfa" |
---|
19 | #include "bits/debug.hfa" |
---|
20 | |
---|
21 | #ifdef __cforall |
---|
22 | #include "bits/random.hfa" |
---|
23 | #endif |
---|
24 | |
---|
25 | struct $thread; |
---|
26 | struct processor; |
---|
27 | struct cluster; |
---|
28 | |
---|
29 | enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION }; |
---|
30 | |
---|
31 | #define KERNEL_STORAGE(T,X) __attribute((aligned(__alignof__(T)))) static char storage_##X[sizeof(T)] |
---|
32 | |
---|
33 | #ifdef __cforall |
---|
34 | extern "C" { |
---|
35 | extern "Cforall" { |
---|
36 | extern __attribute__((aligned(128))) thread_local struct KernelThreadData { |
---|
37 | struct $thread * volatile this_thread; |
---|
38 | struct processor * volatile this_processor; |
---|
39 | struct __stats_t * volatile this_stats; |
---|
40 | |
---|
41 | struct { |
---|
42 | volatile unsigned short disable_count; |
---|
43 | volatile bool enabled; |
---|
44 | volatile bool in_progress; |
---|
45 | } preemption_state; |
---|
46 | |
---|
47 | #if defined(__SIZEOF_INT128__) |
---|
48 | __uint128_t rand_seed; |
---|
49 | #else |
---|
50 | uint64_t rand_seed; |
---|
51 | #endif |
---|
52 | struct { |
---|
53 | uint64_t fwd_seed; |
---|
54 | uint64_t bck_seed; |
---|
55 | } ready_rng; |
---|
56 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | static inline uint64_t __tls_rand() { |
---|
61 | #if defined(__SIZEOF_INT128__) |
---|
62 | return __lehmer64( kernelTLS.rand_seed ); |
---|
63 | #else |
---|
64 | return __xorshift64( kernelTLS.rand_seed ); |
---|
65 | #endif |
---|
66 | } |
---|
67 | |
---|
68 | #define M (1_l64u << 48_l64u) |
---|
69 | #define A (25214903917_l64u) |
---|
70 | #define AI (18446708753438544741_l64u) |
---|
71 | #define C (11_l64u) |
---|
72 | #define D (16_l64u) |
---|
73 | |
---|
74 | static inline unsigned __tls_rand_fwd() { |
---|
75 | |
---|
76 | kernelTLS.ready_rng.fwd_seed = (A * kernelTLS.ready_rng.fwd_seed + C) & (M - 1); |
---|
77 | return kernelTLS.ready_rng.fwd_seed >> D; |
---|
78 | } |
---|
79 | |
---|
80 | static inline unsigned __tls_rand_bck() { |
---|
81 | unsigned int r = kernelTLS.ready_rng.bck_seed >> D; |
---|
82 | kernelTLS.ready_rng.bck_seed = AI * (kernelTLS.ready_rng.bck_seed - C) & (M - 1); |
---|
83 | return r; |
---|
84 | } |
---|
85 | |
---|
86 | #undef M |
---|
87 | #undef A |
---|
88 | #undef AI |
---|
89 | #undef C |
---|
90 | #undef D |
---|
91 | |
---|
92 | static inline void __tls_rand_advance_bck(void) { |
---|
93 | kernelTLS.ready_rng.bck_seed = kernelTLS.ready_rng.fwd_seed; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | #if 0 // def __ARM_ARCH |
---|
98 | // function prototypes are only really used by these macros on ARM |
---|
99 | void disable_global_interrupts(); |
---|
100 | void enable_global_interrupts(); |
---|
101 | |
---|
102 | #define TL_GET( member ) ( { __typeof__( kernelTLS.member ) target; \ |
---|
103 | disable_global_interrupts(); \ |
---|
104 | target = kernelTLS.member; \ |
---|
105 | enable_global_interrupts(); \ |
---|
106 | target; } ) |
---|
107 | #define TL_SET( member, value ) disable_global_interrupts(); \ |
---|
108 | kernelTLS.member = value; \ |
---|
109 | enable_global_interrupts(); |
---|
110 | #else |
---|
111 | #define TL_GET( member ) kernelTLS.member |
---|
112 | #define TL_SET( member, value ) kernelTLS.member = value; |
---|
113 | #endif |
---|
114 | |
---|
115 | extern void disable_interrupts(); |
---|
116 | extern void enable_interrupts_noPoll(); |
---|
117 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
118 | |
---|
119 | extern "Cforall" { |
---|
120 | extern void park( void ); |
---|
121 | extern void unpark( struct $thread * this ); |
---|
122 | static inline struct $thread * active_thread () { return TL_GET( this_thread ); } |
---|
123 | |
---|
124 | extern bool force_yield( enum __Preemption_Reason ); |
---|
125 | |
---|
126 | static inline void yield() { |
---|
127 | force_yield(__MANUAL_PREEMPTION); |
---|
128 | } |
---|
129 | |
---|
130 | // Yield: yield N times |
---|
131 | static inline void yield( unsigned times ) { |
---|
132 | for( times ) { |
---|
133 | yield(); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | //----------------------------------------------------------------------- |
---|
138 | // Statics call at the end of each thread to register statistics |
---|
139 | #if !defined(__CFA_NO_STATISTICS__) |
---|
140 | static inline struct __stats_t * __tls_stats() { |
---|
141 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
142 | /* paranoid */ verify( kernelTLS.this_stats ); |
---|
143 | return kernelTLS.this_stats; |
---|
144 | } |
---|
145 | |
---|
146 | #define __STATS__(in_kernel, ...) { \ |
---|
147 | if( !(in_kernel) ) disable_interrupts(); \ |
---|
148 | with( *__tls_stats() ) { \ |
---|
149 | __VA_ARGS__ \ |
---|
150 | } \ |
---|
151 | if( !(in_kernel) ) enable_interrupts( __cfaabi_dbg_ctx ); \ |
---|
152 | } |
---|
153 | #else |
---|
154 | #define __STATS__(in_kernel, ...) |
---|
155 | #endif |
---|
156 | } |
---|
157 | } |
---|
158 | #endif |
---|