[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 | |
---|
[14a61b5] | 20 | #define TL_GET( member ) kernelTLS.member |
---|
| 21 | #define TL_SET( member, value ) kernelTLS.member = value; |
---|
[b10affd] | 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; |
---|
[14a61b5] | 46 | } kernelTLS; |
---|
[025278e] | 47 | } |
---|
| 48 | |
---|
[b10affd] | 49 | static inline struct coroutine_desc * volatile active_coroutine() { return TL_GET( this_coroutine ); } |
---|
[14a61b5] | 50 | static inline struct thread_desc * volatile active_thread () { return TL_GET( this_thread ); } |
---|
[afd550c] | 51 | static inline struct processor * volatile active_processor() { return TL_GET( this_processor ); } // UNSAFE |
---|
[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 | |
---|
[de94a60] | 138 | struct { |
---|
| 139 | struct thread_desc * next; |
---|
| 140 | struct thread_desc * prev; |
---|
| 141 | } node; |
---|
[97e3296] | 142 | }; |
---|
[8118303] | 143 | |
---|
[0cf5b79] | 144 | #ifdef __cforall |
---|
[b18830e] | 145 | extern "Cforall" { |
---|
[0cf5b79] | 146 | static inline thread_desc * & get_next( thread_desc & this ) { |
---|
| 147 | return this.next; |
---|
| 148 | } |
---|
| 149 | |
---|
[de94a60] | 150 | static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { |
---|
| 151 | return this.node.[next, prev]; |
---|
| 152 | } |
---|
| 153 | |
---|
[0cf5b79] | 154 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); |
---|
| 155 | |
---|
| 156 | static inline void ?{}(__monitor_group_t & this) { |
---|
| 157 | (this.data){NULL}; |
---|
| 158 | (this.size){0}; |
---|
| 159 | (this.func){NULL}; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { |
---|
| 163 | (this.data){data}; |
---|
| 164 | (this.size){size}; |
---|
| 165 | (this.func){func}; |
---|
[025278e] | 166 | } |
---|
| 167 | |
---|
| 168 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { |
---|
[0cf5b79] | 169 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; |
---|
[025278e] | 170 | if( lhs.size != rhs.size ) return false; |
---|
| 171 | if( lhs.func != rhs.func ) return false; |
---|
| 172 | |
---|
| 173 | // Check that all the monitors match |
---|
| 174 | for( int i = 0; i < lhs.size; i++ ) { |
---|
| 175 | // If not a match, check next function |
---|
| 176 | if( lhs[i] != rhs[i] ) return false; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return true; |
---|
| 180 | } |
---|
[0cf5b79] | 181 | |
---|
| 182 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { |
---|
| 183 | lhs.data = rhs.data; |
---|
| 184 | lhs.size = rhs.size; |
---|
| 185 | lhs.func = rhs.func; |
---|
| 186 | } |
---|
[025278e] | 187 | } |
---|
| 188 | #endif |
---|
[b18830e] | 189 | |
---|
[5c81105] | 190 | #endif //_INVOKE_H_ |
---|
| 191 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 192 | #ifndef _INVOKE_PRIVATE_H_ |
---|
| 193 | #define _INVOKE_PRIVATE_H_ |
---|
[b227f68] | 194 | |
---|
[025278e] | 195 | struct machine_context_t { |
---|
| 196 | void *SP; |
---|
| 197 | void *FP; |
---|
| 198 | void *PC; |
---|
| 199 | }; |
---|
| 200 | |
---|
| 201 | // assembler routines that performs the context switch |
---|
| 202 | extern void CtxInvokeStub( void ); |
---|
| 203 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); |
---|
| 204 | |
---|
[381fdee] | 205 | #if defined( __i386 ) |
---|
[025278e] | 206 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 207 | "movl %%esp,%0\n" \ |
---|
| 208 | "movl %%ebp,%1\n" \ |
---|
| 209 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[381fdee] | 210 | #elif defined( __x86_64 ) |
---|
| 211 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 212 | "movq %%rsp,%0\n" \ |
---|
| 213 | "movq %%rbp,%1\n" \ |
---|
| 214 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[b158d8f] | 215 | #elif defined( __ARM_ARCH ) |
---|
| 216 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 217 | "mov %0,%%sp\n" \ |
---|
| 218 | "mov %1,%%r11\n" \ |
---|
| 219 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[381fdee] | 220 | #else |
---|
| 221 | #error unknown hardware architecture |
---|
[025278e] | 222 | #endif |
---|
[78b3f52] | 223 | |
---|
[5c81105] | 224 | #endif //_INVOKE_PRIVATE_H_ |
---|
| 225 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
[0cf5b79] | 226 | #ifdef __cforall |
---|
[5c81105] | 227 | } |
---|
| 228 | #endif |
---|
[6b0b624] | 229 | |
---|
| 230 | // Local Variables: // |
---|
| 231 | // mode: c // |
---|
| 232 | // tab-width: 4 // |
---|
| 233 | // End: // |
---|