[e660761] | 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 | |
---|
[3e2b9c9] | 16 | #pragma once |
---|
| 17 | |
---|
[e660761] | 18 | #include "bits/defs.hfa" |
---|
| 19 | #include "bits/debug.hfa" |
---|
| 20 | |
---|
[3e2b9c9] | 21 | #ifdef __cforall |
---|
| 22 | #include "bits/random.hfa" |
---|
[e660761] | 23 | #endif |
---|
| 24 | |
---|
| 25 | struct $thread; |
---|
| 26 | struct processor; |
---|
| 27 | struct cluster; |
---|
| 28 | |
---|
[3e2b9c9] | 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 | |
---|
[e660761] | 33 | #ifdef __cforall |
---|
| 34 | extern "C" { |
---|
[3e2b9c9] | 35 | extern "Cforall" { |
---|
[e660761] | 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 |
---|
[f2384c9a] | 52 | struct { |
---|
| 53 | uint64_t fwd_seed; |
---|
| 54 | uint64_t bck_seed; |
---|
| 55 | } ready_rng; |
---|
[e660761] | 56 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); |
---|
[3e2b9c9] | 57 | |
---|
[f2384c9a] | 58 | |
---|
| 59 | |
---|
[3e2b9c9] | 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 | } |
---|
[f2384c9a] | 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 | } |
---|
[e660761] | 95 | } |
---|
| 96 | |
---|
[3e2b9c9] | 97 | #ifdef __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(); |
---|
[e660761] | 117 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
| 118 | |
---|
[3e2b9c9] | 119 | extern "Cforall" { |
---|
| 120 | extern void park( __cfaabi_dbg_ctx_param ); |
---|
| 121 | extern void unpark( struct $thread * this __cfaabi_dbg_ctx_param2 ); |
---|
| 122 | static inline struct $thread * active_thread () { return TL_GET( this_thread ); } |
---|
| 123 | |
---|
| 124 | extern bool force_yield( enum __Preemption_Reason ); |
---|
[e660761] | 125 | |
---|
[3e2b9c9] | 126 | static inline void yield() { |
---|
| 127 | force_yield(__MANUAL_PREEMPTION); |
---|
| 128 | } |
---|
[e660761] | 129 | |
---|
[3e2b9c9] | 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 | } |
---|
[e660761] | 157 | } |
---|
| 158 | #endif |
---|