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