| 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 : Tue Mar 14 13:39:31 2023 | 
|---|
| 13 | // Update Count     : 59 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | // No not use #pragma once was this file is included twice in some places. It has its own guard system. | 
|---|
| 17 |  | 
|---|
| 18 | #include "bits/containers.hfa" | 
|---|
| 19 | #include "bits/defs.hfa" | 
|---|
| 20 | #include "bits/locks.hfa" | 
|---|
| 21 | #include "bits/random.hfa" | 
|---|
| 22 | #include "kernel/fwd.hfa" | 
|---|
| 23 |  | 
|---|
| 24 | #ifdef __cforall | 
|---|
| 25 | #include "containers/list.hfa" | 
|---|
| 26 | extern "C" { | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 | #if ! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| 30 | #ifndef _INVOKE_H_ | 
|---|
| 31 | #define _INVOKE_H_ | 
|---|
| 32 |  | 
|---|
| 33 | enum { DEFAULT_STACK_SIZE = 65000 }; | 
|---|
| 34 |  | 
|---|
| 35 | struct __cfaehm_try_resume_node; | 
|---|
| 36 | struct __cfaehm_base_exception_t; | 
|---|
| 37 | struct exception_context_t { | 
|---|
| 38 | struct __cfaehm_try_resume_node * top_resume; | 
|---|
| 39 | struct __cfaehm_base_exception_t * current_exception; | 
|---|
| 40 | }; | 
|---|
| 41 |  | 
|---|
| 42 | struct __stack_context_t { | 
|---|
| 43 | void * SP; | 
|---|
| 44 | void * FP; | 
|---|
| 45 | }; | 
|---|
| 46 |  | 
|---|
| 47 | // low adresses  :           +----------------------+ <- start of allocation | 
|---|
| 48 | //                           |  optional guard page | | 
|---|
| 49 | //                           +----------------------+ <- __stack_t.limit | 
|---|
| 50 | //                           |                      | | 
|---|
| 51 | //                           |       /\ /\ /\       | | 
|---|
| 52 | //                           |       || || ||       | | 
|---|
| 53 | //                           |                      | | 
|---|
| 54 | //                           |    program  stack    | | 
|---|
| 55 | //                           |                      | | 
|---|
| 56 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base | 
|---|
| 57 | //                           |      __stack_t       | | 
|---|
| 58 | // high adresses :           +----------------------+ <- end of allocation | 
|---|
| 59 |  | 
|---|
| 60 | struct __stack_t { | 
|---|
| 61 | // stack grows towards stack limit | 
|---|
| 62 | void * limit; | 
|---|
| 63 |  | 
|---|
| 64 | // base of stack | 
|---|
| 65 | void * base; | 
|---|
| 66 |  | 
|---|
| 67 | // Information for exception handling. | 
|---|
| 68 | struct exception_context_t exception_context; | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | struct __stack_info_t { | 
|---|
| 72 | // pointer to stack | 
|---|
| 73 | struct __stack_t * storage; | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 | struct nonlocal_ehm { | 
|---|
| 77 | // list of pending nonlocal exceptions | 
|---|
| 78 | __queue_t(struct nonlocal_exception) ehm_buffer; | 
|---|
| 79 |  | 
|---|
| 80 | // lock to protect the buffer | 
|---|
| 81 | struct __spinlock_t buffer_lock; | 
|---|
| 82 |  | 
|---|
| 83 | // enable/disabled flag | 
|---|
| 84 | bool ehm_enabled; | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active, Cancelled, Halting }; | 
|---|
| 88 |  | 
|---|
| 89 | struct coroutine$ { | 
|---|
| 90 | // context that is switch during a __cfactx_switch | 
|---|
| 91 | struct __stack_context_t context; | 
|---|
| 92 |  | 
|---|
| 93 | // stack information of the coroutine | 
|---|
| 94 | struct __stack_info_t stack; | 
|---|
| 95 |  | 
|---|
| 96 | // textual name for coroutine/task | 
|---|
| 97 | const char * name; | 
|---|
| 98 |  | 
|---|
| 99 | // current execution status for coroutine | 
|---|
| 100 | enum __Coroutine_State state; | 
|---|
| 101 |  | 
|---|
| 102 | // first coroutine to resume this one | 
|---|
| 103 | struct coroutine$ * starter; | 
|---|
| 104 |  | 
|---|
| 105 | // last coroutine to resume this one | 
|---|
| 106 | struct coroutine$ * last; | 
|---|
| 107 |  | 
|---|
| 108 | // If non-null stack must be unwound with this exception | 
|---|
| 109 | struct _Unwind_Exception * cancellation; | 
|---|
| 110 |  | 
|---|
| 111 | // Non-local exception handling information | 
|---|
| 112 | struct nonlocal_ehm ehm_state; | 
|---|
| 113 | }; | 
|---|
| 114 | // Wrapper for gdb | 
|---|
| 115 | struct cfathread_coroutine_t { struct coroutine$ debug; }; | 
|---|
| 116 |  | 
|---|
| 117 | static inline struct __stack_t * __get_stack( struct coroutine$ * cor ) { | 
|---|
| 118 | return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // struct which calls the monitor is accepting | 
|---|
| 122 | struct __waitfor_mask_t { | 
|---|
| 123 | // the index of the accepted function, -1 if none | 
|---|
| 124 | short * accepted; | 
|---|
| 125 |  | 
|---|
| 126 | // list of acceptable functions, null if any | 
|---|
| 127 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 | struct monitor$ { | 
|---|
| 131 | // spinlock to protect internal data | 
|---|
| 132 | struct __spinlock_t lock; | 
|---|
| 133 |  | 
|---|
| 134 | // current owner of the monitor | 
|---|
| 135 | struct thread$ * owner; | 
|---|
| 136 |  | 
|---|
| 137 | // queue of threads that are blocked waiting for the monitor | 
|---|
| 138 | __queue_t(struct thread$) entry_queue; | 
|---|
| 139 |  | 
|---|
| 140 | // stack of conditions to run next once we exit the monitor | 
|---|
| 141 | __stack_t(struct __condition_criterion_t) signal_stack; | 
|---|
| 142 |  | 
|---|
| 143 | // monitor routines can be called recursively, we need to keep track of that | 
|---|
| 144 | unsigned int recursion; | 
|---|
| 145 |  | 
|---|
| 146 | // mask used to know if some thread is waiting for something while holding the monitor | 
|---|
| 147 | struct __waitfor_mask_t mask; | 
|---|
| 148 |  | 
|---|
| 149 | // node used to signal the dtor in a waitfor dtor | 
|---|
| 150 | struct __condition_node_t * dtor_node; | 
|---|
| 151 | }; | 
|---|
| 152 | // Wrapper for gdb | 
|---|
| 153 | struct cfathread_monitor_t { struct monitor$ debug; }; | 
|---|
| 154 |  | 
|---|
| 155 | struct __monitor_group_t { | 
|---|
| 156 | // currently held monitors | 
|---|
| 157 | __cfa_anonymous_object( __small_array_t(monitor$*) ); | 
|---|
| 158 |  | 
|---|
| 159 | // last function that acquired monitors | 
|---|
| 160 | fptr_t func; | 
|---|
| 161 | }; | 
|---|
| 162 |  | 
|---|
| 163 | // Link lists fields | 
|---|
| 164 | // instrusive link field for threads in the ready-queue | 
|---|
| 165 | struct __thread_desc_link { | 
|---|
| 166 | struct thread$ * next; | 
|---|
| 167 | volatile unsigned long long ts; | 
|---|
| 168 | }; | 
|---|
| 169 |  | 
|---|
| 170 | // Link lists fields | 
|---|
| 171 | // instrusive link field for threads in the user_link/cltr_link | 
|---|
| 172 | struct __thread_user_link { | 
|---|
| 173 | #ifdef __cforall | 
|---|
| 174 | inline dlink(thread$); | 
|---|
| 175 | #else | 
|---|
| 176 | struct thread$ * next; struct thread$ * back; | 
|---|
| 177 | #endif | 
|---|
| 178 | }; | 
|---|
| 179 | _Static_assert(sizeof(struct __thread_user_link) == 2 * sizeof(struct thread$ *), "__thread_user_link should be consistent in C and Cforall"); | 
|---|
| 180 |  | 
|---|
| 181 | struct thread$ { | 
|---|
| 182 | // Core threading fields | 
|---|
| 183 | // context that is switch during a __cfactx_switch | 
|---|
| 184 | struct __stack_context_t context; | 
|---|
| 185 |  | 
|---|
| 186 | // Link lists fields | 
|---|
| 187 | // instrusive link field for threads | 
|---|
| 188 | struct __thread_desc_link rdy_link; | 
|---|
| 189 |  | 
|---|
| 190 | // current execution status for coroutine | 
|---|
| 191 | // Possible values are: | 
|---|
| 192 | //    - TICKET_BLOCKED (-1) thread is blocked | 
|---|
| 193 | //    - TICKET_RUNNING ( 0) thread is running | 
|---|
| 194 | //    - TICKET_UNBLOCK ( 1) thread should ignore next block | 
|---|
| 195 | volatile int ticket; | 
|---|
| 196 | enum __Coroutine_State state:8; | 
|---|
| 197 | enum __Preemption_Reason preempted:8; | 
|---|
| 198 |  | 
|---|
| 199 | bool corctx_flag; | 
|---|
| 200 |  | 
|---|
| 201 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it | 
|---|
| 202 |  | 
|---|
| 203 | // pointer to the cluster on which the thread is running | 
|---|
| 204 | struct cluster * curr_cluster; | 
|---|
| 205 |  | 
|---|
| 206 | // preferred ready-queue or CPU | 
|---|
| 207 | unsigned preferred; | 
|---|
| 208 |  | 
|---|
| 209 | // coroutine body used to store context | 
|---|
| 210 | struct coroutine$  self_cor; | 
|---|
| 211 |  | 
|---|
| 212 | // current active context | 
|---|
| 213 | struct coroutine$ * curr_cor; | 
|---|
| 214 |  | 
|---|
| 215 | // monitor body used for mutual exclusion | 
|---|
| 216 | struct monitor$    self_mon; | 
|---|
| 217 |  | 
|---|
| 218 | // pointer to monitor with sufficient lifetime for current monitors | 
|---|
| 219 | struct monitor$ *  self_mon_p; | 
|---|
| 220 |  | 
|---|
| 221 | // monitors currently held by this thread | 
|---|
| 222 | struct __monitor_group_t monitors; | 
|---|
| 223 |  | 
|---|
| 224 | // intrusive link fields, used for locks, monitors and any user defined data structure | 
|---|
| 225 | // default link fields for dlist | 
|---|
| 226 | struct __thread_user_link user_link; | 
|---|
| 227 |  | 
|---|
| 228 | // secondary intrusive link fields, used for global cluster list | 
|---|
| 229 | // default link fields for dlist | 
|---|
| 230 | struct __thread_user_link cltr_link; | 
|---|
| 231 |  | 
|---|
| 232 | struct processor * last_proc; | 
|---|
| 233 |  | 
|---|
| 234 | // ptr used during handover between blocking lists to allow for stack allocation of intrusive nodes | 
|---|
| 235 | // main use case is wait-morphing to allow a different node to be used to block on condvar vs lock | 
|---|
| 236 | void * link_node; | 
|---|
| 237 |  | 
|---|
| 238 | PRNG_STATE_T random_state;                                              // fast random numbers | 
|---|
| 239 |  | 
|---|
| 240 | #if defined( __CFA_WITH_VERIFY__ ) | 
|---|
| 241 | struct processor * volatile executing; | 
|---|
| 242 | void * canary; | 
|---|
| 243 | #endif | 
|---|
| 244 | }; | 
|---|
| 245 |  | 
|---|
| 246 | // Wrapper for gdb | 
|---|
| 247 | struct cfathread_thread_t { struct thread$ debug; }; | 
|---|
| 248 |  | 
|---|
| 249 | #ifdef __CFA_DEBUG__ | 
|---|
| 250 | void __cfaabi_dbg_record_thrd(thread$ & this, bool park, const char prev_name[]); | 
|---|
| 251 | #else | 
|---|
| 252 | #define __cfaabi_dbg_record_thrd(x, y, z) | 
|---|
| 253 | #endif | 
|---|
| 254 |  | 
|---|
| 255 | #ifdef __cforall | 
|---|
| 256 | extern "Cforall" { | 
|---|
| 257 | static inline bool exception_in_flight() { | 
|---|
| 258 | return __get_stack( &active_thread()->self_cor )->exception_context.current_exception != 0p; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | static inline thread$ * volatile & ?`next ( thread$ * this ) { | 
|---|
| 262 | return this->user_link.next; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | static inline thread$ *& get_next( thread$ & this ) __attribute__((const)) { | 
|---|
| 266 | return this.user_link.next; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | static inline tytagref( dlink(thread$), dlink(thread$) ) ?`inner( thread$ & this ) { | 
|---|
| 270 | dlink(thread$) & b = this.user_link; | 
|---|
| 271 | tytagref( dlink(thread$), dlink(thread$) ) result = { b }; | 
|---|
| 272 | return result; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | static inline tytagref(struct __thread_user_link, dlink(thread$)) ?`inner( struct thread$ & this ) { | 
|---|
| 276 | struct __thread_user_link & ib = this.cltr_link; | 
|---|
| 277 | dlink(thread$) & b = ib`inner; | 
|---|
| 278 | tytagref(struct __thread_user_link, dlink(thread$)) result = { b }; | 
|---|
| 279 | return result; | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | P9_EMBEDDED(struct __thread_user_link, dlink(thread$)) | 
|---|
| 283 |  | 
|---|
| 284 | static inline void ?{}(__monitor_group_t & this) { | 
|---|
| 285 | (this.data){0p}; | 
|---|
| 286 | (this.size){0}; | 
|---|
| 287 | (this.func){NULL}; | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | static inline void ?{}(__monitor_group_t & this, struct monitor$ ** data, __lock_size_t size, fptr_t func) { | 
|---|
| 291 | (this.data){data}; | 
|---|
| 292 | (this.size){size}; | 
|---|
| 293 | (this.func){func}; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) { | 
|---|
| 297 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; | 
|---|
| 298 | if( lhs.size != rhs.size ) return false; | 
|---|
| 299 | if( lhs.func != rhs.func ) return false; | 
|---|
| 300 |  | 
|---|
| 301 | // Check that all the monitors match | 
|---|
| 302 | for( int i = 0; i < lhs.size; i++ ) { | 
|---|
| 303 | // If not a match, check next function | 
|---|
| 304 | if( lhs[i] != rhs[i] ) return false; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | return true; | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { | 
|---|
| 311 | lhs.data = rhs.data; | 
|---|
| 312 | lhs.size = rhs.size; | 
|---|
| 313 | lhs.func = rhs.func; | 
|---|
| 314 | } | 
|---|
| 315 | } | 
|---|
| 316 | #endif | 
|---|
| 317 |  | 
|---|
| 318 | #endif //_INVOKE_H_ | 
|---|
| 319 | #else //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| 320 | #ifndef _INVOKE_PRIVATE_H_ | 
|---|
| 321 | #define _INVOKE_PRIVATE_H_ | 
|---|
| 322 |  | 
|---|
| 323 | struct machine_context_t { | 
|---|
| 324 | void *SP; | 
|---|
| 325 | void *FP; | 
|---|
| 326 | void *PC; | 
|---|
| 327 | }; | 
|---|
| 328 |  | 
|---|
| 329 | // assembler routines that performs the context switch | 
|---|
| 330 | extern void __cfactx_invoke_stub( void ); | 
|---|
| 331 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); | 
|---|
| 332 | // void CtxStore ( void * this ) asm ("CtxStore"); | 
|---|
| 333 | // void CtxRet   ( void * dst  ) asm ("CtxRet"); | 
|---|
| 334 |  | 
|---|
| 335 | #endif //_INVOKE_PRIVATE_H_ | 
|---|
| 336 | #endif //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| 337 | #ifdef __cforall | 
|---|
| 338 | } | 
|---|
| 339 | #endif | 
|---|
| 340 |  | 
|---|
| 341 | // Local Variables: // | 
|---|
| 342 | // mode: c // | 
|---|
| 343 | // tab-width: 4 // | 
|---|
| 344 | // End: // | 
|---|