[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 | |
---|
| 128 | // monitors currently held by this thread |
---|
| 129 | struct __monitor_group_t monitors; |
---|
| 130 | |
---|
| 131 | // Link lists fields |
---|
| 132 | // instrusive link field for threads |
---|
| 133 | struct thread_desc * next; |
---|
[f7d6bb0] | 134 | |
---|
| 135 | __cfaabi_dbg_debug_do( |
---|
| 136 | // instrusive link field for debugging |
---|
| 137 | struct thread_desc * dbg_next; |
---|
| 138 | struct thread_desc * dbg_prev; |
---|
| 139 | ) |
---|
[97e3296] | 140 | }; |
---|
[8118303] | 141 | |
---|
[0cf5b79] | 142 | #ifdef __cforall |
---|
[b18830e] | 143 | extern "Cforall" { |
---|
[0cf5b79] | 144 | static inline thread_desc * & get_next( thread_desc & this ) { |
---|
| 145 | return this.next; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); |
---|
| 149 | |
---|
| 150 | static inline void ?{}(__monitor_group_t & this) { |
---|
| 151 | (this.data){NULL}; |
---|
| 152 | (this.size){0}; |
---|
| 153 | (this.func){NULL}; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { |
---|
| 157 | (this.data){data}; |
---|
| 158 | (this.size){size}; |
---|
| 159 | (this.func){func}; |
---|
[025278e] | 160 | } |
---|
| 161 | |
---|
| 162 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { |
---|
[0cf5b79] | 163 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; |
---|
[025278e] | 164 | if( lhs.size != rhs.size ) return false; |
---|
| 165 | if( lhs.func != rhs.func ) return false; |
---|
| 166 | |
---|
| 167 | // Check that all the monitors match |
---|
| 168 | for( int i = 0; i < lhs.size; i++ ) { |
---|
| 169 | // If not a match, check next function |
---|
| 170 | if( lhs[i] != rhs[i] ) return false; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | return true; |
---|
| 174 | } |
---|
[0cf5b79] | 175 | |
---|
| 176 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { |
---|
| 177 | lhs.data = rhs.data; |
---|
| 178 | lhs.size = rhs.size; |
---|
| 179 | lhs.func = rhs.func; |
---|
| 180 | } |
---|
[025278e] | 181 | } |
---|
| 182 | #endif |
---|
[b18830e] | 183 | |
---|
[5c81105] | 184 | #endif //_INVOKE_H_ |
---|
| 185 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 186 | #ifndef _INVOKE_PRIVATE_H_ |
---|
| 187 | #define _INVOKE_PRIVATE_H_ |
---|
[b227f68] | 188 | |
---|
[025278e] | 189 | struct machine_context_t { |
---|
| 190 | void *SP; |
---|
| 191 | void *FP; |
---|
| 192 | void *PC; |
---|
| 193 | }; |
---|
| 194 | |
---|
| 195 | // assembler routines that performs the context switch |
---|
| 196 | extern void CtxInvokeStub( void ); |
---|
| 197 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); |
---|
| 198 | |
---|
[381fdee] | 199 | #if defined( __i386 ) |
---|
[025278e] | 200 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 201 | "movl %%esp,%0\n" \ |
---|
| 202 | "movl %%ebp,%1\n" \ |
---|
| 203 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[381fdee] | 204 | #elif defined( __x86_64 ) |
---|
| 205 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 206 | "movq %%rsp,%0\n" \ |
---|
| 207 | "movq %%rbp,%1\n" \ |
---|
| 208 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[b158d8f] | 209 | #elif defined( __ARM_ARCH ) |
---|
| 210 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 211 | "mov %0,%%sp\n" \ |
---|
| 212 | "mov %1,%%r11\n" \ |
---|
| 213 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
[381fdee] | 214 | #else |
---|
| 215 | #error unknown hardware architecture |
---|
[025278e] | 216 | #endif |
---|
[78b3f52] | 217 | |
---|
[5c81105] | 218 | #endif //_INVOKE_PRIVATE_H_ |
---|
| 219 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
[0cf5b79] | 220 | #ifdef __cforall |
---|
[5c81105] | 221 | } |
---|
| 222 | #endif |
---|
[6b0b624] | 223 | |
---|
| 224 | // Local Variables: // |
---|
| 225 | // mode: c // |
---|
| 226 | // tab-width: 4 // |
---|
| 227 | // End: // |
---|