[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 |
---|
| 12 | // Last Modified On : Fri Jul 21 22:28:56 2017 |
---|
| 13 | // Update Count : 1 |
---|
[8def349] | 14 | // |
---|
| 15 | |
---|
[ea7d2b0] | 16 | #include "bits/defs.h" |
---|
| 17 | #include "bits/locks.h" |
---|
[78b3f52] | 18 | |
---|
[5c81105] | 19 | #ifdef __CFORALL__ |
---|
| 20 | extern "C" { |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | #if ! defined(__CFA_INVOKE_PRIVATE__) |
---|
[78b3f52] | 24 | #ifndef _INVOKE_H_ |
---|
| 25 | #define _INVOKE_H_ |
---|
| 26 | |
---|
[025278e] | 27 | typedef void (*fptr_t)(); |
---|
[513daec] | 28 | typedef int_fast16_t __lock_size_t; |
---|
[b18830e] | 29 | |
---|
[025278e] | 30 | struct __thread_queue_t { |
---|
| 31 | struct thread_desc * head; |
---|
| 32 | struct thread_desc ** tail; |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | struct __condition_stack_t { |
---|
| 36 | struct __condition_criterion_t * top; |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | #ifdef __CFORALL__ |
---|
| 40 | extern "Cforall" { |
---|
| 41 | void ?{}( struct __thread_queue_t & ); |
---|
| 42 | void append( struct __thread_queue_t &, struct thread_desc * ); |
---|
| 43 | struct thread_desc * pop_head( struct __thread_queue_t & ); |
---|
| 44 | struct thread_desc * remove( struct __thread_queue_t &, struct thread_desc ** ); |
---|
| 45 | |
---|
| 46 | void ?{}( struct __condition_stack_t & ); |
---|
| 47 | void push( struct __condition_stack_t &, struct __condition_criterion_t * ); |
---|
| 48 | struct __condition_criterion_t * pop( struct __condition_stack_t & ); |
---|
| 49 | } |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | struct coStack_t { |
---|
| 53 | // size of stack |
---|
[c1a9c86] | 54 | size_t size; |
---|
[025278e] | 55 | |
---|
| 56 | // pointer to stack |
---|
| 57 | void *storage; |
---|
| 58 | |
---|
| 59 | // stack grows towards stack limit |
---|
| 60 | void *limit; |
---|
| 61 | |
---|
| 62 | // base of stack |
---|
| 63 | void *base; |
---|
| 64 | |
---|
| 65 | // address of cfa_context_t |
---|
| 66 | void *context; |
---|
| 67 | |
---|
| 68 | // address of top of storage |
---|
| 69 | void *top; |
---|
| 70 | |
---|
| 71 | // whether or not the user allocated the stack |
---|
| 72 | bool userStack; |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; |
---|
| 76 | |
---|
| 77 | struct coroutine_desc { |
---|
| 78 | // stack information of the coroutine |
---|
| 79 | struct coStack_t stack; |
---|
| 80 | |
---|
| 81 | // textual name for coroutine/task, initialized by uC++ generated code |
---|
| 82 | const char *name; |
---|
| 83 | |
---|
| 84 | // copy of global UNIX variable errno |
---|
| 85 | int errno_; |
---|
| 86 | |
---|
| 87 | // current execution status for coroutine |
---|
| 88 | enum coroutine_state state; |
---|
| 89 | |
---|
| 90 | // first coroutine to resume this one |
---|
| 91 | struct coroutine_desc * starter; |
---|
| 92 | |
---|
| 93 | // last coroutine to resume this one |
---|
| 94 | struct coroutine_desc * last; |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | struct __waitfor_mask_t { |
---|
| 98 | // the index of the accepted function, -1 if none |
---|
| 99 | short * accepted; |
---|
| 100 | |
---|
| 101 | // list of acceptable functions, null if any |
---|
| 102 | struct __acceptable_t * clauses; |
---|
| 103 | |
---|
| 104 | // number of acceptable functions |
---|
[c1a9c86] | 105 | __lock_size_t size; |
---|
[025278e] | 106 | }; |
---|
| 107 | |
---|
| 108 | struct monitor_desc { |
---|
| 109 | // spinlock to protect internal data |
---|
[ea7d2b0] | 110 | struct __spinlock_t lock; |
---|
[025278e] | 111 | |
---|
| 112 | // current owner of the monitor |
---|
| 113 | struct thread_desc * owner; |
---|
| 114 | |
---|
| 115 | // queue of threads that are blocked waiting for the monitor |
---|
| 116 | struct __thread_queue_t entry_queue; |
---|
| 117 | |
---|
| 118 | // stack of conditions to run next once we exit the monitor |
---|
| 119 | struct __condition_stack_t signal_stack; |
---|
| 120 | |
---|
| 121 | // monitor routines can be called recursively, we need to keep track of that |
---|
| 122 | unsigned int recursion; |
---|
| 123 | |
---|
| 124 | // mask used to know if some thread is waiting for something while holding the monitor |
---|
| 125 | struct __waitfor_mask_t mask; |
---|
| 126 | |
---|
| 127 | // node used to signal the dtor in a waitfor dtor |
---|
| 128 | struct __condition_node_t * dtor_node; |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | struct __monitor_group_t { |
---|
| 132 | // currently held monitors |
---|
| 133 | struct monitor_desc ** list; |
---|
| 134 | |
---|
| 135 | // number of currently held monitors |
---|
[c1a9c86] | 136 | __lock_size_t size; |
---|
[025278e] | 137 | |
---|
| 138 | // last function that acquired monitors |
---|
[c1a9c86] | 139 | fptr_t func; |
---|
[025278e] | 140 | }; |
---|
| 141 | |
---|
| 142 | struct thread_desc { |
---|
| 143 | // Core threading fields |
---|
| 144 | // coroutine body used to store context |
---|
| 145 | struct coroutine_desc self_cor; |
---|
| 146 | |
---|
| 147 | // monitor body used for mutual exclusion |
---|
| 148 | struct monitor_desc self_mon; |
---|
| 149 | |
---|
| 150 | // pointer to monitor with sufficient lifetime for current monitors |
---|
| 151 | struct monitor_desc * self_mon_p; |
---|
| 152 | |
---|
| 153 | // monitors currently held by this thread |
---|
| 154 | struct __monitor_group_t monitors; |
---|
| 155 | |
---|
| 156 | // Link lists fields |
---|
| 157 | // instrusive link field for threads |
---|
| 158 | struct thread_desc * next; |
---|
[97e3296] | 159 | }; |
---|
[8118303] | 160 | |
---|
[b18830e] | 161 | #ifdef __CFORALL__ |
---|
| 162 | extern "Cforall" { |
---|
[025278e] | 163 | static inline monitor_desc * ?[?]( const __monitor_group_t & this, ptrdiff_t index ) { |
---|
| 164 | return this.list[index]; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { |
---|
| 168 | if( (lhs.list != 0) != (rhs.list != 0) ) return false; |
---|
| 169 | if( lhs.size != rhs.size ) return false; |
---|
| 170 | if( lhs.func != rhs.func ) return false; |
---|
| 171 | |
---|
| 172 | // Check that all the monitors match |
---|
| 173 | for( int i = 0; i < lhs.size; i++ ) { |
---|
| 174 | // If not a match, check next function |
---|
| 175 | if( lhs[i] != rhs[i] ) return false; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | return true; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | #endif |
---|
[b18830e] | 182 | |
---|
[5c81105] | 183 | #endif //_INVOKE_H_ |
---|
| 184 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 185 | #ifndef _INVOKE_PRIVATE_H_ |
---|
| 186 | #define _INVOKE_PRIVATE_H_ |
---|
[b227f68] | 187 | |
---|
[025278e] | 188 | struct machine_context_t { |
---|
| 189 | void *SP; |
---|
| 190 | void *FP; |
---|
| 191 | void *PC; |
---|
| 192 | }; |
---|
| 193 | |
---|
| 194 | // assembler routines that performs the context switch |
---|
| 195 | extern void CtxInvokeStub( void ); |
---|
| 196 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); |
---|
| 197 | |
---|
| 198 | #if defined( __x86_64__ ) |
---|
| 199 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 200 | "movq %%rsp,%0\n" \ |
---|
| 201 | "movq %%rbp,%1\n" \ |
---|
| 202 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 203 | #elif defined( __i386__ ) |
---|
| 204 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 205 | "movl %%esp,%0\n" \ |
---|
| 206 | "movl %%ebp,%1\n" \ |
---|
| 207 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 208 | #endif |
---|
[78b3f52] | 209 | |
---|
[5c81105] | 210 | #endif //_INVOKE_PRIVATE_H_ |
---|
| 211 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 212 | #ifdef __CFORALL__ |
---|
| 213 | } |
---|
| 214 | #endif |
---|
[6b0b624] | 215 | |
---|
| 216 | // Local Variables: // |
---|
| 217 | // mode: c // |
---|
| 218 | // tab-width: 4 // |
---|
| 219 | // End: // |
---|