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 : Tue Jan 23 14:55:46 2018 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include "bits/containers.h" |
---|
17 | #include "bits/defs.h" |
---|
18 | #include "bits/locks.h" |
---|
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 __cforall |
---|
29 | extern "Cforall" { |
---|
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 ); |
---|
32 | } |
---|
33 | #endif |
---|
34 | |
---|
35 | struct coStack_t { |
---|
36 | // size of stack |
---|
37 | size_t size; |
---|
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 |
---|
85 | __small_array_t(struct __acceptable_t) __cfa_anonymous_object; |
---|
86 | }; |
---|
87 | |
---|
88 | struct monitor_desc { |
---|
89 | // spinlock to protect internal data |
---|
90 | struct __spinlock_t lock; |
---|
91 | |
---|
92 | // current owner of the monitor |
---|
93 | struct thread_desc * owner; |
---|
94 | |
---|
95 | // queue of threads that are blocked waiting for the monitor |
---|
96 | __queue_t(struct thread_desc) entry_queue; |
---|
97 | |
---|
98 | // stack of conditions to run next once we exit the monitor |
---|
99 | __stack_t(struct __condition_criterion_t) signal_stack; |
---|
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 |
---|
113 | __small_array_t(monitor_desc*) __cfa_anonymous_object; |
---|
114 | |
---|
115 | // last function that acquired monitors |
---|
116 | fptr_t func; |
---|
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; |
---|
136 | |
---|
137 | __cfaabi_dbg_debug_do( |
---|
138 | // instrusive link field for debugging |
---|
139 | struct thread_desc * dbg_next; |
---|
140 | struct thread_desc * dbg_prev; |
---|
141 | ) |
---|
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 struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this ); |
---|
151 | |
---|
152 | static inline void ?{}(__monitor_group_t & this) { |
---|
153 | (this.data){NULL}; |
---|
154 | (this.size){0}; |
---|
155 | (this.func){NULL}; |
---|
156 | } |
---|
157 | |
---|
158 | static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) { |
---|
159 | (this.data){data}; |
---|
160 | (this.size){size}; |
---|
161 | (this.func){func}; |
---|
162 | } |
---|
163 | |
---|
164 | static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) { |
---|
165 | if( (lhs.data != 0) != (rhs.data != 0) ) return false; |
---|
166 | if( lhs.size != rhs.size ) return false; |
---|
167 | if( lhs.func != rhs.func ) return false; |
---|
168 | |
---|
169 | // Check that all the monitors match |
---|
170 | for( int i = 0; i < lhs.size; i++ ) { |
---|
171 | // If not a match, check next function |
---|
172 | if( lhs[i] != rhs[i] ) return false; |
---|
173 | } |
---|
174 | |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) { |
---|
179 | lhs.data = rhs.data; |
---|
180 | lhs.size = rhs.size; |
---|
181 | lhs.func = rhs.func; |
---|
182 | } |
---|
183 | } |
---|
184 | #endif |
---|
185 | |
---|
186 | #endif //_INVOKE_H_ |
---|
187 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
188 | #ifndef _INVOKE_PRIVATE_H_ |
---|
189 | #define _INVOKE_PRIVATE_H_ |
---|
190 | |
---|
191 | struct machine_context_t { |
---|
192 | void *SP; |
---|
193 | void *FP; |
---|
194 | void *PC; |
---|
195 | }; |
---|
196 | |
---|
197 | // assembler routines that performs the context switch |
---|
198 | extern void CtxInvokeStub( void ); |
---|
199 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); |
---|
200 | |
---|
201 | #if defined( __x86_64__ ) |
---|
202 | #define CtxGet( ctx ) __asm__ ( \ |
---|
203 | "movq %%rsp,%0\n" \ |
---|
204 | "movq %%rbp,%1\n" \ |
---|
205 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
206 | #elif defined( __i386__ ) |
---|
207 | #define CtxGet( ctx ) __asm__ ( \ |
---|
208 | "movl %%esp,%0\n" \ |
---|
209 | "movl %%ebp,%1\n" \ |
---|
210 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
211 | #elif defined( __ARM_ARCH ) |
---|
212 | #define CtxGet( ctx ) __asm__ ( \ |
---|
213 | "mov %0,%%sp\n" \ |
---|
214 | "mov %1,%%r11\n" \ |
---|
215 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
216 | #endif |
---|
217 | |
---|
218 | #endif //_INVOKE_PRIVATE_H_ |
---|
219 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
220 | #ifdef __cforall |
---|
221 | } |
---|
222 | #endif |
---|
223 | |
---|
224 | // Local Variables: // |
---|
225 | // mode: c // |
---|
226 | // tab-width: 4 // |
---|
227 | // End: // |
---|