[8def349] | 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 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[597c34a3] | 12 | // Last Modified On : Sat May 19 08:23:21 2018 |
---|
| 13 | // Update Count : 31 |
---|
[8def349] | 14 | // |
---|
| 15 | |
---|
[73abe95] | 16 | #include "bits/containers.hfa" |
---|
| 17 | #include "bits/defs.hfa" |
---|
| 18 | #include "bits/locks.hfa" |
---|
[78b3f52] | 19 | |
---|
[0cf5b79] | 20 | #ifdef __cforall |
---|
[5c81105] | 21 | extern "C" { |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #if ! defined(__CFA_INVOKE_PRIVATE__) |
---|
[78b3f52] | 25 | #ifndef _INVOKE_H_ |
---|
| 26 | #define _INVOKE_H_ |
---|
| 27 | |
---|
[597c34a3] | 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 | |
---|
[0cf5b79] | 46 | #ifdef __cforall |
---|
[025278e] | 47 | extern "Cforall" { |
---|
[0cf5b79] | 48 | static inline struct thread_desc * & get_next( struct thread_desc & this ); |
---|
| 49 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); |
---|
[b10affd] | 50 | |
---|
| 51 | extern thread_local struct KernelThreadData { |
---|
| 52 | struct thread_desc * volatile this_thread; |
---|
| 53 | struct processor * volatile this_processor; |
---|
| 54 | |
---|
| 55 | struct { |
---|
| 56 | volatile unsigned short disable_count; |
---|
| 57 | volatile bool enabled; |
---|
| 58 | volatile bool in_progress; |
---|
| 59 | } preemption_state; |
---|
[afc2427] | 60 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); |
---|
[025278e] | 61 | } |
---|
[4c1b48f3] | 62 | #endif |
---|
[025278e] | 63 | |
---|
[b2f6113] | 64 | struct __stack_context_t { |
---|
| 65 | void * SP; |
---|
| 66 | |
---|
| 67 | void * FP; |
---|
| 68 | // copy of global UNIX variable errno |
---|
| 69 | int errno_; |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | // low adresses : +----------------------+ <- start of allocation |
---|
| 73 | // | optional guard page | |
---|
| 74 | // +----------------------+ <- __stack_t.limit |
---|
| 75 | // | | |
---|
| 76 | // | /\ /\ /\ | |
---|
| 77 | // | || || || | |
---|
| 78 | // | | |
---|
| 79 | // | program stack | |
---|
| 80 | // | | |
---|
| 81 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base |
---|
| 82 | // | __stack_t | |
---|
| 83 | // high adresses : +----------------------+ <- end of allocation |
---|
| 84 | |
---|
| 85 | struct __stack_t { |
---|
| 86 | // size of stack |
---|
| 87 | size_t size; |
---|
| 88 | |
---|
| 89 | // stack grows towards stack limit |
---|
| 90 | void * limit; |
---|
| 91 | |
---|
| 92 | // base of stack |
---|
| 93 | void * base; |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | struct __stack_info_t { |
---|
| 97 | // pointer to stack |
---|
| 98 | struct __stack_t * storage; |
---|
[025278e] | 99 | }; |
---|
| 100 | |
---|
| 101 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; |
---|
| 102 | |
---|
| 103 | struct coroutine_desc { |
---|
[b2f6113] | 104 | // context that is switch during a CtxSwitch |
---|
| 105 | struct __stack_context_t context; |
---|
| 106 | |
---|
[76e069f] | 107 | // stack information of the coroutine |
---|
[b2f6113] | 108 | struct __stack_info_t stack; |
---|
[76e069f] | 109 | |
---|
| 110 | // textual name for coroutine/task, initialized by uC++ generated code |
---|
| 111 | const char * name; |
---|
| 112 | |
---|
| 113 | // current execution status for coroutine |
---|
| 114 | enum coroutine_state state; |
---|
[b2f6113] | 115 | |
---|
[76e069f] | 116 | // first coroutine to resume this one |
---|
| 117 | struct coroutine_desc * starter; |
---|
| 118 | |
---|
| 119 | // last coroutine to resume this one |
---|
| 120 | struct coroutine_desc * last; |
---|
| 121 | |
---|
| 122 | // If non-null stack must be unwound with this exception |
---|
| 123 | struct _Unwind_Exception * cancellation; |
---|
| 124 | |
---|
[025278e] | 125 | }; |
---|
| 126 | |
---|
[f23b685] | 127 | // struct which calls the monitor is accepting |
---|
[025278e] | 128 | struct __waitfor_mask_t { |
---|
| 129 | // the index of the accepted function, -1 if none |
---|
| 130 | short * accepted; |
---|
| 131 | |
---|
| 132 | // list of acceptable functions, null if any |
---|
[b1672e1] | 133 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); |
---|
[025278e] | 134 | }; |
---|
| 135 | |
---|
| 136 | struct monitor_desc { |
---|
| 137 | // spinlock to protect internal data |
---|
[ea7d2b0] | 138 | struct __spinlock_t lock; |
---|
[025278e] | 139 | |
---|
| 140 | // current owner of the monitor |
---|
| 141 | struct thread_desc * owner; |
---|
| 142 | |
---|
| 143 | // queue of threads that are blocked waiting for the monitor |
---|
[0cf5b79] | 144 | __queue_t(struct thread_desc) entry_queue; |
---|
[025278e] | 145 | |
---|
| 146 | // stack of conditions to run next once we exit the monitor |
---|
[0cf5b79] | 147 | __stack_t(struct __condition_criterion_t) signal_stack; |
---|
[025278e] | 148 | |
---|
| 149 | // monitor routines can be called recursively, we need to keep track of that |
---|
| 150 | unsigned int recursion; |
---|
| 151 | |
---|
| 152 | // mask used to know if some thread is waiting for something while holding the monitor |
---|
| 153 | struct __waitfor_mask_t mask; |
---|
| 154 | |
---|
| 155 | // node used to signal the dtor in a waitfor dtor |
---|
| 156 | struct __condition_node_t * dtor_node; |
---|
| 157 | }; |
---|
| 158 | |
---|
| 159 | struct __monitor_group_t { |
---|
| 160 | // currently held monitors |
---|
[b1672e1] | 161 | __cfa_anonymous_object( __small_array_t(monitor_desc*) ); |
---|
[025278e] | 162 | |
---|
| 163 | // last function that acquired monitors |
---|
[c1a9c86] | 164 | fptr_t func; |
---|
[025278e] | 165 | }; |
---|
| 166 | |
---|
| 167 | struct thread_desc { |
---|
| 168 | // Core threading fields |
---|
| 169 | // coroutine body used to store context |
---|
| 170 | struct coroutine_desc self_cor; |
---|
| 171 | |
---|
[82c948c] | 172 | // current active context |
---|
| 173 | struct coroutine_desc * curr_cor; |
---|
| 174 | |
---|
[025278e] | 175 | // monitor body used for mutual exclusion |
---|
| 176 | struct monitor_desc self_mon; |
---|
| 177 | |
---|
| 178 | // pointer to monitor with sufficient lifetime for current monitors |
---|
| 179 | struct monitor_desc * self_mon_p; |
---|
| 180 | |
---|
[de6319f] | 181 | // pointer to the cluster on which the thread is running |
---|
| 182 | struct cluster * curr_cluster; |
---|
| 183 | |
---|
[025278e] | 184 | // monitors currently held by this thread |
---|
| 185 | struct __monitor_group_t monitors; |
---|
| 186 | |
---|
| 187 | // Link lists fields |
---|
| 188 | // instrusive link field for threads |
---|
| 189 | struct thread_desc * next; |
---|
[f7d6bb0] | 190 | |
---|
[de94a60] | 191 | struct { |
---|
| 192 | struct thread_desc * next; |
---|
| 193 | struct thread_desc * prev; |
---|
| 194 | } node; |
---|
[212c2187] | 195 | }; |
---|
| 196 | |
---|
| 197 | #ifdef __cforall |
---|
| 198 | extern "Cforall" { |
---|
| 199 | static inline struct coroutine_desc * volatile active_coroutine() { return TL_GET( this_thread )->curr_cor; } |
---|
| 200 | static inline struct thread_desc * volatile active_thread () { return TL_GET( this_thread ); } |
---|
| 201 | static inline struct processor * volatile active_processor() { return TL_GET( this_processor ); } // UNSAFE |
---|
[8118303] | 202 | |
---|
[0cf5b79] | 203 | static inline thread_desc * & get_next( thread_desc & this ) { |
---|
| 204 | return this.next; |
---|
| 205 | } |
---|
| 206 | |
---|
[de94a60] | 207 | static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { |
---|
| 208 | return this.node.[next, prev]; |
---|
| 209 | } |
---|
| 210 | |
---|
[0cf5b79] | 211 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); |
---|
| 212 | |
---|
| 213 | static inline void ?{}(__monitor_group_t & this) { |
---|
| 214 | (this.data){NULL}; |
---|
| 215 | (this.size){0}; |
---|
| 216 | (this.func){NULL}; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { |
---|
| 220 | (this.data){data}; |
---|
| 221 | (this.size){size}; |
---|
| 222 | (this.func){func}; |
---|
[025278e] | 223 | } |
---|
| 224 | |
---|
| 225 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { |
---|
[0cf5b79] | 226 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; |
---|
[025278e] | 227 | if( lhs.size != rhs.size ) return false; |
---|
| 228 | if( lhs.func != rhs.func ) return false; |
---|
| 229 | |
---|
| 230 | // Check that all the monitors match |
---|
| 231 | for( int i = 0; i < lhs.size; i++ ) { |
---|
| 232 | // If not a match, check next function |
---|
| 233 | if( lhs[i] != rhs[i] ) return false; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | return true; |
---|
| 237 | } |
---|
[0cf5b79] | 238 | |
---|
| 239 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { |
---|
| 240 | lhs.data = rhs.data; |
---|
| 241 | lhs.size = rhs.size; |
---|
| 242 | lhs.func = rhs.func; |
---|
| 243 | } |
---|
[025278e] | 244 | } |
---|
| 245 | #endif |
---|
[b18830e] | 246 | |
---|
[5c81105] | 247 | #endif //_INVOKE_H_ |
---|
| 248 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 249 | #ifndef _INVOKE_PRIVATE_H_ |
---|
| 250 | #define _INVOKE_PRIVATE_H_ |
---|
[b227f68] | 251 | |
---|
[025278e] | 252 | struct machine_context_t { |
---|
| 253 | void *SP; |
---|
| 254 | void *FP; |
---|
| 255 | void *PC; |
---|
| 256 | }; |
---|
| 257 | |
---|
| 258 | // assembler routines that performs the context switch |
---|
| 259 | extern void CtxInvokeStub( void ); |
---|
[b2f6113] | 260 | void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch"); |
---|
[212c2187] | 261 | // void CtxStore ( void * this ) asm ("CtxStore"); |
---|
| 262 | // void CtxRet ( void * dst ) asm ("CtxRet"); |
---|
[025278e] | 263 | |
---|
[381fdee] | 264 | #if defined( __i386 ) |
---|
[025278e] | 265 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 266 | "movl %%esp,%0\n" \ |
---|
| 267 | "movl %%ebp,%1\n" \ |
---|
| 268 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[381fdee] | 269 | #elif defined( __x86_64 ) |
---|
| 270 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 271 | "movq %%rsp,%0\n" \ |
---|
| 272 | "movq %%rbp,%1\n" \ |
---|
| 273 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[b158d8f] | 274 | #elif defined( __ARM_ARCH ) |
---|
| 275 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 276 | "mov %0,%%sp\n" \ |
---|
| 277 | "mov %1,%%r11\n" \ |
---|
| 278 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[381fdee] | 279 | #else |
---|
| 280 | #error unknown hardware architecture |
---|
[025278e] | 281 | #endif |
---|
[78b3f52] | 282 | |
---|
[5c81105] | 283 | #endif //_INVOKE_PRIVATE_H_ |
---|
| 284 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
[0cf5b79] | 285 | #ifdef __cforall |
---|
[5c81105] | 286 | } |
---|
| 287 | #endif |
---|
[6b0b624] | 288 | |
---|
| 289 | // Local Variables: // |
---|
| 290 | // mode: c // |
---|
| 291 | // tab-width: 4 // |
---|
| 292 | // End: // |
---|