| [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 | 
|---|
| [597c34a3] | 12 | // Last Modified On : Sat May 19 08:23:21 2018 | 
|---|
|  | 13 | // Update Count     : 31 | 
|---|
| [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" { | 
|---|
| [0cf5b79] | 48 | static inline struct thread_desc             * & get_next( struct thread_desc             & this ); | 
|---|
|  | 49 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); | 
|---|
| [b10affd] | 50 |  | 
|---|
|  | 51 | extern thread_local struct KernelThreadData { | 
|---|
|  | 52 | struct coroutine_desc * volatile this_coroutine; | 
|---|
|  | 53 | struct thread_desc    * volatile this_thread; | 
|---|
|  | 54 | struct processor      * volatile this_processor; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | struct { | 
|---|
|  | 57 | volatile unsigned short disable_count; | 
|---|
|  | 58 | volatile bool enabled; | 
|---|
|  | 59 | volatile bool in_progress; | 
|---|
|  | 60 | } preemption_state; | 
|---|
| [afc2427] | 61 | } kernelTLS __attribute__ ((tls_model ( "initial-exec" ))); | 
|---|
| [025278e] | 62 | } | 
|---|
|  | 63 |  | 
|---|
| [b10affd] | 64 | static inline struct coroutine_desc * volatile active_coroutine() { return TL_GET( this_coroutine ); } | 
|---|
| [14a61b5] | 65 | static inline struct thread_desc    * volatile active_thread   () { return TL_GET( this_thread    ); } | 
|---|
| [afd550c] | 66 | static inline struct processor      * volatile active_processor() { return TL_GET( this_processor ); } // UNSAFE | 
|---|
| [4c1b48f3] | 67 | #endif | 
|---|
| [025278e] | 68 |  | 
|---|
| [b10affd] | 69 | struct coStack_t { | 
|---|
|  | 70 | size_t size;                                                                    // size of stack | 
|---|
|  | 71 | void * storage;                                                                 // pointer to stack | 
|---|
|  | 72 | void * limit;                                                                   // stack grows towards stack limit | 
|---|
|  | 73 | void * base;                                                                    // base of stack | 
|---|
|  | 74 | void * context;                                                                 // address of cfa_context_t | 
|---|
|  | 75 | void * top;                                                                             // address of top of storage | 
|---|
|  | 76 | bool userStack;                                                                 // whether or not the user allocated the stack | 
|---|
| [025278e] | 77 | }; | 
|---|
|  | 78 |  | 
|---|
|  | 79 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | struct coroutine_desc { | 
|---|
| [b10affd] | 82 | struct coStack_t stack;                                                 // stack information of the coroutine | 
|---|
|  | 83 | const char * name;                                                              // textual name for coroutine/task, initialized by uC++ generated code | 
|---|
|  | 84 | int errno_;                                                                             // copy of global UNIX variable errno | 
|---|
|  | 85 | enum coroutine_state state;                                             // current execution status for coroutine | 
|---|
|  | 86 | struct coroutine_desc * starter;                                // first coroutine to resume this one | 
|---|
|  | 87 | struct coroutine_desc * last;                                   // last coroutine to resume this one | 
|---|
| [025278e] | 88 | }; | 
|---|
|  | 89 |  | 
|---|
|  | 90 | struct __waitfor_mask_t { | 
|---|
|  | 91 | // the index of the accepted function, -1 if none | 
|---|
|  | 92 | short * accepted; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | // list of acceptable functions, null if any | 
|---|
| [b1672e1] | 95 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) ); | 
|---|
| [025278e] | 96 | }; | 
|---|
|  | 97 |  | 
|---|
|  | 98 | struct monitor_desc { | 
|---|
|  | 99 | // spinlock to protect internal data | 
|---|
| [ea7d2b0] | 100 | struct __spinlock_t lock; | 
|---|
| [025278e] | 101 |  | 
|---|
|  | 102 | // current owner of the monitor | 
|---|
|  | 103 | struct thread_desc * owner; | 
|---|
|  | 104 |  | 
|---|
|  | 105 | // queue of threads that are blocked waiting for the monitor | 
|---|
| [0cf5b79] | 106 | __queue_t(struct thread_desc) entry_queue; | 
|---|
| [025278e] | 107 |  | 
|---|
|  | 108 | // stack of conditions to run next once we exit the monitor | 
|---|
| [0cf5b79] | 109 | __stack_t(struct __condition_criterion_t) signal_stack; | 
|---|
| [025278e] | 110 |  | 
|---|
|  | 111 | // monitor routines can be called recursively, we need to keep track of that | 
|---|
|  | 112 | unsigned int recursion; | 
|---|
|  | 113 |  | 
|---|
|  | 114 | // mask used to know if some thread is waiting for something while holding the monitor | 
|---|
|  | 115 | struct __waitfor_mask_t mask; | 
|---|
|  | 116 |  | 
|---|
|  | 117 | // node used to signal the dtor in a waitfor dtor | 
|---|
|  | 118 | struct __condition_node_t * dtor_node; | 
|---|
|  | 119 | }; | 
|---|
|  | 120 |  | 
|---|
|  | 121 | struct __monitor_group_t { | 
|---|
|  | 122 | // currently held monitors | 
|---|
| [b1672e1] | 123 | __cfa_anonymous_object( __small_array_t(monitor_desc*) ); | 
|---|
| [025278e] | 124 |  | 
|---|
|  | 125 | // last function that acquired monitors | 
|---|
| [c1a9c86] | 126 | fptr_t func; | 
|---|
| [025278e] | 127 | }; | 
|---|
|  | 128 |  | 
|---|
|  | 129 | struct thread_desc { | 
|---|
|  | 130 | // Core threading fields | 
|---|
|  | 131 | // coroutine body used to store context | 
|---|
|  | 132 | struct coroutine_desc  self_cor; | 
|---|
|  | 133 |  | 
|---|
| [82c948c] | 134 | // current active context | 
|---|
|  | 135 | struct coroutine_desc * curr_cor; | 
|---|
|  | 136 |  | 
|---|
| [025278e] | 137 | // monitor body used for mutual exclusion | 
|---|
|  | 138 | struct monitor_desc    self_mon; | 
|---|
|  | 139 |  | 
|---|
|  | 140 | // pointer to monitor with sufficient lifetime for current monitors | 
|---|
|  | 141 | struct monitor_desc *  self_mon_p; | 
|---|
|  | 142 |  | 
|---|
| [de6319f] | 143 | // pointer to the cluster on which the thread is running | 
|---|
|  | 144 | struct cluster * curr_cluster; | 
|---|
|  | 145 |  | 
|---|
| [025278e] | 146 | // monitors currently held by this thread | 
|---|
|  | 147 | struct __monitor_group_t monitors; | 
|---|
|  | 148 |  | 
|---|
|  | 149 | // Link lists fields | 
|---|
|  | 150 | // instrusive link field for threads | 
|---|
|  | 151 | struct thread_desc * next; | 
|---|
| [f7d6bb0] | 152 |  | 
|---|
| [de94a60] | 153 | struct { | 
|---|
|  | 154 | struct thread_desc * next; | 
|---|
|  | 155 | struct thread_desc * prev; | 
|---|
|  | 156 | } node; | 
|---|
| [97e3296] | 157 | }; | 
|---|
| [8118303] | 158 |  | 
|---|
| [0cf5b79] | 159 | #ifdef __cforall | 
|---|
| [b18830e] | 160 | extern "Cforall" { | 
|---|
| [0cf5b79] | 161 | static inline thread_desc * & get_next( thread_desc & this ) { | 
|---|
|  | 162 | return this.next; | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
| [de94a60] | 165 | static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { | 
|---|
|  | 166 | return this.node.[next, prev]; | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
| [0cf5b79] | 169 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); | 
|---|
|  | 170 |  | 
|---|
|  | 171 | static inline void ?{}(__monitor_group_t & this) { | 
|---|
|  | 172 | (this.data){NULL}; | 
|---|
|  | 173 | (this.size){0}; | 
|---|
|  | 174 | (this.func){NULL}; | 
|---|
|  | 175 | } | 
|---|
|  | 176 |  | 
|---|
|  | 177 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { | 
|---|
|  | 178 | (this.data){data}; | 
|---|
|  | 179 | (this.size){size}; | 
|---|
|  | 180 | (this.func){func}; | 
|---|
| [025278e] | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { | 
|---|
| [0cf5b79] | 184 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; | 
|---|
| [025278e] | 185 | if( lhs.size != rhs.size ) return false; | 
|---|
|  | 186 | if( lhs.func != rhs.func ) return false; | 
|---|
|  | 187 |  | 
|---|
|  | 188 | // Check that all the monitors match | 
|---|
|  | 189 | for( int i = 0; i < lhs.size; i++ ) { | 
|---|
|  | 190 | // If not a match, check next function | 
|---|
|  | 191 | if( lhs[i] != rhs[i] ) return false; | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 | return true; | 
|---|
|  | 195 | } | 
|---|
| [0cf5b79] | 196 |  | 
|---|
|  | 197 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { | 
|---|
|  | 198 | lhs.data = rhs.data; | 
|---|
|  | 199 | lhs.size = rhs.size; | 
|---|
|  | 200 | lhs.func = rhs.func; | 
|---|
|  | 201 | } | 
|---|
| [025278e] | 202 | } | 
|---|
|  | 203 | #endif | 
|---|
| [b18830e] | 204 |  | 
|---|
| [5c81105] | 205 | #endif //_INVOKE_H_ | 
|---|
|  | 206 | #else //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
|  | 207 | #ifndef _INVOKE_PRIVATE_H_ | 
|---|
|  | 208 | #define _INVOKE_PRIVATE_H_ | 
|---|
| [b227f68] | 209 |  | 
|---|
| [025278e] | 210 | struct machine_context_t { | 
|---|
|  | 211 | void *SP; | 
|---|
|  | 212 | void *FP; | 
|---|
|  | 213 | void *PC; | 
|---|
|  | 214 | }; | 
|---|
|  | 215 |  | 
|---|
|  | 216 | // assembler routines that performs the context switch | 
|---|
|  | 217 | extern void CtxInvokeStub( void ); | 
|---|
|  | 218 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); | 
|---|
|  | 219 |  | 
|---|
| [381fdee] | 220 | #if   defined( __i386 ) | 
|---|
| [025278e] | 221 | #define CtxGet( ctx ) __asm__ ( \ | 
|---|
|  | 222 | "movl %%esp,%0\n"   \ | 
|---|
|  | 223 | "movl %%ebp,%1\n"   \ | 
|---|
|  | 224 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) | 
|---|
| [381fdee] | 225 | #elif defined( __x86_64 ) | 
|---|
|  | 226 | #define CtxGet( ctx ) __asm__ ( \ | 
|---|
|  | 227 | "movq %%rsp,%0\n"   \ | 
|---|
|  | 228 | "movq %%rbp,%1\n"   \ | 
|---|
|  | 229 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) | 
|---|
| [b158d8f] | 230 | #elif defined( __ARM_ARCH ) | 
|---|
|  | 231 | #define CtxGet( ctx ) __asm__ ( \ | 
|---|
|  | 232 | "mov %0,%%sp\n"   \ | 
|---|
|  | 233 | "mov %1,%%r11\n"   \ | 
|---|
|  | 234 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) | 
|---|
| [381fdee] | 235 | #else | 
|---|
|  | 236 | #error unknown hardware architecture | 
|---|
| [025278e] | 237 | #endif | 
|---|
| [78b3f52] | 238 |  | 
|---|
| [5c81105] | 239 | #endif //_INVOKE_PRIVATE_H_ | 
|---|
|  | 240 | #endif //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| [0cf5b79] | 241 | #ifdef __cforall | 
|---|
| [5c81105] | 242 | } | 
|---|
|  | 243 | #endif | 
|---|
| [6b0b624] | 244 |  | 
|---|
|  | 245 | // Local Variables: // | 
|---|
|  | 246 | // mode: c // | 
|---|
|  | 247 | // tab-width: 4 // | 
|---|
|  | 248 | // End: // | 
|---|