| [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" | 
|---|
| [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" { | 
|---|
| [21184e3] | 48 | extern __attribute__((aligned(128))) thread_local struct KernelThreadData { | 
|---|
| [b10affd] | 49 | struct thread_desc    * volatile this_thread; | 
|---|
|  | 50 | struct processor      * volatile this_processor; | 
|---|
|  | 51 |  | 
|---|
|  | 52 | struct { | 
|---|
|  | 53 | volatile unsigned short disable_count; | 
|---|
|  | 54 | volatile bool enabled; | 
|---|
|  | 55 | volatile bool in_progress; | 
|---|
|  | 56 | } preemption_state; | 
|---|
| [21184e3] | 57 |  | 
|---|
|  | 58 | uint32_t rand_seed; | 
|---|
| [afc2427] | 59 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); | 
|---|
| [025278e] | 60 | } | 
|---|
| [4c1b48f3] | 61 | #endif | 
|---|
| [025278e] | 62 |  | 
|---|
| [b2f6113] | 63 | struct __stack_context_t { | 
|---|
|  | 64 | void * SP; | 
|---|
|  | 65 | void * FP; | 
|---|
|  | 66 | }; | 
|---|
|  | 67 |  | 
|---|
|  | 68 | // low adresses  :           +----------------------+ <- start of allocation | 
|---|
|  | 69 | //                           |  optional guard page | | 
|---|
|  | 70 | //                           +----------------------+ <- __stack_t.limit | 
|---|
|  | 71 | //                           |                      | | 
|---|
|  | 72 | //                           |       /\ /\ /\       | | 
|---|
|  | 73 | //                           |       || || ||       | | 
|---|
|  | 74 | //                           |                      | | 
|---|
|  | 75 | //                           |    program  stack    | | 
|---|
|  | 76 | //                           |                      | | 
|---|
|  | 77 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base | 
|---|
|  | 78 | //                           |      __stack_t       | | 
|---|
|  | 79 | // high adresses :           +----------------------+ <- end of allocation | 
|---|
|  | 80 |  | 
|---|
|  | 81 | struct __stack_t { | 
|---|
|  | 82 | // stack grows towards stack limit | 
|---|
|  | 83 | void * limit; | 
|---|
|  | 84 |  | 
|---|
|  | 85 | // base of stack | 
|---|
|  | 86 | void * base; | 
|---|
|  | 87 | }; | 
|---|
|  | 88 |  | 
|---|
|  | 89 | struct __stack_info_t { | 
|---|
|  | 90 | // pointer to stack | 
|---|
|  | 91 | struct __stack_t * storage; | 
|---|
| [025278e] | 92 | }; | 
|---|
|  | 93 |  | 
|---|
| [63364d8] | 94 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; | 
|---|
| [025278e] | 95 |  | 
|---|
|  | 96 | struct coroutine_desc { | 
|---|
| [b2f6113] | 97 | // context that is switch during a CtxSwitch | 
|---|
|  | 98 | struct __stack_context_t context; | 
|---|
|  | 99 |  | 
|---|
| [76e069f] | 100 | // stack information of the coroutine | 
|---|
| [b2f6113] | 101 | struct __stack_info_t stack; | 
|---|
| [76e069f] | 102 |  | 
|---|
| [e8e457e] | 103 | // textual name for coroutine/task | 
|---|
| [76e069f] | 104 | const char * name; | 
|---|
|  | 105 |  | 
|---|
|  | 106 | // current execution status for coroutine | 
|---|
|  | 107 | enum coroutine_state state; | 
|---|
| [b2f6113] | 108 |  | 
|---|
| [76e069f] | 109 | // first coroutine to resume this one | 
|---|
|  | 110 | struct coroutine_desc * starter; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | // last coroutine to resume this one | 
|---|
|  | 113 | struct coroutine_desc * last; | 
|---|
|  | 114 |  | 
|---|
|  | 115 | // If non-null stack must be unwound with this exception | 
|---|
|  | 116 | struct _Unwind_Exception * cancellation; | 
|---|
|  | 117 |  | 
|---|
| [025278e] | 118 | }; | 
|---|
|  | 119 |  | 
|---|
| [f23b685] | 120 | // struct which calls the monitor is accepting | 
|---|
| [025278e] | 121 | struct __waitfor_mask_t { | 
|---|
|  | 122 | // the index of the accepted function, -1 if none | 
|---|
|  | 123 | short * accepted; | 
|---|
|  | 124 |  | 
|---|
|  | 125 | // list of acceptable functions, null if any | 
|---|
| [b1672e1] | 126 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); | 
|---|
| [025278e] | 127 | }; | 
|---|
|  | 128 |  | 
|---|
|  | 129 | struct monitor_desc { | 
|---|
|  | 130 | // spinlock to protect internal data | 
|---|
| [ea7d2b0] | 131 | struct __spinlock_t lock; | 
|---|
| [025278e] | 132 |  | 
|---|
|  | 133 | // current owner of the monitor | 
|---|
|  | 134 | struct thread_desc * owner; | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // queue of threads that are blocked waiting for the monitor | 
|---|
| [0cf5b79] | 137 | __queue_t(struct thread_desc) entry_queue; | 
|---|
| [025278e] | 138 |  | 
|---|
|  | 139 | // stack of conditions to run next once we exit the monitor | 
|---|
| [0cf5b79] | 140 | __stack_t(struct __condition_criterion_t) signal_stack; | 
|---|
| [025278e] | 141 |  | 
|---|
|  | 142 | // monitor routines can be called recursively, we need to keep track of that | 
|---|
|  | 143 | unsigned int recursion; | 
|---|
|  | 144 |  | 
|---|
|  | 145 | // mask used to know if some thread is waiting for something while holding the monitor | 
|---|
|  | 146 | struct __waitfor_mask_t mask; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | // node used to signal the dtor in a waitfor dtor | 
|---|
|  | 149 | struct __condition_node_t * dtor_node; | 
|---|
|  | 150 | }; | 
|---|
|  | 151 |  | 
|---|
|  | 152 | struct __monitor_group_t { | 
|---|
|  | 153 | // currently held monitors | 
|---|
| [b1672e1] | 154 | __cfa_anonymous_object( __small_array_t(monitor_desc*) ); | 
|---|
| [025278e] | 155 |  | 
|---|
|  | 156 | // last function that acquired monitors | 
|---|
| [c1a9c86] | 157 | fptr_t func; | 
|---|
| [025278e] | 158 | }; | 
|---|
|  | 159 |  | 
|---|
|  | 160 | struct thread_desc { | 
|---|
|  | 161 | // Core threading fields | 
|---|
| [e8e457e] | 162 | // context that is switch during a CtxSwitch | 
|---|
|  | 163 | struct __stack_context_t context; | 
|---|
|  | 164 |  | 
|---|
|  | 165 | // current execution status for coroutine | 
|---|
|  | 166 | enum coroutine_state state; | 
|---|
|  | 167 |  | 
|---|
| [5c1a531] | 168 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it | 
|---|
|  | 169 |  | 
|---|
| [025278e] | 170 | // coroutine body used to store context | 
|---|
|  | 171 | struct coroutine_desc  self_cor; | 
|---|
|  | 172 |  | 
|---|
| [82c948c] | 173 | // current active context | 
|---|
|  | 174 | struct coroutine_desc * curr_cor; | 
|---|
|  | 175 |  | 
|---|
| [025278e] | 176 | // monitor body used for mutual exclusion | 
|---|
|  | 177 | struct monitor_desc    self_mon; | 
|---|
|  | 178 |  | 
|---|
|  | 179 | // pointer to monitor with sufficient lifetime for current monitors | 
|---|
|  | 180 | struct monitor_desc *  self_mon_p; | 
|---|
|  | 181 |  | 
|---|
| [de6319f] | 182 | // pointer to the cluster on which the thread is running | 
|---|
|  | 183 | struct cluster * curr_cluster; | 
|---|
|  | 184 |  | 
|---|
| [025278e] | 185 | // monitors currently held by this thread | 
|---|
|  | 186 | struct __monitor_group_t monitors; | 
|---|
|  | 187 |  | 
|---|
|  | 188 | // Link lists fields | 
|---|
|  | 189 | // instrusive link field for threads | 
|---|
|  | 190 | struct thread_desc * next; | 
|---|
| [f7d6bb0] | 191 |  | 
|---|
| [de94a60] | 192 | struct { | 
|---|
|  | 193 | struct thread_desc * next; | 
|---|
|  | 194 | struct thread_desc * prev; | 
|---|
|  | 195 | } node; | 
|---|
| [212c2187] | 196 | }; | 
|---|
|  | 197 |  | 
|---|
|  | 198 | #ifdef __cforall | 
|---|
|  | 199 | extern "Cforall" { | 
|---|
| [d4e68a6] | 200 | static inline thread_desc *& get_next( thread_desc & this ) { | 
|---|
| [0cf5b79] | 201 | return this.next; | 
|---|
|  | 202 | } | 
|---|
|  | 203 |  | 
|---|
| [de94a60] | 204 | static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { | 
|---|
|  | 205 | return this.node.[next, prev]; | 
|---|
|  | 206 | } | 
|---|
|  | 207 |  | 
|---|
| [0cf5b79] | 208 | static inline void ?{}(__monitor_group_t & this) { | 
|---|
| [121be3e] | 209 | (this.data){0p}; | 
|---|
| [0cf5b79] | 210 | (this.size){0}; | 
|---|
|  | 211 | (this.func){NULL}; | 
|---|
|  | 212 | } | 
|---|
|  | 213 |  | 
|---|
|  | 214 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { | 
|---|
|  | 215 | (this.data){data}; | 
|---|
|  | 216 | (this.size){size}; | 
|---|
|  | 217 | (this.func){func}; | 
|---|
| [025278e] | 218 | } | 
|---|
|  | 219 |  | 
|---|
|  | 220 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { | 
|---|
| [0cf5b79] | 221 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; | 
|---|
| [025278e] | 222 | if( lhs.size != rhs.size ) return false; | 
|---|
|  | 223 | if( lhs.func != rhs.func ) return false; | 
|---|
|  | 224 |  | 
|---|
|  | 225 | // Check that all the monitors match | 
|---|
|  | 226 | for( int i = 0; i < lhs.size; i++ ) { | 
|---|
|  | 227 | // If not a match, check next function | 
|---|
|  | 228 | if( lhs[i] != rhs[i] ) return false; | 
|---|
|  | 229 | } | 
|---|
|  | 230 |  | 
|---|
|  | 231 | return true; | 
|---|
|  | 232 | } | 
|---|
| [0cf5b79] | 233 |  | 
|---|
|  | 234 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { | 
|---|
|  | 235 | lhs.data = rhs.data; | 
|---|
|  | 236 | lhs.size = rhs.size; | 
|---|
|  | 237 | lhs.func = rhs.func; | 
|---|
|  | 238 | } | 
|---|
| [025278e] | 239 | } | 
|---|
|  | 240 | #endif | 
|---|
| [b18830e] | 241 |  | 
|---|
| [5c81105] | 242 | #endif //_INVOKE_H_ | 
|---|
|  | 243 | #else //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
|  | 244 | #ifndef _INVOKE_PRIVATE_H_ | 
|---|
|  | 245 | #define _INVOKE_PRIVATE_H_ | 
|---|
| [b227f68] | 246 |  | 
|---|
| [025278e] | 247 | struct machine_context_t { | 
|---|
|  | 248 | void *SP; | 
|---|
|  | 249 | void *FP; | 
|---|
|  | 250 | void *PC; | 
|---|
|  | 251 | }; | 
|---|
|  | 252 |  | 
|---|
|  | 253 | // assembler routines that performs the context switch | 
|---|
|  | 254 | extern void CtxInvokeStub( void ); | 
|---|
| [3c06bba] | 255 | extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch"); | 
|---|
| [212c2187] | 256 | // void CtxStore ( void * this ) asm ("CtxStore"); | 
|---|
|  | 257 | // void CtxRet   ( void * dst  ) asm ("CtxRet"); | 
|---|
| [025278e] | 258 |  | 
|---|
| [5c81105] | 259 | #endif //_INVOKE_PRIVATE_H_ | 
|---|
|  | 260 | #endif //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| [0cf5b79] | 261 | #ifdef __cforall | 
|---|
| [5c81105] | 262 | } | 
|---|
|  | 263 | #endif | 
|---|
| [6b0b624] | 264 |  | 
|---|
|  | 265 | // Local Variables: // | 
|---|
|  | 266 | // mode: c // | 
|---|
|  | 267 | // tab-width: 4 // | 
|---|
|  | 268 | // End: // | 
|---|