| [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 |  | 
|---|
| [5c81105] | 16 | #include <stdbool.h> | 
|---|
|  | 17 | #include <stdint.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 |  | 
|---|
| [596f987b] | 27 | #define unlikely(x)    __builtin_expect(!!(x), 0) | 
|---|
| [8def349] | 28 | #define thread_local _Thread_local | 
|---|
| [bd98b58] | 29 |  | 
|---|
| [90c4df0] | 30 | typedef void (*fptr_t)(); | 
|---|
|  | 31 |  | 
|---|
| [db6f06a] | 32 | struct spinlock { | 
|---|
|  | 33 | volatile int lock; | 
|---|
| [1c273d0] | 34 | #ifdef __CFA_DEBUG__ | 
|---|
| [b227f68] | 35 | const char * prev_name; | 
|---|
|  | 36 | void* prev_thrd; | 
|---|
| [1c273d0] | 37 | #endif | 
|---|
| [db6f06a] | 38 | }; | 
|---|
|  | 39 |  | 
|---|
| [5ea06d6] | 40 | struct __thread_queue_t { | 
|---|
| [348006f] | 41 | struct thread_desc * head; | 
|---|
|  | 42 | struct thread_desc ** tail; | 
|---|
| [bd98b58] | 43 | }; | 
|---|
|  | 44 |  | 
|---|
| [0c78741] | 45 | struct __condition_stack_t { | 
|---|
|  | 46 | struct __condition_criterion_t * top; | 
|---|
| [690f13c] | 47 | }; | 
|---|
|  | 48 |  | 
|---|
| [bd98b58] | 49 | #ifdef __CFORALL__ | 
|---|
|  | 50 | extern "Cforall" { | 
|---|
| [242a902] | 51 | void ?{}( struct __thread_queue_t & ); | 
|---|
| [5ea06d6] | 52 | void append( struct __thread_queue_t *, struct thread_desc * ); | 
|---|
|  | 53 | struct thread_desc * pop_head( struct __thread_queue_t * ); | 
|---|
| [90c4df0] | 54 | struct thread_desc * remove( struct __thread_queue_t *, struct thread_desc ** ); | 
|---|
| [db6f06a] | 55 |  | 
|---|
| [242a902] | 56 | void ?{}( struct __condition_stack_t & ); | 
|---|
| [0c78741] | 57 | void push( struct __condition_stack_t *, struct __condition_criterion_t * ); | 
|---|
|  | 58 | struct __condition_criterion_t * pop( struct __condition_stack_t * ); | 
|---|
| [690f13c] | 59 |  | 
|---|
| [242a902] | 60 | void ?{}(spinlock & this); | 
|---|
|  | 61 | void ^?{}(spinlock & this); | 
|---|
| [bd98b58] | 62 | } | 
|---|
|  | 63 | #endif | 
|---|
| [596f987b] | 64 |  | 
|---|
|  | 65 | struct coStack_t { | 
|---|
| [5ea06d6] | 66 | unsigned int size;                        // size of stack | 
|---|
|  | 67 | void *storage;                            // pointer to stack | 
|---|
|  | 68 | void *limit;                              // stack grows towards stack limit | 
|---|
|  | 69 | void *base;                               // base of stack | 
|---|
|  | 70 | void *context;                            // address of cfa_context_t | 
|---|
|  | 71 | void *top;                                // address of top of storage | 
|---|
|  | 72 | bool userStack;                           // whether or not the user allocated the stack | 
|---|
| [5c81105] | 73 | }; | 
|---|
| [78b3f52] | 74 |  | 
|---|
| [ee897e4b] | 75 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; | 
|---|
| [78b3f52] | 76 |  | 
|---|
| [c3acb841] | 77 | struct coroutine_desc { | 
|---|
| [5ea06d6] | 78 | struct coStack_t stack;                   // stack information of the coroutine | 
|---|
|  | 79 | const char *name;                         // textual name for coroutine/task, initialized by uC++ generated code | 
|---|
|  | 80 | int errno_;                               // copy of global UNIX variable errno | 
|---|
|  | 81 | enum coroutine_state state;               // current execution status for coroutine | 
|---|
| [39fea2f] | 82 | struct coroutine_desc * starter;          // first coroutine to resume this one | 
|---|
|  | 83 | struct coroutine_desc * last;             // last coroutine to resume this one | 
|---|
| [5c81105] | 84 | }; | 
|---|
| [78b3f52] | 85 |  | 
|---|
| [6ae8c92] | 86 | struct __waitfor_mask_t { | 
|---|
|  | 87 | short * accepted;                         // the index of the accepted function, -1 if none | 
|---|
|  | 88 | struct __acceptable_t * clauses;          // list of acceptable functions, null if any | 
|---|
|  | 89 | short size;                               // number of acceptable functions | 
|---|
|  | 90 | }; | 
|---|
|  | 91 |  | 
|---|
| [cb0e6de] | 92 | struct monitor_desc { | 
|---|
| [5ea06d6] | 93 | struct spinlock lock;                     // spinlock to protect internal data | 
|---|
|  | 94 | struct thread_desc * owner;               // current owner of the monitor | 
|---|
|  | 95 | struct __thread_queue_t entry_queue;      // queue of threads that are blocked waiting for the monitor | 
|---|
| [0c78741] | 96 | struct __condition_stack_t signal_stack;  // stack of conditions to run next once we exit the monitor | 
|---|
| [5ea06d6] | 97 | unsigned int recursion;                   // monitor routines can be called recursively, we need to keep track of that | 
|---|
| [6ae8c92] | 98 | struct __waitfor_mask_t mask;               // mask used to know if some thread is waiting for something while holding the monitor | 
|---|
| [b18830e] | 99 | }; | 
|---|
|  | 100 |  | 
|---|
| [6ae8c92] | 101 | struct __monitor_group_t { | 
|---|
| [b18830e] | 102 | struct monitor_desc ** list;              // currently held monitors | 
|---|
|  | 103 | short                  size;              // number of currently held monitors | 
|---|
|  | 104 | fptr_t                 func;              // last function that acquired monitors | 
|---|
|  | 105 | }; | 
|---|
| [cb0e6de] | 106 |  | 
|---|
| [348006f] | 107 | struct thread_desc { | 
|---|
| [90c4df0] | 108 | // Core threading fields | 
|---|
| [b18830e] | 109 | struct coroutine_desc  self_cor;          // coroutine body used to store context | 
|---|
|  | 110 | struct monitor_desc    self_mon;          // monitor body used for mutual exclusion | 
|---|
|  | 111 | struct monitor_desc *  self_mon_p;        // pointer to monitor with sufficient lifetime for current monitors | 
|---|
| [206de5a] | 112 | struct __monitor_group_t monitors;        // monitors currently held by this thread | 
|---|
| [90c4df0] | 113 |  | 
|---|
|  | 114 | // Link lists fields | 
|---|
| [5ea06d6] | 115 | struct thread_desc * next;                // instrusive link field for threads | 
|---|
| [90c4df0] | 116 |  | 
|---|
| [b18830e] | 117 |  | 
|---|
| [97e3296] | 118 | }; | 
|---|
| [8118303] | 119 |  | 
|---|
| [b18830e] | 120 | #ifdef __CFORALL__ | 
|---|
|  | 121 | extern "Cforall" { | 
|---|
| [6ae8c92] | 122 | static inline monitor_desc * ?[?]( const __monitor_group_t & this, ptrdiff_t index ) { | 
|---|
| [b18830e] | 123 | return this.list[index]; | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
| [6ae8c92] | 126 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { | 
|---|
| [4cc9b13] | 127 | if( (lhs.list != 0) != (rhs.list != 0) ) return false; | 
|---|
| [b18830e] | 128 | if( lhs.size != rhs.size ) return false; | 
|---|
|  | 129 | if( lhs.func != rhs.func ) return false; | 
|---|
|  | 130 |  | 
|---|
|  | 131 | // Check that all the monitors match | 
|---|
|  | 132 | for( int i = 0; i < lhs.size; i++ ) { | 
|---|
|  | 133 | // If not a match, check next function | 
|---|
|  | 134 | if( lhs[i] != rhs[i] ) return false; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | return true; | 
|---|
|  | 138 | } | 
|---|
|  | 139 | } | 
|---|
|  | 140 | #endif | 
|---|
|  | 141 |  | 
|---|
| [5c81105] | 142 | #endif //_INVOKE_H_ | 
|---|
|  | 143 | #else //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
|  | 144 | #ifndef _INVOKE_PRIVATE_H_ | 
|---|
|  | 145 | #define _INVOKE_PRIVATE_H_ | 
|---|
| [b227f68] | 146 |  | 
|---|
| [596f987b] | 147 | struct machine_context_t { | 
|---|
| [5c81105] | 148 | void *SP; | 
|---|
|  | 149 | void *FP; | 
|---|
|  | 150 | void *PC; | 
|---|
|  | 151 | }; | 
|---|
| [78b3f52] | 152 |  | 
|---|
| [5c81105] | 153 | // assembler routines that performs the context switch | 
|---|
| [b58a5772] | 154 | extern void CtxInvokeStub( void ); | 
|---|
| [eb2e723] | 155 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); | 
|---|
| [f32e53e] | 156 |  | 
|---|
|  | 157 | #if   defined( __x86_64__ ) | 
|---|
|  | 158 | #define CtxGet( ctx ) __asm__ ( \ | 
|---|
|  | 159 | "movq %%rsp,%0\n"   \ | 
|---|
|  | 160 | "movq %%rbp,%1\n"   \ | 
|---|
|  | 161 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) | 
|---|
|  | 162 | #elif defined( __i386__ ) | 
|---|
|  | 163 | #define CtxGet( ctx ) __asm__ ( \ | 
|---|
|  | 164 | "movl %%esp,%0\n"   \ | 
|---|
|  | 165 | "movl %%ebp,%1\n"   \ | 
|---|
|  | 166 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) | 
|---|
|  | 167 | #endif | 
|---|
| [78b3f52] | 168 |  | 
|---|
| [5c81105] | 169 | #endif //_INVOKE_PRIVATE_H_ | 
|---|
|  | 170 | #endif //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
|  | 171 | #ifdef __CFORALL__ | 
|---|
|  | 172 | } | 
|---|
|  | 173 | #endif | 
|---|
| [6b0b624] | 174 |  | 
|---|
|  | 175 | // Local Variables: // | 
|---|
|  | 176 | // mode: c // | 
|---|
|  | 177 | // tab-width: 4 // | 
|---|
|  | 178 | // End: // | 
|---|