| 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
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Thu Dec  5 16:26:03 2019
 | 
|---|
| 13 | // Update Count     : 44
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "bits/containers.hfa"
 | 
|---|
| 17 | #include "bits/defs.hfa"
 | 
|---|
| 18 | #include "bits/locks.hfa"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #ifdef __cforall
 | 
|---|
| 21 | extern "C" {
 | 
|---|
| 22 | #endif
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #if ! defined(__CFA_INVOKE_PRIVATE__)
 | 
|---|
| 25 | #ifndef _INVOKE_H_
 | 
|---|
| 26 | #define _INVOKE_H_
 | 
|---|
| 27 | 
 | 
|---|
| 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 | 
 | 
|---|
| 46 |         #ifdef __cforall
 | 
|---|
| 47 |         extern "Cforall" {
 | 
|---|
| 48 |                 extern __attribute__((aligned(128))) thread_local struct KernelThreadData {
 | 
|---|
| 49 |                         struct $thread    * volatile this_thread;
 | 
|---|
| 50 |                         struct processor  * volatile this_processor;
 | 
|---|
| 51 |                         struct __stats_t  * volatile this_stats;
 | 
|---|
| 52 | 
 | 
|---|
| 53 |                         struct {
 | 
|---|
| 54 |                                 volatile unsigned short disable_count;
 | 
|---|
| 55 |                                 volatile bool enabled;
 | 
|---|
| 56 |                                 volatile bool in_progress;
 | 
|---|
| 57 |                         } preemption_state;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |                         #if defined(__SIZEOF_INT128__)
 | 
|---|
| 60 |                                 __uint128_t rand_seed;
 | 
|---|
| 61 |                         #else
 | 
|---|
| 62 |                                 uint64_t rand_seed;
 | 
|---|
| 63 |                         #endif
 | 
|---|
| 64 |                 } kernelTLS __attribute__ ((tls_model ( "initial-exec" )));
 | 
|---|
| 65 |         }
 | 
|---|
| 66 |         #endif
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         struct __stack_context_t {
 | 
|---|
| 69 |                 void * SP;
 | 
|---|
| 70 |                 void * FP;
 | 
|---|
| 71 |         };
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         // low adresses  :           +----------------------+ <- start of allocation
 | 
|---|
| 74 |         //                           |  optional guard page |
 | 
|---|
| 75 |         //                           +----------------------+ <- __stack_t.limit
 | 
|---|
| 76 |         //                           |                      |
 | 
|---|
| 77 |         //                           |       /\ /\ /\       |
 | 
|---|
| 78 |         //                           |       || || ||       |
 | 
|---|
| 79 |         //                           |                      |
 | 
|---|
| 80 |         //                           |    program  stack    |
 | 
|---|
| 81 |         //                           |                      |
 | 
|---|
| 82 |         // __stack_info_t.storage -> +----------------------+ <- __stack_t.base
 | 
|---|
| 83 |         //                           |      __stack_t       |
 | 
|---|
| 84 |         // high adresses :           +----------------------+ <- end of allocation
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         struct __stack_t {
 | 
|---|
| 87 |                 // stack grows towards stack limit
 | 
|---|
| 88 |                 void * limit;
 | 
|---|
| 89 | 
 | 
|---|
| 90 |                 // base of stack
 | 
|---|
| 91 |                 void * base;
 | 
|---|
| 92 |         };
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         struct __stack_info_t {
 | 
|---|
| 95 |                 // pointer to stack
 | 
|---|
| 96 |                 struct __stack_t * storage;
 | 
|---|
| 97 |         };
 | 
|---|
| 98 | 
 | 
|---|
| 99 |         enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active };
 | 
|---|
| 100 |         enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION };
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         struct $coroutine {
 | 
|---|
| 103 |                 // context that is switch during a __cfactx_switch
 | 
|---|
| 104 |                 struct __stack_context_t context;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |                 // stack information of the coroutine
 | 
|---|
| 107 |                 struct __stack_info_t stack;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |                 // textual name for coroutine/task
 | 
|---|
| 110 |                 const char * name;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |                 // current execution status for coroutine
 | 
|---|
| 113 |                 enum __Coroutine_State state;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |                 // first coroutine to resume this one
 | 
|---|
| 116 |                 struct $coroutine * starter;
 | 
|---|
| 117 | 
 | 
|---|
| 118 |                 // last coroutine to resume this one
 | 
|---|
| 119 |                 struct $coroutine * last;
 | 
|---|
| 120 | 
 | 
|---|
| 121 |                 // If non-null stack must be unwound with this exception
 | 
|---|
| 122 |                 struct _Unwind_Exception * cancellation;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         };
 | 
|---|
| 125 | 
 | 
