[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 |
|
---|
[0cf5b79] | 16 | #include "bits/containers.h"
|
---|
[ea7d2b0] | 17 | #include "bits/defs.h"
|
---|
| 18 | #include "bits/locks.h"
|
---|
[78b3f52] | 19 |
|
---|
[0cf5b79] | 20 | #ifdef __cforall
|
---|
[5c81105] | 21 | extern "C" {
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 | #if ! defined(__CFA_INVOKE_PRIVATE__)
|
---|
[78b3f52] | 25 | #ifndef _INVOKE_H_
|
---|
| 26 | #define _INVOKE_H_
|
---|
| 27 |
|
---|
[0cf5b79] | 28 | #ifdef __cforall
|
---|
[025278e] | 29 | extern "Cforall" {
|
---|
[0cf5b79] | 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 );
|
---|
[025278e] | 32 | }
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | struct coStack_t {
|
---|
| 36 | // size of stack
|
---|
[c1a9c86] | 37 | size_t size;
|
---|
[025278e] | 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
|
---|
[0cf5b79] | 85 | __small_array_t(struct __acceptable_t) __cfa_anonymous_object;
|
---|
[025278e] | 86 | };
|
---|
| 87 |
|
---|
| 88 | struct monitor_desc {
|
---|
| 89 | // spinlock to protect internal data
|
---|
[ea7d2b0] | 90 | struct __spinlock_t lock;
|
---|
[025278e] | 91 |
|
---|
| 92 | // current owner of the monitor
|
---|
| 93 | struct thread_desc * owner;
|
---|
| 94 |
|
---|
| 95 | // queue of threads that are blocked waiting for the monitor
|
---|
[0cf5b79] | 96 | __queue_t(struct thread_desc) entry_queue;
|
---|
[025278e] | 97 |
|
---|
| 98 | // stack of conditions to run next once we exit the monitor
|
---|
[0cf5b79] | 99 | __stack_t(struct __condition_criterion_t) signal_stack;
|
---|
[025278e] | 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
|
---|
[0cf5b79] | 113 | __small_array_t(monitor_desc*) __cfa_anonymous_object;
|
---|
[025278e] | 114 |
|
---|
| 115 | // last function that acquired monitors
|
---|
[c1a9c86] | 116 | fptr_t func;
|
---|
[025278e] | 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 | // monitor body used for mutual exclusion
|
---|
| 125 | struct monitor_desc self_mon;
|
---|
| 126 |
|
---|
| 127 | // pointer to monitor with sufficient lifetime for current monitors
|
---|
| 128 | struct monitor_desc * self_mon_p;
|
---|
| 129 |
|
---|
| 130 | // monitors currently held by this thread
|
---|
| 131 | struct __monitor_group_t monitors;
|
---|
| 132 |
|
---|
| 133 | // Link lists fields
|
---|
| 134 | // instrusive link field for threads
|
---|
| 135 | struct thread_desc * next;
|
---|
[97e3296] | 136 | };
|
---|
[8118303] | 137 |
|
---|
[0cf5b79] | 138 | #ifdef __cforall
|
---|
[b18830e] | 139 | extern "Cforall" {
|
---|
[0cf5b79] | 140 | static inline thread_desc * & get_next( thread_desc & this ) {
|
---|
| 141 | return this.next;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this );
|
---|
| 145 |
|
---|
| 146 | static inline void ?{}(__monitor_group_t & this) {
|
---|
| 147 | (this.data){NULL};
|
---|
| 148 | (this.size){0};
|
---|
| 149 | (this.func){NULL};
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) {
|
---|
| 153 | (this.data){data};
|
---|
| 154 | (this.size){size};
|
---|
| 155 | (this.func){func};
|
---|
[025278e] | 156 | }
|
---|
| 157 |
|
---|
| 158 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) {
|
---|
[0cf5b79] | 159 | if( (lhs.data != 0) != (rhs.data != 0) ) return false;
|
---|
[025278e] | 160 | if( lhs.size != rhs.size ) return false;
|
---|
| 161 | if( lhs.func != rhs.func ) return false;
|
---|
| 162 |
|
---|
| 163 | // Check that all the monitors match
|
---|
| 164 | for( int i = 0; i < lhs.size; i++ ) {
|
---|
| 165 | // If not a match, check next function
|
---|
| 166 | if( lhs[i] != rhs[i] ) return false;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | return true;
|
---|
| 170 | }
|
---|
[0cf5b79] | 171 |
|
---|
| 172 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
|
---|
| 173 | lhs.data = rhs.data;
|
---|
| 174 | lhs.size = rhs.size;
|
---|
| 175 | lhs.func = rhs.func;
|
---|
| 176 | }
|
---|
[025278e] | 177 | }
|
---|
| 178 | #endif
|
---|
[b18830e] | 179 |
|
---|
[5c81105] | 180 | #endif //_INVOKE_H_
|
---|
| 181 | #else //! defined(__CFA_INVOKE_PRIVATE__)
|
---|
| 182 | #ifndef _INVOKE_PRIVATE_H_
|
---|
| 183 | #define _INVOKE_PRIVATE_H_
|
---|
[b227f68] | 184 |
|
---|
[025278e] | 185 | struct machine_context_t {
|
---|
| 186 | void *SP;
|
---|
| 187 | void *FP;
|
---|
| 188 | void *PC;
|
---|
| 189 | };
|
---|
| 190 |
|
---|
| 191 | // assembler routines that performs the context switch
|
---|
| 192 | extern void CtxInvokeStub( void );
|
---|
| 193 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch");
|
---|
| 194 |
|
---|
| 195 | #if defined( __x86_64__ )
|
---|
| 196 | #define CtxGet( ctx ) __asm__ ( \
|
---|
| 197 | "movq %%rsp,%0\n" \
|
---|
| 198 | "movq %%rbp,%1\n" \
|
---|
| 199 | : "=rm" (ctx.SP), "=rm" (ctx.FP) )
|
---|
| 200 | #elif defined( __i386__ )
|
---|
| 201 | #define CtxGet( ctx ) __asm__ ( \
|
---|
| 202 | "movl %%esp,%0\n" \
|
---|
| 203 | "movl %%ebp,%1\n" \
|
---|
| 204 | : "=rm" (ctx.SP), "=rm" (ctx.FP) )
|
---|
| 205 | #endif
|
---|
[78b3f52] | 206 |
|
---|
[5c81105] | 207 | #endif //_INVOKE_PRIVATE_H_
|
---|
| 208 | #endif //! defined(__CFA_INVOKE_PRIVATE__)
|
---|
[0cf5b79] | 209 | #ifdef __cforall
|
---|
[5c81105] | 210 | }
|
---|
| 211 | #endif
|
---|
[6b0b624] | 212 |
|
---|
| 213 | // Local Variables: //
|
---|
| 214 | // mode: c //
|
---|
| 215 | // tab-width: 4 //
|
---|
| 216 | // End: //
|
---|