[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
|
---|
[dd46fd3] | 12 | // Last Modified On : Tue Nov 29 20:42:21 2022
|
---|
| 13 | // Update Count : 56
|
---|
[8def349] | 14 | //
|
---|
| 15 |
|
---|
[73abe95] | 16 | #include "bits/containers.hfa"
|
---|
| 17 | #include "bits/defs.hfa"
|
---|
| 18 | #include "bits/locks.hfa"
|
---|
[9fce2572] | 19 | #include "bits/random.hfa"
|
---|
[3e2b9c9] | 20 | #include "kernel/fwd.hfa"
|
---|
[78b3f52] | 21 |
|
---|
[0cf5b79] | 22 | #ifdef __cforall
|
---|
[82f4063] | 23 | #include "containers/list.hfa"
|
---|
[5c81105] | 24 | extern "C" {
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 | #if ! defined(__CFA_INVOKE_PRIVATE__)
|
---|
[78b3f52] | 28 | #ifndef _INVOKE_H_
|
---|
| 29 | #define _INVOKE_H_
|
---|
| 30 |
|
---|
[eaf269d] | 31 | enum { DEFAULT_STACK_SIZE = 65000 };
|
---|
| 32 |
|
---|
[80ec409] | 33 | struct __cfaehm_try_resume_node;
|
---|
| 34 | struct __cfaehm_base_exception_t;
|
---|
| 35 | struct exception_context_t {
|
---|
| 36 | struct __cfaehm_try_resume_node * top_resume;
|
---|
| 37 | struct __cfaehm_base_exception_t * current_exception;
|
---|
| 38 | };
|
---|
| 39 |
|
---|
[b2f6113] | 40 | struct __stack_context_t {
|
---|
| 41 | void * SP;
|
---|
| 42 | void * FP;
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | // low adresses : +----------------------+ <- start of allocation
|
---|
| 46 | // | optional guard page |
|
---|
| 47 | // +----------------------+ <- __stack_t.limit
|
---|
| 48 | // | |
|
---|
| 49 | // | /\ /\ /\ |
|
---|
| 50 | // | || || || |
|
---|
| 51 | // | |
|
---|
| 52 | // | program stack |
|
---|
| 53 | // | |
|
---|
| 54 | // __stack_info_t.storage -> +----------------------+ <- __stack_t.base
|
---|
| 55 | // | __stack_t |
|
---|
| 56 | // high adresses : +----------------------+ <- end of allocation
|
---|
| 57 |
|
---|
| 58 | struct __stack_t {
|
---|
| 59 | // stack grows towards stack limit
|
---|
| 60 | void * limit;
|
---|
| 61 |
|
---|
| 62 | // base of stack
|
---|
| 63 | void * base;
|
---|
[80ec409] | 64 |
|
---|
| 65 | // Information for exception handling.
|
---|
| 66 | struct exception_context_t exception_context;
|
---|
[b2f6113] | 67 | };
|
---|
| 68 |
|
---|
| 69 | struct __stack_info_t {
|
---|
| 70 | // pointer to stack
|
---|
| 71 | struct __stack_t * storage;
|
---|
[025278e] | 72 | };
|
---|
| 73 |
|
---|
[3ea8ad1] | 74 | enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active, Cancelled, Halting };
|
---|
[025278e] | 75 |
|
---|
[e84ab3d] | 76 | struct coroutine$ {
|
---|
[c7a900a] | 77 | // context that is switch during a __cfactx_switch
|
---|
[b2f6113] | 78 | struct __stack_context_t context;
|
---|
| 79 |
|
---|
[76e069f] | 80 | // stack information of the coroutine
|
---|
[b2f6113] | 81 | struct __stack_info_t stack;
|
---|
[76e069f] | 82 |
|
---|
[e8e457e] | 83 | // textual name for coroutine/task
|
---|
[76e069f] | 84 | const char * name;
|
---|
| 85 |
|
---|
| 86 | // current execution status for coroutine
|
---|
[ff79d5e] | 87 | enum __Coroutine_State state;
|
---|
[b2f6113] | 88 |
|
---|
[76e069f] | 89 | // first coroutine to resume this one
|
---|
[e84ab3d] | 90 | struct coroutine$ * starter;
|
---|
[76e069f] | 91 |
|
---|
| 92 | // last coroutine to resume this one
|
---|
[e84ab3d] | 93 | struct coroutine$ * last;
|
---|
[76e069f] | 94 |
|
---|
| 95 | // If non-null stack must be unwound with this exception
|
---|
| 96 | struct _Unwind_Exception * cancellation;
|
---|
| 97 |
|
---|
[025278e] | 98 | };
|
---|
[038110a] | 99 | // Wrapper for gdb
|
---|
[e84ab3d] | 100 | struct cfathread_coroutine_t { struct coroutine$ debug; };
|
---|
[025278e] | 101 |
|
---|
[e84ab3d] | 102 | static inline struct __stack_t * __get_stack( struct coroutine$ * cor ) {
|
---|
[80ec409] | 103 | return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2));
|
---|
| 104 | }
|
---|
[210b8b3] | 105 |
|
---|
[f23b685] | 106 | // struct which calls the monitor is accepting
|
---|
[025278e] | 107 | struct __waitfor_mask_t {
|
---|
| 108 | // the index of the accepted function, -1 if none
|
---|
| 109 | short * accepted;
|
---|
| 110 |
|
---|
| 111 | // list of acceptable functions, null if any
|
---|
[b1672e1] | 112 | __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
|
---|
[025278e] | 113 | };
|
---|
| 114 |
|
---|
[e84ab3d] | 115 | struct monitor$ {
|
---|
[025278e] | 116 | // spinlock to protect internal data
|
---|
[ea7d2b0] | 117 | struct __spinlock_t lock;
|
---|
[025278e] | 118 |
|
---|
| 119 | // current owner of the monitor
|
---|
[e84ab3d] | 120 | struct thread$ * owner;
|
---|
[025278e] | 121 |
|
---|
| 122 | // queue of threads that are blocked waiting for the monitor
|
---|
[e84ab3d] | 123 | __queue_t(struct thread$) entry_queue;
|
---|
[025278e] | 124 |
|
---|
| 125 | // stack of conditions to run next once we exit the monitor
|
---|
[0cf5b79] | 126 | __stack_t(struct __condition_criterion_t) signal_stack;
|
---|
[025278e] | 127 |
|
---|
| 128 | // monitor routines can be called recursively, we need to keep track of that
|
---|
| 129 | unsigned int recursion;
|
---|
| 130 |
|
---|
| 131 | // mask used to know if some thread is waiting for something while holding the monitor
|
---|
| 132 | struct __waitfor_mask_t mask;
|
---|
| 133 |
|
---|
| 134 | // node used to signal the dtor in a waitfor dtor
|
---|
| 135 | struct __condition_node_t * dtor_node;
|
---|
| 136 | };
|
---|
[038110a] | 137 | // Wrapper for gdb
|
---|
[e84ab3d] | 138 | struct cfathread_monitor_t { struct monitor$ debug; };
|
---|
[025278e] | 139 |
|
---|
| 140 | struct __monitor_group_t {
|
---|
| 141 | // currently held monitors
|
---|
[e84ab3d] | 142 | __cfa_anonymous_object( __small_array_t(monitor$*) );
|
---|
[025278e] | 143 |
|
---|
| 144 | // last function that acquired monitors
|
---|
[c1a9c86] | 145 | fptr_t func;
|
---|
[025278e] | 146 | };
|
---|
| 147 |
|
---|
[b798713] | 148 | // Link lists fields
|
---|
[639e4fc] | 149 | // instrusive link field for threads in the ready-queue
|
---|
[b798713] | 150 | struct __thread_desc_link {
|
---|
[e84ab3d] | 151 | struct thread$ * next;
|
---|
[1b143de] | 152 | volatile unsigned long long ts;
|
---|
[b798713] | 153 | };
|
---|
| 154 |
|
---|
[639e4fc] | 155 | // Link lists fields
|
---|
| 156 | // instrusive link field for threads in the user_link/cltr_link
|
---|
| 157 | struct __thread_user_link {
|
---|
| 158 | #ifdef __cforall
|
---|
| 159 | inline dlink(thread$);
|
---|
| 160 | #else
|
---|
| 161 | struct thread$ * next; struct thread$ * back;
|
---|
| 162 | #endif
|
---|
| 163 | };
|
---|
| 164 | _Static_assert(sizeof(struct __thread_user_link) == 2 * sizeof(struct thread$ *), "__thread_user_link should be consistent in C and Cforall");
|
---|
| 165 |
|
---|
[e84ab3d] | 166 | struct thread$ {
|
---|
[025278e] | 167 | // Core threading fields
|
---|
[c7a900a] | 168 | // context that is switch during a __cfactx_switch
|
---|
[e8e457e] | 169 | struct __stack_context_t context;
|
---|
| 170 |
|
---|
[d3ba775] | 171 | // Link lists fields
|
---|
| 172 | // instrusive link field for threads
|
---|
[15c93d8] | 173 | struct __thread_desc_link rdy_link;
|
---|
[d3ba775] | 174 |
|
---|
[e8e457e] | 175 | // current execution status for coroutine
|
---|
[6a77224] | 176 | // Possible values are:
|
---|
| 177 | // - TICKET_BLOCKED (-1) thread is blocked
|
---|
| 178 | // - TICKET_RUNNING ( 0) thread is running
|
---|
| 179 | // - TICKET_UNBLOCK ( 1) thread should ignore next block
|
---|
[ff79d5e] | 180 | volatile int ticket;
|
---|
| 181 | enum __Coroutine_State state:8;
|
---|
| 182 | enum __Preemption_Reason preempted:8;
|
---|
[e8e457e] | 183 |
|
---|
[ab5baab] | 184 | bool corctx_flag;
|
---|
| 185 |
|
---|
[5c1a531] | 186 | //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it
|
---|
| 187 |
|
---|
[37ba662] | 188 | // pointer to the cluster on which the thread is running
|
---|
| 189 | struct cluster * curr_cluster;
|
---|
| 190 |
|
---|
[24e321c] | 191 | // preferred ready-queue or CPU
|
---|
[d3ba775] | 192 | unsigned preferred;
|
---|
[37ba662] | 193 |
|
---|
[025278e] | 194 | // coroutine body used to store context
|
---|
[e84ab3d] | 195 | struct coroutine$ self_cor;
|
---|
[025278e] | 196 |
|
---|
[82c948c] | 197 | // current active context
|
---|
[e84ab3d] | 198 | struct coroutine$ * curr_cor;
|
---|
[82c948c] | 199 |
|
---|
[025278e] | 200 | // monitor body used for mutual exclusion
|
---|
[e84ab3d] | 201 | struct monitor$ self_mon;
|
---|
[025278e] | 202 |
|
---|
| 203 | // pointer to monitor with sufficient lifetime for current monitors
|
---|
[e84ab3d] | 204 | struct monitor$ * self_mon_p;
|
---|
[025278e] | 205 |
|
---|
| 206 | // monitors currently held by this thread
|
---|
| 207 | struct __monitor_group_t monitors;
|
---|
| 208 |
|
---|
[cd5b58f] | 209 | // intrusive link fields, used for locks, monitors and any user defined data structure
|
---|
| 210 | // default link fields for dlist
|
---|
[639e4fc] | 211 | struct __thread_user_link user_link;
|
---|
[82f4063] | 212 |
|
---|
[cd5b58f] | 213 | // secondary intrusive link fields, used for global cluster list
|
---|
| 214 | // default link fields for dlist
|
---|
[639e4fc] | 215 | struct __thread_user_link cltr_link;
|
---|
[ae66348] | 216 |
|
---|
[f835806] | 217 | // used to point to this thd's current clh node
|
---|
| 218 | volatile bool * clh_node;
|
---|
| 219 |
|
---|
[89eff25] | 220 | struct processor * last_proc;
|
---|
| 221 |
|
---|
[dd46fd3] | 222 | PRNG_STATE_T random_state; // fast random numbers
|
---|
[2210cfc] | 223 |
|
---|
[b4b63e8] | 224 | #if defined( __CFA_WITH_VERIFY__ )
|
---|
[878cfcc] | 225 | struct processor * volatile executing;
|
---|
[ac12f1f] | 226 | void * canary;
|
---|
[ae66348] | 227 | #endif
|
---|
[212c2187] | 228 | };
|
---|
[2856982c] | 229 |
|
---|
[038110a] | 230 | // Wrapper for gdb
|
---|
[e84ab3d] | 231 | struct cfathread_thread_t { struct thread$ debug; };
|
---|
[212c2187] | 232 |
|
---|
[ae66348] | 233 | #ifdef __CFA_DEBUG__
|
---|
[e84ab3d] | 234 | void __cfaabi_dbg_record_thrd(thread$ & this, bool park, const char prev_name[]);
|
---|
[ae66348] | 235 | #else
|
---|
| 236 | #define __cfaabi_dbg_record_thrd(x, y, z)
|
---|
| 237 | #endif
|
---|
| 238 |
|
---|
[212c2187] | 239 | #ifdef __cforall
|
---|
| 240 | extern "Cforall" {
|
---|
[cd5b58f] | 241 | static inline thread$ * volatile & ?`next ( thread$ * this ) {
|
---|
| 242 | return this->user_link.next;
|
---|
| 243 | }
|
---|
[6a490b2] | 244 |
|
---|
[e84ab3d] | 245 | static inline thread$ *& get_next( thread$ & this ) __attribute__((const)) {
|
---|
[be5f0a5] | 246 | return this.user_link.next;
|
---|
[0cf5b79] | 247 | }
|
---|
| 248 |
|
---|
[2856982c] | 249 | static inline tytagref( dlink(thread$), dlink(thread$) ) ?`inner( thread$ & this ) {
|
---|
| 250 | dlink(thread$) & b = this.user_link;
|
---|
| 251 | tytagref( dlink(thread$), dlink(thread$) ) result = { b };
|
---|
| 252 | return result;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[639e4fc] | 255 | static inline tytagref(struct __thread_user_link, dlink(thread$)) ?`inner( struct thread$ & this ) {
|
---|
| 256 | struct __thread_user_link & ib = this.cltr_link;
|
---|
[6a4ef0c] | 257 | dlink(thread$) & b = ib`inner;
|
---|
[639e4fc] | 258 | tytagref(struct __thread_user_link, dlink(thread$)) result = { b };
|
---|
[6a4ef0c] | 259 | return result;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[639e4fc] | 262 | P9_EMBEDDED(struct __thread_user_link, dlink(thread$))
|
---|
[cd5b58f] | 263 |
|
---|
[0cf5b79] | 264 | static inline void ?{}(__monitor_group_t & this) {
|
---|
[121be3e] | 265 | (this.data){0p};
|
---|
[0cf5b79] | 266 | (this.size){0};
|
---|
| 267 | (this.func){NULL};
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[e84ab3d] | 270 | static inline void ?{}(__monitor_group_t & this, struct monitor$ ** data, __lock_size_t size, fptr_t func) {
|
---|
[0cf5b79] | 271 | (this.data){data};
|
---|
| 272 | (this.size){size};
|
---|
| 273 | (this.func){func};
|
---|
[025278e] | 274 | }
|
---|
| 275 |
|
---|
[8c50aed] | 276 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) {
|
---|
[0cf5b79] | 277 | if( (lhs.data != 0) != (rhs.data != 0) ) return false;
|
---|
[025278e] | 278 | if( lhs.size != rhs.size ) return false;
|
---|
| 279 | if( lhs.func != rhs.func ) return false;
|
---|
| 280 |
|
---|
| 281 | // Check that all the monitors match
|
---|
| 282 | for( int i = 0; i < lhs.size; i++ ) {
|
---|
| 283 | // If not a match, check next function
|
---|
| 284 | if( lhs[i] != rhs[i] ) return false;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return true;
|
---|
| 288 | }
|
---|
[0cf5b79] | 289 |
|
---|
| 290 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
|
---|
| 291 | lhs.data = rhs.data;
|
---|
| 292 | lhs.size = rhs.size;
|
---|
| 293 | lhs.func = rhs.func;
|
---|
| 294 | }
|
---|
[025278e] | 295 | }
|
---|
| 296 | #endif
|
---|
[b18830e] | 297 |
|
---|
[5c81105] | 298 | #endif //_INVOKE_H_
|
---|
| 299 | #else //! defined(__CFA_INVOKE_PRIVATE__)
|
---|
| 300 | #ifndef _INVOKE_PRIVATE_H_
|
---|
| 301 | #define _INVOKE_PRIVATE_H_
|
---|
[b227f68] | 302 |
|
---|
[025278e] | 303 | struct machine_context_t {
|
---|
| 304 | void *SP;
|
---|
| 305 | void *FP;
|
---|
| 306 | void *PC;
|
---|
| 307 | };
|
---|
| 308 |
|
---|
| 309 | // assembler routines that performs the context switch
|
---|
[c7a900a] | 310 | extern void __cfactx_invoke_stub( void );
|
---|
| 311 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
|
---|
[212c2187] | 312 | // void CtxStore ( void * this ) asm ("CtxStore");
|
---|
| 313 | // void CtxRet ( void * dst ) asm ("CtxRet");
|
---|
[025278e] | 314 |
|
---|
[5c81105] | 315 | #endif //_INVOKE_PRIVATE_H_
|
---|
| 316 | #endif //! defined(__CFA_INVOKE_PRIVATE__)
|
---|
[0cf5b79] | 317 | #ifdef __cforall
|
---|
[5c81105] | 318 | }
|
---|
| 319 | #endif
|
---|
[6b0b624] | 320 |
|
---|
| 321 | // Local Variables: //
|
---|
| 322 | // mode: c //
|
---|
| 323 | // tab-width: 4 //
|
---|
| 324 | // End: //
|
---|