| [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 | 
|---|
| [09d4b22] | 12 | // Last Modified On : Thu Dec  5 16:26:03 2019 | 
|---|
|  | 13 | // Update Count     : 44 | 
|---|
| [8def349] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [73abe95] | 16 | #include "bits/containers.hfa" | 
|---|
|  | 17 | #include "bits/defs.hfa" | 
|---|
|  | 18 | #include "bits/locks.hfa" | 
|---|
| [3e2b9c9] | 19 | #include "kernel/fwd.hfa" | 
|---|
| [78b3f52] | 20 |  | 
|---|
| [0cf5b79] | 21 | #ifdef __cforall | 
|---|
| [5c81105] | 22 | extern "C" { | 
|---|
|  | 23 | #endif | 
|---|
|  | 24 |  | 
|---|
|  | 25 | #if ! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| [78b3f52] | 26 | #ifndef _INVOKE_H_ | 
|---|
|  | 27 | #define _INVOKE_H_ | 
|---|
|  | 28 |  | 
|---|
| [b2f6113] | 29 | struct __stack_context_t { | 
|---|
|  | 30 | void * SP; | 
|---|
|  | 31 | void * FP; | 
|---|
|  | 32 | }; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | // low adresses  :           +----------------------+ <- start of allocation | 
|---|
|  | 35 | //                           |  optional guard page | | 
|---|
|  | 36 | //                           +----------------------+ <- __stack_t.limit | 
|---|
|  | 37 | //                           |                      | | 
|---|
|  | 38 | //                           |       /\ /\ /\       | | 
|---|
|  | 39 | //                           |       || || ||       | | 
|---|
|  | 40 | //                           |                      | | 
|---|
|  | 41 | //                           |    program  stack    | | 
|---|
|  | 42 | //                           |                      | | 
|---|
|  | 43 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base | 
|---|
|  | 44 | //                           |      __stack_t       | | 
|---|
|  | 45 | // high adresses :           +----------------------+ <- end of allocation | 
|---|
|  | 46 |  | 
|---|
|  | 47 | struct __stack_t { | 
|---|
|  | 48 | // stack grows towards stack limit | 
|---|
|  | 49 | void * limit; | 
|---|
|  | 50 |  | 
|---|
|  | 51 | // base of stack | 
|---|
|  | 52 | void * base; | 
|---|
|  | 53 | }; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | struct __stack_info_t { | 
|---|
|  | 56 | // pointer to stack | 
|---|
|  | 57 | struct __stack_t * storage; | 
|---|
| [025278e] | 58 | }; | 
|---|
|  | 59 |  | 
|---|
| [ff79d5e] | 60 | enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active }; | 
|---|
| [025278e] | 61 |  | 
|---|
| [ac2b598] | 62 | struct $coroutine { | 
|---|
| [c7a900a] | 63 | // context that is switch during a __cfactx_switch | 
|---|
| [b2f6113] | 64 | struct __stack_context_t context; | 
|---|
|  | 65 |  | 
|---|
| [76e069f] | 66 | // stack information of the coroutine | 
|---|
| [b2f6113] | 67 | struct __stack_info_t stack; | 
|---|
| [76e069f] | 68 |  | 
|---|
| [e8e457e] | 69 | // textual name for coroutine/task | 
|---|
| [76e069f] | 70 | const char * name; | 
|---|
|  | 71 |  | 
|---|
|  | 72 | // current execution status for coroutine | 
|---|
| [ff79d5e] | 73 | enum __Coroutine_State state; | 
|---|
| [b2f6113] | 74 |  | 
|---|
| [76e069f] | 75 | // first coroutine to resume this one | 
|---|
| [ac2b598] | 76 | struct $coroutine * starter; | 
|---|
| [76e069f] | 77 |  | 
|---|
|  | 78 | // last coroutine to resume this one | 
|---|
| [ac2b598] | 79 | struct $coroutine * last; | 
|---|
| [76e069f] | 80 |  | 
|---|
|  | 81 | // If non-null stack must be unwound with this exception | 
|---|
|  | 82 | struct _Unwind_Exception * cancellation; | 
|---|
|  | 83 |  | 
|---|
| [025278e] | 84 | }; | 
|---|
|  | 85 |  | 
|---|
| [210b8b3] | 86 | static inline struct __stack_t * __get_stack( struct $coroutine * cor ) { return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); } | 
|---|
|  | 87 |  | 
|---|
| [f23b685] | 88 | // struct which calls the monitor is accepting | 
|---|
| [025278e] | 89 | struct __waitfor_mask_t { | 
|---|
|  | 90 | // the index of the accepted function, -1 if none | 
|---|
|  | 91 | short * accepted; | 
|---|
|  | 92 |  | 
|---|
|  | 93 | // list of acceptable functions, null if any | 
|---|
| [b1672e1] | 94 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); | 
|---|
| [025278e] | 95 | }; | 
|---|
|  | 96 |  | 
|---|
| [ac2b598] | 97 | struct $monitor { | 
|---|
| [025278e] | 98 | // spinlock to protect internal data | 
|---|
| [ea7d2b0] | 99 | struct __spinlock_t lock; | 
|---|
| [025278e] | 100 |  | 
|---|
|  | 101 | // current owner of the monitor | 
|---|
| [ac2b598] | 102 | struct $thread * owner; | 
|---|
| [025278e] | 103 |  | 
|---|
|  | 104 | // queue of threads that are blocked waiting for the monitor | 
|---|
| [ac2b598] | 105 | __queue_t(struct $thread) entry_queue; | 
|---|
| [025278e] | 106 |  | 
|---|
|  | 107 | // stack of conditions to run next once we exit the monitor | 
|---|
| [0cf5b79] | 108 | __stack_t(struct __condition_criterion_t) signal_stack; | 
|---|
| [025278e] | 109 |  | 
|---|
|  | 110 | // monitor routines can be called recursively, we need to keep track of that | 
|---|
|  | 111 | unsigned int recursion; | 
|---|
|  | 112 |  | 
|---|
|  | 113 | // mask used to know if some thread is waiting for something while holding the monitor | 
|---|
|  | 114 | struct __waitfor_mask_t mask; | 
|---|
|  | 115 |  | 
|---|
|  | 116 | // node used to signal the dtor in a waitfor dtor | 
|---|
|  | 117 | struct __condition_node_t * dtor_node; | 
|---|
|  | 118 | }; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | struct __monitor_group_t { | 
|---|
|  | 121 | // currently held monitors | 
|---|
| [ac2b598] | 122 | __cfa_anonymous_object( __small_array_t($monitor*) ); | 
|---|
| [025278e] | 123 |  | 
|---|
|  | 124 | // last function that acquired monitors | 
|---|
| [c1a9c86] | 125 | fptr_t func; | 
|---|
| [025278e] | 126 | }; | 
|---|
|  | 127 |  | 
|---|
| [b798713] | 128 | // Link lists fields | 
|---|
|  | 129 | // instrusive link field for threads | 
|---|
|  | 130 | struct __thread_desc_link { | 
|---|
| [6a490b2] | 131 | struct $thread * next; | 
|---|
|  | 132 | struct $thread * prev; | 
|---|
| [1b143de] | 133 | volatile unsigned long long ts; | 
|---|
| [d72c074] | 134 | int preferred; | 
|---|
| [b798713] | 135 | }; | 
|---|
|  | 136 |  | 
|---|
| [ac2b598] | 137 | struct $thread { | 
|---|
| [025278e] | 138 | // Core threading fields | 
|---|
| [c7a900a] | 139 | // context that is switch during a __cfactx_switch | 
|---|
| [e8e457e] | 140 | struct __stack_context_t context; | 
|---|
|  | 141 |  | 
|---|
|  | 142 | // current execution status for coroutine | 
|---|
| [ff79d5e] | 143 | volatile int ticket; | 
|---|
|  | 144 | enum __Coroutine_State state:8; | 
|---|
|  | 145 | enum __Preemption_Reason preempted:8; | 
|---|
| [e8e457e] | 146 |  | 
|---|
| [5c1a531] | 147 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it | 
|---|
|  | 148 |  | 
|---|
| [37ba662] | 149 | // pointer to the cluster on which the thread is running | 
|---|
|  | 150 | struct cluster * curr_cluster; | 
|---|
|  | 151 |  | 
|---|
|  | 152 | // Link lists fields | 
|---|
|  | 153 | // instrusive link field for threads | 
|---|
|  | 154 | struct __thread_desc_link link; | 
|---|
|  | 155 |  | 
|---|
| [025278e] | 156 | // coroutine body used to store context | 
|---|
| [ac2b598] | 157 | struct $coroutine  self_cor; | 
|---|
| [025278e] | 158 |  | 
|---|
| [82c948c] | 159 | // current active context | 
|---|
| [ac2b598] | 160 | struct $coroutine * curr_cor; | 
|---|
| [82c948c] | 161 |  | 
|---|
| [025278e] | 162 | // monitor body used for mutual exclusion | 
|---|
| [ac2b598] | 163 | struct $monitor    self_mon; | 
|---|
| [025278e] | 164 |  | 
|---|
|  | 165 | // pointer to monitor with sufficient lifetime for current monitors | 
|---|
| [ac2b598] | 166 | struct $monitor *  self_mon_p; | 
|---|
| [025278e] | 167 |  | 
|---|
|  | 168 | // monitors currently held by this thread | 
|---|
|  | 169 | struct __monitor_group_t monitors; | 
|---|
|  | 170 |  | 
|---|
| [de94a60] | 171 | struct { | 
|---|
| [ac2b598] | 172 | struct $thread * next; | 
|---|
|  | 173 | struct $thread * prev; | 
|---|
| [de94a60] | 174 | } node; | 
|---|
| [ae66348] | 175 |  | 
|---|
|  | 176 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 177 | // previous function to park/unpark the thread | 
|---|
| [ae7be7a] | 178 | const char * park_caller; | 
|---|
| [ff79d5e] | 179 | int park_result; | 
|---|
|  | 180 | enum __Coroutine_State park_state; | 
|---|
| [ae66348] | 181 | bool park_stale; | 
|---|
| [ae7be7a] | 182 | const char * unpark_caller; | 
|---|
| [ff79d5e] | 183 | int unpark_result; | 
|---|
|  | 184 | enum __Coroutine_State unpark_state; | 
|---|
| [ae66348] | 185 | bool unpark_stale; | 
|---|
|  | 186 | #endif | 
|---|
| [212c2187] | 187 | }; | 
|---|
|  | 188 |  | 
|---|
| [ae66348] | 189 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 190 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]); | 
|---|
|  | 191 | #else | 
|---|
|  | 192 | #define __cfaabi_dbg_record_thrd(x, y, z) | 
|---|
|  | 193 | #endif | 
|---|
|  | 194 |  | 
|---|
| [212c2187] | 195 | #ifdef __cforall | 
|---|
|  | 196 | extern "Cforall" { | 
|---|
| [6a490b2] | 197 |  | 
|---|
| [ac2b598] | 198 | static inline $thread *& get_next( $thread & this ) __attribute__((const)) { | 
|---|
| [b798713] | 199 | return this.link.next; | 
|---|
| [0cf5b79] | 200 | } | 
|---|
|  | 201 |  | 
|---|
| [ac2b598] | 202 | static inline [$thread *&, $thread *& ] __get( $thread & this ) __attribute__((const)) { | 
|---|
| [de94a60] | 203 | return this.node.[next, prev]; | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
| [0cf5b79] | 206 | static inline void ?{}(__monitor_group_t & this) { | 
|---|
| [121be3e] | 207 | (this.data){0p}; | 
|---|
| [0cf5b79] | 208 | (this.size){0}; | 
|---|
|  | 209 | (this.func){NULL}; | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
| [ac2b598] | 212 | static inline void ?{}(__monitor_group_t & this, struct $monitor ** data, __lock_size_t size, fptr_t func) { | 
|---|
| [0cf5b79] | 213 | (this.data){data}; | 
|---|
|  | 214 | (this.size){size}; | 
|---|
|  | 215 | (this.func){func}; | 
|---|
| [025278e] | 216 | } | 
|---|
|  | 217 |  | 
|---|
| [8c50aed] | 218 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) { | 
|---|
| [0cf5b79] | 219 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; | 
|---|
| [025278e] | 220 | if( lhs.size != rhs.size ) return false; | 
|---|
|  | 221 | if( lhs.func != rhs.func ) return false; | 
|---|
|  | 222 |  | 
|---|
|  | 223 | // Check that all the monitors match | 
|---|
|  | 224 | for( int i = 0; i < lhs.size; i++ ) { | 
|---|
|  | 225 | // If not a match, check next function | 
|---|
|  | 226 | if( lhs[i] != rhs[i] ) return false; | 
|---|
|  | 227 | } | 
|---|
|  | 228 |  | 
|---|
|  | 229 | return true; | 
|---|
|  | 230 | } | 
|---|
| [0cf5b79] | 231 |  | 
|---|
|  | 232 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { | 
|---|
|  | 233 | lhs.data = rhs.data; | 
|---|
|  | 234 | lhs.size = rhs.size; | 
|---|
|  | 235 | lhs.func = rhs.func; | 
|---|
|  | 236 | } | 
|---|
| [025278e] | 237 | } | 
|---|
|  | 238 | #endif | 
|---|
| [b18830e] | 239 |  | 
|---|
| [5c81105] | 240 | #endif //_INVOKE_H_ | 
|---|
|  | 241 | #else //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
|  | 242 | #ifndef _INVOKE_PRIVATE_H_ | 
|---|
|  | 243 | #define _INVOKE_PRIVATE_H_ | 
|---|
| [b227f68] | 244 |  | 
|---|
| [025278e] | 245 | struct machine_context_t { | 
|---|
|  | 246 | void *SP; | 
|---|
|  | 247 | void *FP; | 
|---|
|  | 248 | void *PC; | 
|---|
|  | 249 | }; | 
|---|
|  | 250 |  | 
|---|
|  | 251 | // assembler routines that performs the context switch | 
|---|
| [c7a900a] | 252 | extern void __cfactx_invoke_stub( void ); | 
|---|
|  | 253 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); | 
|---|
| [212c2187] | 254 | // void CtxStore ( void * this ) asm ("CtxStore"); | 
|---|
|  | 255 | // void CtxRet   ( void * dst  ) asm ("CtxRet"); | 
|---|
| [025278e] | 256 |  | 
|---|
| [5c81105] | 257 | #endif //_INVOKE_PRIVATE_H_ | 
|---|
|  | 258 | #endif //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| [0cf5b79] | 259 | #ifdef __cforall | 
|---|
| [5c81105] | 260 | } | 
|---|
|  | 261 | #endif | 
|---|
| [6b0b624] | 262 |  | 
|---|
|  | 263 | // Local Variables: // | 
|---|
|  | 264 | // mode: c // | 
|---|
|  | 265 | // tab-width: 4 // | 
|---|
|  | 266 | // End: // | 
|---|