source: libcfa/src/concurrency/invoke.h@ b2f6113

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since b2f6113 was b2f6113, checked in by tdelisle <tdelisle@…>, 7 years ago

Swapped memory storage for context and stack information inside the coroutine implementation

  • Property mode set to 100644
File size: 8.1 KB
Line 
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
21extern "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 thread_desc * volatile this_thread;
53 struct processor * volatile this_processor;
54
55 struct {
56 volatile unsigned short disable_count;
57 volatile bool enabled;
58 volatile bool in_progress;
59 } preemption_state;
60 } kernelTLS __attribute__ ((tls_model ( "initial-exec" )));
61 }
62 #endif
63
64 struct __stack_context_t {
65 void * SP;
66
67 void * FP;
68 // copy of global UNIX variable errno
69 int errno_;
70 };
71
72 // low adresses : +----------------------+ <- start of allocation
73 // | optional guard page |
74 // +----------------------+ <- __stack_t.limit
75 // | |
76 // | /\ /\ /\ |
77 // | || || || |
78 // | |
79 // | program stack |
80 // | |
81 // __stack_info_t.storage -> +----------------------+ <- __stack_t.base
82 // | __stack_t |
83 // high adresses : +----------------------+ <- end of allocation
84
85 struct __stack_t {
86 // size of stack
87 size_t size;
88
89 // stack grows towards stack limit
90 void * limit;
91
92 // base of stack
93 void * base;
94 };
95
96 struct __stack_info_t {
97 // pointer to stack
98 struct __stack_t * storage;
99
100 // whether or not the user allocated the stack
101 bool userStack;
102 };
103
104 enum coroutine_state { Halted, Start, Inactive, Active, Primed };
105
106 struct coroutine_desc {
107 // context that is switch during a CtxSwitch
108 struct __stack_context_t context;
109
110 // stack information of the coroutine
111 struct __stack_info_t stack;
112
113 // textual name for coroutine/task, initialized by uC++ generated code
114 const char * name;
115
116 // current execution status for coroutine
117 enum coroutine_state state;
118
119 // first coroutine to resume this one
120 struct coroutine_desc * starter;
121
122 // last coroutine to resume this one
123 struct coroutine_desc * last;
124
125 // If non-null stack must be unwound with this exception
126 struct _Unwind_Exception * cancellation;
127
128 };
129
130 // struct which calls the monitor is accepting
131 struct __waitfor_mask_t {
132 // the index of the accepted function, -1 if none
133 short * accepted;
134
135 // list of acceptable functions, null if any
136 __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
137 };
138
139 struct monitor_desc {
140 // spinlock to protect internal data
141 struct __spinlock_t lock;
142
143 // current owner of the monitor
144 struct thread_desc * owner;
145
146 // queue of threads that are blocked waiting for the monitor
147 __queue_t(struct thread_desc) entry_queue;
148
149 // stack of conditions to run next once we exit the monitor
150 __stack_t(struct __condition_criterion_t) signal_stack;
151
152 // monitor routines can be called recursively, we need to keep track of that
153 unsigned int recursion;
154
155 // mask used to know if some thread is waiting for something while holding the monitor
156 struct __waitfor_mask_t mask;
157
158 // node used to signal the dtor in a waitfor dtor
159 struct __condition_node_t * dtor_node;
160 };
161
162 struct __monitor_group_t {
163 // currently held monitors
164 __cfa_anonymous_object( __small_array_t(monitor_desc*) );
165
166 // last function that acquired monitors
167 fptr_t func;
168 };
169
170 struct thread_desc {
171 // Core threading fields
172 // coroutine body used to store context
173 struct coroutine_desc self_cor;
174
175 // current active context
176 struct coroutine_desc * curr_cor;
177
178 // monitor body used for mutual exclusion
179 struct monitor_desc self_mon;
180
181 // pointer to monitor with sufficient lifetime for current monitors
182 struct monitor_desc * self_mon_p;
183
184 // pointer to the cluster on which the thread is running
185 struct cluster * curr_cluster;
186
187 // monitors currently held by this thread
188 struct __monitor_group_t monitors;
189
190 // Link lists fields
191 // instrusive link field for threads
192 struct thread_desc * next;
193
194 struct {
195 struct thread_desc * next;
196 struct thread_desc * prev;
197 } node;
198 };
199
200 #ifdef __cforall
201 extern "Cforall" {
202 static inline struct coroutine_desc * volatile active_coroutine() { return TL_GET( this_thread )->curr_cor; }
203 static inline struct thread_desc * volatile active_thread () { return TL_GET( this_thread ); }
204 static inline struct processor * volatile active_processor() { return TL_GET( this_processor ); } // UNSAFE
205
206 static inline thread_desc * & get_next( thread_desc & this ) {
207 return this.next;
208 }
209
210 static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) {
211 return this.node.[next, prev];
212 }
213
214 static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this );
215
216 static inline void ?{}(__monitor_group_t & this) {
217 (this.data){NULL};
218 (this.size){0};
219 (this.func){NULL};
220 }
221
222 static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) {
223 (this.data){data};
224 (this.size){size};
225 (this.func){func};
226 }
227
228 static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) {
229 if( (lhs.data != 0) != (rhs.data != 0) ) return false;
230 if( lhs.size != rhs.size ) return false;
231 if( lhs.func != rhs.func ) return false;
232
233 // Check that all the monitors match
234 for( int i = 0; i < lhs.size; i++ ) {
235 // If not a match, check next function
236 if( lhs[i] != rhs[i] ) return false;
237 }
238
239 return true;
240 }
241
242 static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
243 lhs.data = rhs.data;
244 lhs.size = rhs.size;
245 lhs.func = rhs.func;
246 }
247 }
248 #endif
249
250#endif //_INVOKE_H_
251#else //! defined(__CFA_INVOKE_PRIVATE__)
252#ifndef _INVOKE_PRIVATE_H_
253#define _INVOKE_PRIVATE_H_
254
255 struct machine_context_t {
256 void *SP;
257 void *FP;
258 void *PC;
259 };
260
261 // assembler routines that performs the context switch
262 extern void CtxInvokeStub( void );
263 void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch");
264 // void CtxStore ( void * this ) asm ("CtxStore");
265 // void CtxRet ( void * dst ) asm ("CtxRet");
266
267 #if defined( __i386 )
268 #define CtxGet( ctx ) __asm__ ( \
269 "movl %%esp,%0\n" \
270 "movl %%ebp,%1\n" \
271 : "=rm" (ctx.SP), "=rm" (ctx.FP) )
272 #elif defined( __x86_64 )
273 #define CtxGet( ctx ) __asm__ ( \
274 "movq %%rsp,%0\n" \
275 "movq %%rbp,%1\n" \
276 : "=rm" (ctx.SP), "=rm" (ctx.FP) )
277 #elif defined( __ARM_ARCH )
278 #define CtxGet( ctx ) __asm__ ( \
279 "mov %0,%%sp\n" \
280 "mov %1,%%r11\n" \
281 : "=rm" (ctx.SP), "=rm" (ctx.FP) )
282 #else
283 #error unknown hardware architecture
284 #endif
285
286#endif //_INVOKE_PRIVATE_H_
287#endif //! defined(__CFA_INVOKE_PRIVATE__)
288#ifdef __cforall
289}
290#endif
291
292// Local Variables: //
293// mode: c //
294// tab-width: 4 //
295// End: //
Note: See TracBrowser for help on using the repository browser.