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