|---|
| 126 |         static inline struct __stack_t * __get_stack( struct $coroutine * cor ) { return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         // struct which calls the monitor is accepting
 | 
|---|
| 129 |         struct __waitfor_mask_t {
 | 
|---|
| 130 |                 // the index of the accepted function, -1 if none
 | 
|---|
| 131 |                 short * accepted;
 | 
|---|
| 132 | 
 | 
|---|
| 133 |                 // list of acceptable functions, null if any
 | 
|---|
| 134 |                 __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
 | 
|---|
| 135 |         };
 | 
|---|
| 136 | 
 | 
|---|
| 137 |         struct $monitor {
 | 
|---|
| 138 |                 // spinlock to protect internal data
 | 
|---|
| 139 |                 struct __spinlock_t lock;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |                 // current owner of the monitor
 | 
|---|
| 142 |                 struct $thread * owner;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |                 // queue of threads that are blocked waiting for the monitor
 | 
|---|
| 145 |                 __queue_t(struct $thread) entry_queue;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |                 // stack of conditions to run next once we exit the monitor
 | 
|---|
| 148 |                 __stack_t(struct __condition_criterion_t) signal_stack;
 | 
|---|
| 149 | 
 | 
|---|
| 150 |                 // monitor routines can be called recursively, we need to keep track of that
 | 
|---|
| 151 |                 unsigned int recursion;
 | 
|---|
| 152 | 
 | 
|---|
| 153 |                 // mask used to know if some thread is waiting for something while holding the monitor
 | 
|---|
| 154 |                 struct __waitfor_mask_t mask;
 | 
|---|
| 155 | 
 | 
|---|
| 156 |                 // node used to signal the dtor in a waitfor dtor
 | 
|---|
| 157 |                 struct __condition_node_t * dtor_node;
 | 
|---|
| 158 |         };
 | 
|---|
| 159 | 
 | 
|---|
| 160 |         struct __monitor_group_t {
 | 
|---|
| 161 |                 // currently held monitors
 | 
|---|
| 162 |                 __cfa_anonymous_object( __small_array_t($monitor*) );
 | 
|---|
| 163 | 
 | 
|---|
| 164 |                 // last function that acquired monitors
 | 
|---|
| 165 |                 fptr_t func;
 | 
|---|
| 166 |         };
 | 
|---|
| 167 | 
 | 
|---|
| 168 |         // Link lists fields
 | 
|---|
| 169 |         // instrusive link field for threads
 | 
|---|
| 170 |         struct __thread_desc_link {
 | 
|---|
| 171 |                 struct $thread * next;
 | 
|---|
| 172 |                 struct $thread * prev;
 | 
|---|
| 173 |                 volatile unsigned long long ts;
 | 
|---|
| 174 |                 int preferred;
 | 
|---|
| 175 |         };
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         struct $thread {
 | 
|---|
| 178 |                 // Core threading fields
 | 
|---|
| 179 |                 // context that is switch during a __cfactx_switch
 | 
|---|
| 180 |                 struct __stack_context_t context;
 | 
|---|
| 181 | 
 | 
|---|
| 182 |                 // current execution status for coroutine
 | 
|---|
| 183 |                 volatile int ticket;
 | 
|---|
| 184 |                 enum __Coroutine_State state:8;
 | 
|---|
| 185 |                 enum __Preemption_Reason preempted:8;
 | 
|---|
| 186 | 
 | 
|---|
| 187 |                 //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it
 | 
|---|
| 188 | 
 | 
|---|
| 189 |                 // pointer to the cluster on which the thread is running
 | 
|---|
| 190 |                 struct cluster * curr_cluster;
 | 
|---|
| 191 | 
 | 
|---|
| 192 |                 // Link lists fields
 | 
|---|
| 193 |                 // instrusive link field for threads
 | 
|---|
| 194 |                 struct __thread_desc_link link;
 | 
|---|
| 195 | 
 | 
|---|
| 196 |                 // coroutine body used to store context
 | 
|---|
| 197 |                 struct $coroutine  self_cor;
 | 
|---|
| 198 | 
 | 
|---|
| 199 |                 // current active context
 | 
|---|
| 200 |                 struct $coroutine * curr_cor;
 | 
|---|
| 201 | 
 | 
|---|
| 202 |                 // monitor body used for mutual exclusion
 | 
|---|
| 203 |                 struct $monitor    self_mon;
 | 
|---|
| 204 | 
 | 
|---|
| 205 |                 // pointer to monitor with sufficient lifetime for current monitors
 | 
|---|
| 206 |                 struct $monitor *  self_mon_p;
 | 
|---|
| 207 | 
 | 
|---|
| 208 |                 // monitors currently held by this thread
 | 
|---|
| 209 |                 struct __monitor_group_t monitors;
 | 
|---|
| 210 | 
 | 
|---|
| 211 |                 struct {
 | 
|---|
| 212 |                         struct $thread * next;
 | 
|---|
| 213 |                         struct $thread * prev;
 | 
|---|
| 214 |                 } node;
 | 
|---|
| 215 | 
 | 
|---|
| 216 |                 #ifdef __CFA_DEBUG__
 | 
|---|
| 217 |                         // previous function to park/unpark the thread
 | 
|---|
| 218 |                         const char * park_caller;
 | 
|---|
| 219 |                         int park_result;
 | 
|---|
| 220 |                         enum __Coroutine_State park_state;
 | 
|---|
| 221 |                         bool park_stale;
 | 
|---|
| 222 |                         const char * unpark_caller;
 | 
|---|
| 223 |                         int unpark_result;
 | 
|---|
| 224 |                         enum __Coroutine_State unpark_state;
 | 
|---|
| 225 |                         bool unpark_stale;
 | 
|---|
| 226 |                 #endif
 | 
|---|
| 227 |         };
 | 
|---|
| 228 | 
 | 
|---|
| 229 |         #ifdef __CFA_DEBUG__
 | 
|---|
| 230 |                 void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]);
 | 
