source: libcfa/src/concurrency/invoke.h@ 36354b1

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
Last change on this file since 36354b1 was 63364d8, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed suspend_then since I believe it cannot be made correct

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