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