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