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