[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 |
---|
[d4e68a6] | 12 | // Last Modified On : Sat Jun 22 18:19:13 2019 |
---|
| 13 | // Update Count : 40 |
---|
[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" { |
---|
[b10affd] | 48 | extern thread_local struct KernelThreadData { |
---|
| 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; |
---|
[afc2427] | 57 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); |
---|
[025278e] | 58 | } |
---|
[4c1b48f3] | 59 | #endif |
---|
[025278e] | 60 | |
---|
[b2f6113] | 61 | struct __stack_context_t { |
---|
| 62 | void * SP; |
---|
| 63 | void * FP; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | // low adresses : +----------------------+ <- start of allocation |
---|
| 67 | // | optional guard page | |
---|
| 68 | // +----------------------+ <- __stack_t.limit |
---|
| 69 | // | | |
---|
| 70 | // | /\ /\ /\ | |
---|
| 71 | // | || || || | |
---|
| 72 | // | | |
---|
| 73 | // | program stack | |
---|
| 74 | // | | |
---|
| 75 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base |
---|
| 76 | // | __stack_t | |
---|
| 77 | // high adresses : +----------------------+ <- end of allocation |
---|
| 78 | |
---|
| 79 | struct __stack_t { |
---|
| 80 | // stack grows towards stack limit |
---|
| 81 | void * limit; |
---|
| 82 | |
---|
| 83 | // base of stack |
---|
| 84 | void * base; |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | struct __stack_info_t { |
---|
| 88 | // pointer to stack |
---|
| 89 | struct __stack_t * storage; |
---|
[025278e] | 90 | }; |
---|
| 91 | |
---|
[63364d8] | 92 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; |
---|
[025278e] | 93 | |
---|
| 94 | struct coroutine_desc { |
---|
[b2f6113] | 95 | // context that is switch during a CtxSwitch |
---|
| 96 | struct __stack_context_t context; |
---|
| 97 | |
---|
[76e069f] | 98 | // stack information of the coroutine |
---|
[b2f6113] | 99 | struct __stack_info_t stack; |
---|
[76e069f] | 100 | |
---|
[e8e457e] | 101 | // textual name for coroutine/task |
---|
[76e069f] | 102 | const char * name; |
---|
| 103 | |
---|
| 104 | // current execution status for coroutine |
---|
| 105 | enum coroutine_state state; |
---|
[b2f6113] | 106 | |
---|
[76e069f] | 107 | // first coroutine to resume this one |
---|
| 108 | struct coroutine_desc * starter; |
---|
| 109 | |
---|
| 110 | // last coroutine to resume this one |
---|
| 111 | struct coroutine_desc * last; |
---|
| 112 | |
---|
| 113 | // If non-null stack must be unwound with this exception |
---|
| 114 | struct _Unwind_Exception * cancellation; |
---|
| 115 | |
---|
[025278e] | 116 | }; |
---|
| 117 | |
---|
[f23b685] | 118 | // struct which calls the monitor is accepting |
---|
[025278e] | 119 | struct __waitfor_mask_t { |
---|
| 120 | // the index of the accepted function, -1 if none |
---|
| 121 | short * accepted; |
---|
| 122 | |
---|
| 123 | // list of acceptable functions, null if any |
---|
[b1672e1] | 124 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); |
---|
[025278e] | 125 | }; |
---|
| 126 | |
---|
| 127 | struct monitor_desc { |
---|
| 128 | // spinlock to protect internal data |
---|
[ea7d2b0] | 129 | struct __spinlock_t lock; |
---|
[025278e] | 130 | |
---|
| 131 | // current owner of the monitor |
---|
| 132 | struct thread_desc * owner; |
---|
| 133 | |
---|
| 134 | // queue of threads that are blocked waiting for the monitor |
---|
[0cf5b79] | 135 | __queue_t(struct thread_desc) entry_queue; |
---|
[025278e] | 136 | |
---|
| 137 | // stack of conditions to run next once we exit the monitor |
---|
[0cf5b79] | 138 | __stack_t(struct __condition_criterion_t) signal_stack; |
---|
[025278e] | 139 | |
---|
| 140 | // monitor routines can be called recursively, we need to keep track of that |
---|
| 141 | unsigned int recursion; |
---|
| 142 | |
---|
| 143 | // mask used to know if some thread is waiting for something while holding the monitor |
---|
| 144 | struct __waitfor_mask_t mask; |
---|
| 145 | |
---|
| 146 | // node used to signal the dtor in a waitfor dtor |
---|
| 147 | struct __condition_node_t * dtor_node; |
---|
| 148 | }; |
---|
| 149 | |
---|
| 150 | struct __monitor_group_t { |
---|
| 151 | // currently held monitors |
---|
[b1672e1] | 152 | __cfa_anonymous_object( __small_array_t(monitor_desc*) ); |
---|
[025278e] | 153 | |
---|
| 154 | // last function that acquired monitors |
---|
[c1a9c86] | 155 | fptr_t func; |
---|
[025278e] | 156 | }; |
---|
| 157 | |
---|
| 158 | struct thread_desc { |
---|
| 159 | // Core threading fields |
---|
[e8e457e] | 160 | // context that is switch during a CtxSwitch |
---|
| 161 | struct __stack_context_t context; |
---|
| 162 | |
---|
| 163 | // current execution status for coroutine |
---|
| 164 | enum coroutine_state state; |
---|
| 165 | |
---|
[5c1a531] | 166 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it |
---|
| 167 | |
---|
[025278e] | 168 | // coroutine body used to store context |
---|
| 169 | struct coroutine_desc self_cor; |
---|
| 170 | |
---|
[82c948c] | 171 | // current active context |
---|
| 172 | struct coroutine_desc * curr_cor; |
---|
| 173 | |
---|
[025278e] | 174 | // monitor body used for mutual exclusion |
---|
| 175 | struct monitor_desc self_mon; |
---|
| 176 | |
---|
| 177 | // pointer to monitor with sufficient lifetime for current monitors |
---|
| 178 | struct monitor_desc * self_mon_p; |
---|
| 179 | |
---|
[de6319f] | 180 | // pointer to the cluster on which the thread is running |
---|
| 181 | struct cluster * curr_cluster; |
---|
| 182 | |
---|
[025278e] | 183 | // monitors currently held by this thread |
---|
| 184 | struct __monitor_group_t monitors; |
---|
| 185 | |
---|
| 186 | // Link lists fields |
---|
| 187 | // instrusive link field for threads |
---|
| 188 | struct thread_desc * next; |
---|
[f7d6bb0] | 189 | |
---|
[de94a60] | 190 | struct { |
---|
| 191 | struct thread_desc * next; |
---|
| 192 | struct thread_desc * prev; |
---|
| 193 | } node; |
---|
[212c2187] | 194 | }; |
---|
| 195 | |
---|
| 196 | #ifdef __cforall |
---|
| 197 | extern "Cforall" { |
---|
[d4e68a6] | 198 | static inline thread_desc *& get_next( thread_desc & this ) { |
---|
[0cf5b79] | 199 | return this.next; |
---|
| 200 | } |
---|
| 201 | |
---|
[de94a60] | 202 | static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { |
---|
| 203 | return this.node.[next, prev]; |
---|
| 204 | } |
---|
| 205 | |
---|
[0cf5b79] | 206 | static inline void ?{}(__monitor_group_t & this) { |
---|
| 207 | (this.data){NULL}; |
---|
| 208 | (this.size){0}; |
---|
| 209 | (this.func){NULL}; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { |
---|
| 213 | (this.data){data}; |
---|
| 214 | (this.size){size}; |
---|
| 215 | (this.func){func}; |
---|
[025278e] | 216 | } |
---|
| 217 | |
---|
| 218 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { |
---|
[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 |
---|
| 252 | extern void CtxInvokeStub( void ); |
---|
[3c06bba] | 253 | extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch"); |
---|
[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: // |
---|