|---|
| 231 |         #else
 | 
|---|
| 232 |                 #define __cfaabi_dbg_record_thrd(x, y, z)
 | 
|---|
| 233 |         #endif
 | 
|---|
| 234 | 
 | 
|---|
| 235 |         #ifdef __cforall
 | 
|---|
| 236 |         extern "Cforall" {
 | 
|---|
| 237 | 
 | 
|---|
| 238 |                 static inline $thread *& get_next( $thread & this ) __attribute__((const)) {
 | 
|---|
| 239 |                         return this.link.next;
 | 
|---|
| 240 |                 }
 | 
|---|
| 241 | 
 | 
|---|
| 242 |                 static inline [$thread *&, $thread *& ] __get( $thread & this ) __attribute__((const)) {
 | 
|---|
| 243 |                         return this.node.[next, prev];
 | 
|---|
| 244 |                 }
 | 
|---|
| 245 | 
 | 
|---|
| 246 |                 static inline void ?{}(__monitor_group_t & this) {
 | 
|---|
| 247 |                         (this.data){0p};
 | 
|---|
| 248 |                         (this.size){0};
 | 
|---|
| 249 |                         (this.func){NULL};
 | 
|---|
| 250 |                 }
 | 
|---|
| 251 | 
 | 
|---|
| 252 |                 static inline void ?{}(__monitor_group_t & this, struct $monitor ** data, __lock_size_t size, fptr_t func) {
 | 
|---|
| 253 |                         (this.data){data};
 | 
|---|
| 254 |                         (this.size){size};
 | 
|---|
| 255 |                         (this.func){func};
 | 
|---|
| 256 |                 }
 | 
|---|
| 257 | 
 | 
|---|
| 258 |                 static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) {
 | 
|---|
| 259 |                         if( (lhs.data != 0) != (rhs.data != 0) ) return false;
 | 
|---|
| 260 |                         if( lhs.size != rhs.size ) return false;
 | 
|---|
| 261 |                         if( lhs.func != rhs.func ) return false;
 | 
|---|
| 262 | 
 | 
|---|
| 263 |                         // Check that all the monitors match
 | 
|---|
| 264 |                         for( int i = 0; i < lhs.size; i++ ) {
 | 
|---|
| 265 |                                 // If not a match, check next function
 | 
|---|
| 266 |                                 if( lhs[i] != rhs[i] ) return false;
 | 
|---|
| 267 |                         }
 | 
|---|
| 268 | 
 | 
|---|
| 269 |                         return true;
 | 
|---|
| 270 |                 }
 | 
|---|
| 271 | 
 | 
|---|
| 272 |                 static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
 | 
|---|
| 273 |                         lhs.data = rhs.data;
 | 
|---|
| 274 |                         lhs.size = rhs.size;
 | 
|---|
| 275 |                         lhs.func = rhs.func;
 | 
|---|
| 276 |                 }
 | 
|---|
| 277 |         }
 | 
|---|
| 278 |         #endif
 | 
|---|
| 279 | 
 | 
|---|
| 280 | #endif //_INVOKE_H_
 | 
|---|
| 281 | #else //! defined(__CFA_INVOKE_PRIVATE__)
 | 
|---|
| 282 | #ifndef _INVOKE_PRIVATE_H_
 | 
|---|
| 283 | #define _INVOKE_PRIVATE_H_
 | 
|---|
| 284 | 
 | 
|---|
| 285 |         struct machine_context_t {
 | 
|---|
| 286 |                 void *SP;
 | 
|---|
| 287 |                 void *FP;
 | 
|---|
| 288 |                 void *PC;
 | 
|---|
| 289 |         };
 | 
|---|
| 290 | 
 | 
|---|
| 291 |         // assembler routines that performs the context switch
 | 
|---|
| 292 |         extern void __cfactx_invoke_stub( void );
 | 
|---|
| 293 |         extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
 | 
|---|
| 294 |         // void CtxStore ( void * this ) asm ("CtxStore");
 | 
|---|
| 295 |         // void CtxRet   ( void * dst  ) asm ("CtxRet");
 | 
|---|
| 296 | 
 | 
|---|
| 297 | #endif //_INVOKE_PRIVATE_H_
 | 
|---|
| 298 | #endif //! defined(__CFA_INVOKE_PRIVATE__)
 | 
|---|
| 299 | #ifdef __cforall
 | 
|---|
| 300 | }
 | 
|---|
| 301 | #endif
 | 
|---|
| 302 | 
 | 
|---|
| 303 | // Local Variables: //
 | 
|---|
| 304 | // mode: c //
 | 
|---|
| 305 | // tab-width: 4 //
 | 
|---|
| 306 | // End: //
 | 
|---|