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