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