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