[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 |
---|
[c19ca4b] | 12 | // Last Modified On : Tue Mar 14 13:39:31 2023 |
---|
| 13 | // Update Count : 59 |
---|
[8def349] | 14 | // |
---|
| 15 | |
---|
[c19ca4b] | 16 | // No not use #pragma once was this file is included twice in some places. It has its own guard system. |
---|
[5e4a830] | 17 | |
---|
[73abe95] | 18 | #include "bits/containers.hfa" |
---|
| 19 | #include "bits/defs.hfa" |
---|
| 20 | #include "bits/locks.hfa" |
---|
[9fce2572] | 21 | #include "bits/random.hfa" |
---|
[3e2b9c9] | 22 | #include "kernel/fwd.hfa" |
---|
[78b3f52] | 23 | |
---|
[0cf5b79] | 24 | #ifdef __cforall |
---|
[82f4063] | 25 | #include "containers/list.hfa" |
---|
[5c81105] | 26 | extern "C" { |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | #if ! defined(__CFA_INVOKE_PRIVATE__) |
---|
[78b3f52] | 30 | #ifndef _INVOKE_H_ |
---|
| 31 | #define _INVOKE_H_ |
---|
| 32 | |
---|
[eaf269d] | 33 | enum { DEFAULT_STACK_SIZE = 65000 }; |
---|
| 34 | |
---|
[80ec409] | 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 | |
---|
[b2f6113] | 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; |
---|
[80ec409] | 66 | |
---|
| 67 | // Information for exception handling. |
---|
| 68 | struct exception_context_t exception_context; |
---|
[b2f6113] | 69 | }; |
---|
| 70 | |
---|
| 71 | struct __stack_info_t { |
---|
| 72 | // pointer to stack |
---|
| 73 | struct __stack_t * storage; |
---|
[025278e] | 74 | }; |
---|
| 75 | |
---|
[3ea8ad1] | 76 | enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active, Cancelled, Halting }; |
---|
[025278e] | 77 | |
---|
[e84ab3d] | 78 | struct coroutine$ { |
---|
[c7a900a] | 79 | // context that is switch during a __cfactx_switch |
---|
[b2f6113] | 80 | struct __stack_context_t context; |
---|
| 81 | |
---|
[76e069f] | 82 | // stack information of the coroutine |
---|
[b2f6113] | 83 | struct __stack_info_t stack; |
---|
[76e069f] | 84 | |
---|
[e8e457e] | 85 | // textual name for coroutine/task |
---|
[76e069f] | 86 | const char * name; |
---|
| 87 | |
---|
| 88 | // current execution status for coroutine |
---|
[ff79d5e] | 89 | enum __Coroutine_State state; |
---|
[b2f6113] | 90 | |
---|
[76e069f] | 91 | // first coroutine to resume this one |
---|
[e84ab3d] | 92 | struct coroutine$ * starter; |
---|
[76e069f] | 93 | |
---|
| 94 | // last coroutine to resume this one |
---|
[e84ab3d] | 95 | struct coroutine$ * last; |
---|
[76e069f] | 96 | |
---|
| 97 | // If non-null stack must be unwound with this exception |
---|
| 98 | struct _Unwind_Exception * cancellation; |
---|
| 99 | |
---|
[025278e] | 100 | }; |
---|
[038110a] | 101 | // Wrapper for gdb |
---|
[e84ab3d] | 102 | struct cfathread_coroutine_t { struct coroutine$ debug; }; |
---|
[025278e] | 103 | |
---|
[e84ab3d] | 104 | static inline struct __stack_t * __get_stack( struct coroutine$ * cor ) { |
---|
[80ec409] | 105 | return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); |
---|
| 106 | } |
---|
[210b8b3] | 107 | |
---|
[f23b685] | 108 | // struct which calls the monitor is accepting |
---|
[025278e] | 109 | struct __waitfor_mask_t { |
---|
| 110 | // the index of the accepted function, -1 if none |
---|
| 111 | short * accepted; |
---|
| 112 | |
---|
| 113 | // list of acceptable functions, null if any |
---|
[b1672e1] | 114 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); |
---|
[025278e] | 115 | }; |
---|
| 116 | |
---|
[e84ab3d] | 117 | struct monitor$ { |
---|
[025278e] | 118 | // spinlock to protect internal data |
---|
[ea7d2b0] | 119 | struct __spinlock_t lock; |
---|
[025278e] | 120 | |
---|
| 121 | // current owner of the monitor |
---|
[e84ab3d] | 122 | struct thread$ * owner; |
---|
[025278e] | 123 | |
---|
| 124 | // queue of threads that are blocked waiting for the monitor |
---|
[e84ab3d] | 125 | __queue_t(struct thread$) entry_queue; |
---|
[025278e] | 126 | |
---|
| 127 | // stack of conditions to run next once we exit the monitor |
---|
[0cf5b79] | 128 | __stack_t(struct __condition_criterion_t) signal_stack; |
---|
[025278e] | 129 | |
---|
| 130 | // monitor routines can be called recursively, we need to keep track of that |
---|
| 131 | unsigned int recursion; |
---|
| 132 | |
---|
| 133 | // mask used to know if some thread is waiting for something while holding the monitor |
---|
| 134 | struct __waitfor_mask_t mask; |
---|
| 135 | |
---|
| 136 | // node used to signal the dtor in a waitfor dtor |
---|
| 137 | struct __condition_node_t * dtor_node; |
---|
| 138 | }; |
---|
[038110a] | 139 | // Wrapper for gdb |
---|
[e84ab3d] | 140 | struct cfathread_monitor_t { struct monitor$ debug; }; |
---|
[025278e] | 141 | |
---|
| 142 | struct __monitor_group_t { |
---|
| 143 | // currently held monitors |
---|
[e84ab3d] | 144 | __cfa_anonymous_object( __small_array_t(monitor$*) ); |
---|
[025278e] | 145 | |
---|
| 146 | // last function that acquired monitors |
---|
[c1a9c86] | 147 | fptr_t func; |
---|
[025278e] | 148 | }; |
---|
| 149 | |
---|
[b798713] | 150 | // Link lists fields |
---|
[639e4fc] | 151 | // instrusive link field for threads in the ready-queue |
---|
[b798713] | 152 | struct __thread_desc_link { |
---|
[e84ab3d] | 153 | struct thread$ * next; |
---|
[1b143de] | 154 | volatile unsigned long long ts; |
---|
[b798713] | 155 | }; |
---|
| 156 | |
---|
[639e4fc] | 157 | // Link lists fields |
---|
| 158 | // instrusive link field for threads in the user_link/cltr_link |
---|
| 159 | struct __thread_user_link { |
---|
| 160 | #ifdef __cforall |
---|
| 161 | inline dlink(thread$); |
---|
| 162 | #else |
---|
| 163 | struct thread$ * next; struct thread$ * back; |
---|
| 164 | #endif |
---|
| 165 | }; |
---|
| 166 | _Static_assert(sizeof(struct __thread_user_link) == 2 * sizeof(struct thread$ *), "__thread_user_link should be consistent in C and Cforall"); |
---|
| 167 | |
---|
[e84ab3d] | 168 | struct thread$ { |
---|
[025278e] | 169 | // Core threading fields |
---|
[c7a900a] | 170 | // context that is switch during a __cfactx_switch |
---|
[e8e457e] | 171 | struct __stack_context_t context; |
---|
| 172 | |
---|
[d3ba775] | 173 | // Link lists fields |
---|
| 174 | // instrusive link field for threads |
---|
[15c93d8] | 175 | struct __thread_desc_link rdy_link; |
---|
[d3ba775] | 176 | |
---|
[e8e457e] | 177 | // current execution status for coroutine |
---|
[6a77224] | 178 | // Possible values are: |
---|
| 179 | // - TICKET_BLOCKED (-1) thread is blocked |
---|
| 180 | // - TICKET_RUNNING ( 0) thread is running |
---|
| 181 | // - TICKET_UNBLOCK ( 1) thread should ignore next block |
---|
[ff79d5e] | 182 | volatile int ticket; |
---|
| 183 | enum __Coroutine_State state:8; |
---|
| 184 | enum __Preemption_Reason preempted:8; |
---|
[e8e457e] | 185 | |
---|
[ab5baab] | 186 | bool corctx_flag; |
---|
| 187 | |
---|
[5c1a531] | 188 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it |
---|
| 189 | |
---|
[37ba662] | 190 | // pointer to the cluster on which the thread is running |
---|
| 191 | struct cluster * curr_cluster; |
---|
| 192 | |
---|
[24e321c] | 193 | // preferred ready-queue or CPU |
---|
[d3ba775] | 194 | unsigned preferred; |
---|
[37ba662] | 195 | |
---|
[025278e] | 196 | // coroutine body used to store context |
---|
[e84ab3d] | 197 | struct coroutine$ self_cor; |
---|
[025278e] | 198 | |
---|
[82c948c] | 199 | // current active context |
---|
[e84ab3d] | 200 | struct coroutine$ * curr_cor; |
---|
[82c948c] | 201 | |
---|
[025278e] | 202 | // monitor body used for mutual exclusion |
---|
[e84ab3d] | 203 | struct monitor$ self_mon; |
---|
[025278e] | 204 | |
---|
| 205 | // pointer to monitor with sufficient lifetime for current monitors |
---|
[e84ab3d] | 206 | struct monitor$ * self_mon_p; |
---|
[025278e] | 207 | |
---|
| 208 | // monitors currently held by this thread |
---|
| 209 | struct __monitor_group_t monitors; |
---|
| 210 | |
---|
[cd5b58f] | 211 | // intrusive link fields, used for locks, monitors and any user defined data structure |
---|
| 212 | // default link fields for dlist |
---|
[639e4fc] | 213 | struct __thread_user_link user_link; |
---|
[82f4063] | 214 | |
---|
[cd5b58f] | 215 | // secondary intrusive link fields, used for global cluster list |
---|
| 216 | // default link fields for dlist |
---|
[639e4fc] | 217 | struct __thread_user_link cltr_link; |
---|
[ae66348] | 218 | |
---|
[f835806] | 219 | // used to point to this thd's current clh node |
---|
| 220 | volatile bool * clh_node; |
---|
| 221 | |
---|
[89eff25] | 222 | struct processor * last_proc; |
---|
| 223 | |
---|
[dd46fd3] | 224 | PRNG_STATE_T random_state; // fast random numbers |
---|
[2210cfc] | 225 | |
---|
[b4b63e8] | 226 | #if defined( __CFA_WITH_VERIFY__ ) |
---|
[878cfcc] | 227 | struct processor * volatile executing; |
---|
[ac12f1f] | 228 | void * canary; |
---|
[ae66348] | 229 | #endif |
---|
[212c2187] | 230 | }; |
---|
[2856982c] | 231 | |
---|
[038110a] | 232 | // Wrapper for gdb |
---|
[e84ab3d] | 233 | struct cfathread_thread_t { struct thread$ debug; }; |
---|
[212c2187] | 234 | |
---|
[ae66348] | 235 | #ifdef __CFA_DEBUG__ |
---|
[e84ab3d] | 236 | void __cfaabi_dbg_record_thrd(thread$ & this, bool park, const char prev_name[]); |
---|
[ae66348] | 237 | #else |
---|
| 238 | #define __cfaabi_dbg_record_thrd(x, y, z) |
---|
| 239 | #endif |
---|
| 240 | |
---|
[212c2187] | 241 | #ifdef __cforall |
---|
| 242 | extern "Cforall" { |
---|
[cd5b58f] | 243 | static inline thread$ * volatile & ?`next ( thread$ * this ) { |
---|
| 244 | return this->user_link.next; |
---|
| 245 | } |
---|
[6a490b2] | 246 | |
---|
[e84ab3d] | 247 | static inline thread$ *& get_next( thread$ & this ) __attribute__((const)) { |
---|
[be5f0a5] | 248 | return this.user_link.next; |
---|
[0cf5b79] | 249 | } |
---|
| 250 | |
---|
[2856982c] | 251 | static inline tytagref( dlink(thread$), dlink(thread$) ) ?`inner( thread$ & this ) { |
---|
| 252 | dlink(thread$) & b = this.user_link; |
---|
| 253 | tytagref( dlink(thread$), dlink(thread$) ) result = { b }; |
---|
| 254 | return result; |
---|
| 255 | } |
---|
| 256 | |
---|
[639e4fc] | 257 | static inline tytagref(struct __thread_user_link, dlink(thread$)) ?`inner( struct thread$ & this ) { |
---|
| 258 | struct __thread_user_link & ib = this.cltr_link; |
---|
[6a4ef0c] | 259 | dlink(thread$) & b = ib`inner; |
---|
[639e4fc] | 260 | tytagref(struct __thread_user_link, dlink(thread$)) result = { b }; |
---|
[6a4ef0c] | 261 | return result; |
---|
| 262 | } |
---|
| 263 | |
---|
[639e4fc] | 264 | P9_EMBEDDED(struct __thread_user_link, dlink(thread$)) |
---|
[cd5b58f] | 265 | |
---|
[0cf5b79] | 266 | static inline void ?{}(__monitor_group_t & this) { |
---|
[121be3e] | 267 | (this.data){0p}; |
---|
[0cf5b79] | 268 | (this.size){0}; |
---|
| 269 | (this.func){NULL}; |
---|
| 270 | } |
---|
| 271 | |
---|
[e84ab3d] | 272 | static inline void ?{}(__monitor_group_t & this, struct monitor$ ** data, __lock_size_t size, fptr_t func) { |
---|
[0cf5b79] | 273 | (this.data){data}; |
---|
| 274 | (this.size){size}; |
---|
| 275 | (this.func){func}; |
---|
[025278e] | 276 | } |
---|
| 277 | |
---|
[8c50aed] | 278 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) { |
---|
[0cf5b79] | 279 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; |
---|
[025278e] | 280 | if( lhs.size != rhs.size ) return false; |
---|
| 281 | if( lhs.func != rhs.func ) return false; |
---|
| 282 | |
---|
| 283 | // Check that all the monitors match |
---|
| 284 | for( int i = 0; i < lhs.size; i++ ) { |
---|
| 285 | // If not a match, check next function |
---|
| 286 | if( lhs[i] != rhs[i] ) return false; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | return true; |
---|
| 290 | } |
---|
[0cf5b79] | 291 | |
---|
| 292 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { |
---|
| 293 | lhs.data = rhs.data; |
---|
| 294 | lhs.size = rhs.size; |
---|
| 295 | lhs.func = rhs.func; |
---|
| 296 | } |
---|
[025278e] | 297 | } |
---|
| 298 | #endif |
---|
[b18830e] | 299 | |
---|
[5c81105] | 300 | #endif //_INVOKE_H_ |
---|
| 301 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 302 | #ifndef _INVOKE_PRIVATE_H_ |
---|
| 303 | #define _INVOKE_PRIVATE_H_ |
---|
[b227f68] | 304 | |
---|
[025278e] | 305 | struct machine_context_t { |
---|
| 306 | void *SP; |
---|
| 307 | void *FP; |
---|
| 308 | void *PC; |
---|
| 309 | }; |
---|
| 310 | |
---|
| 311 | // assembler routines that performs the context switch |
---|
[c7a900a] | 312 | extern void __cfactx_invoke_stub( void ); |
---|
| 313 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); |
---|
[212c2187] | 314 | // void CtxStore ( void * this ) asm ("CtxStore"); |
---|
| 315 | // void CtxRet ( void * dst ) asm ("CtxRet"); |
---|
[025278e] | 316 | |
---|
[5c81105] | 317 | #endif //_INVOKE_PRIVATE_H_ |
---|
| 318 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
[0cf5b79] | 319 | #ifdef __cforall |
---|
[5c81105] | 320 | } |
---|
| 321 | #endif |
---|
[6b0b624] | 322 | |
---|
| 323 | // Local Variables: // |
---|
| 324 | // mode: c // |
---|
| 325 | // tab-width: 4 // |
---|
| 326 | // End: // |
---|