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