1 | #include "locks.hfa"
|
---|
2 | #include "kernel_private.hfa"
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <stdio.h>
|
---|
5 |
|
---|
6 | #include <kernel.hfa>
|
---|
7 | #include <stdlib.hfa>
|
---|
8 | #include <thread.hfa>
|
---|
9 |
|
---|
10 | ///////////////////////////////////////////////////////////////////
|
---|
11 | //// info_thread
|
---|
12 | ///////////////////////////////////////////////////////////////////
|
---|
13 |
|
---|
14 | forall(dtype L | is_blocking_lock(L)) {
|
---|
15 | void ?{}( info_thread(L) & this, $thread * t ) {
|
---|
16 | ((Seqable &) this){};
|
---|
17 | this.t = t;
|
---|
18 | this.lock = 0p;
|
---|
19 | this.listed = false;
|
---|
20 | }
|
---|
21 |
|
---|
22 | void ?{}( info_thread(L) & this, $thread * t, uintptr_t info ) {
|
---|
23 | ((Seqable &) this){};
|
---|
24 | this.t = t;
|
---|
25 | this.info = info;
|
---|
26 | this.lock = 0p;
|
---|
27 | this.listed = false;
|
---|
28 | }
|
---|
29 |
|
---|
30 | void ^?{}( info_thread(L) & this ){ }
|
---|
31 |
|
---|
32 | info_thread(L) *& Back( info_thread(L) * this ) {
|
---|
33 | return (info_thread(L) *)Back( (Seqable *)this );
|
---|
34 | }
|
---|
35 |
|
---|
36 | info_thread(L) *& Next( info_thread(L) * this ) {
|
---|
37 | return (info_thread(L) *)Next( (Colable *)this );
|
---|
38 | }
|
---|
39 |
|
---|
40 | bool listed( info_thread(L) * this ) {
|
---|
41 | return Next( (Colable *)this ) != 0p;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | ///////////////////////////////////////////////////////////////////
|
---|
46 | //// Blocking Locks
|
---|
47 | ///////////////////////////////////////////////////////////////////
|
---|
48 |
|
---|
49 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ) {
|
---|
50 | this.lock{};
|
---|
51 | this.blocked_threads{};
|
---|
52 | this.wait_count = 0;
|
---|
53 | this.multi_acquisition = multi_acquisition;
|
---|
54 | this.strict_owner = strict_owner;
|
---|
55 | this.owner = 0p;
|
---|
56 | this.recursion_count = 0;
|
---|
57 | }
|
---|
58 |
|
---|
59 | void ^?{}( blocking_lock & this ) {}
|
---|
60 | void ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };}
|
---|
61 | void ^?{}( single_acquisition_lock & this ) {}
|
---|
62 | void ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };}
|
---|
63 | void ^?{}( owner_lock & this ) {}
|
---|
64 | void ?{}( multiple_acquisition_lock & this ) {((blocking_lock &)this){ true, false };}
|
---|
65 | void ^?{}( multiple_acquisition_lock & this ) {}
|
---|
66 |
|
---|
67 | void lock( blocking_lock & this ) with( this ) {
|
---|
68 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
69 | if ( owner == active_thread() && !multi_acquisition) {
|
---|
70 | abort("A single acquisition lock holder attempted to reacquire the lock resulting in a deadlock.");
|
---|
71 | } else if ( owner != 0p && owner != active_thread() ) {
|
---|
72 | addTail( blocked_threads, *active_thread() );
|
---|
73 | wait_count++;
|
---|
74 | unlock( lock );
|
---|
75 | park( );
|
---|
76 | } else if ( owner == active_thread() && multi_acquisition ) {
|
---|
77 | recursion_count++;
|
---|
78 | unlock( lock );
|
---|
79 | } else {
|
---|
80 | owner = active_thread();
|
---|
81 | recursion_count = 1;
|
---|
82 | unlock( lock );
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool try_lock( blocking_lock & this ) with( this ) {
|
---|
87 | bool ret = false;
|
---|
88 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
89 | if ( owner == 0p ) {
|
---|
90 | owner = active_thread();
|
---|
91 | recursion_count = 1;
|
---|
92 | ret = true;
|
---|
93 | } else if ( owner == active_thread() && multi_acquisition ) {
|
---|
94 | recursion_count++;
|
---|
95 | ret = true;
|
---|
96 | }
|
---|
97 | unlock( lock );
|
---|
98 | return ret;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void unlock_error_check( blocking_lock & this ) with( this ) {
|
---|
102 | if ( owner == 0p ){ // no owner implies lock isn't held
|
---|
103 | abort( "There was an attempt to release a lock that isn't held" );
|
---|
104 | } else if ( strict_owner && owner != active_thread() ) {
|
---|
105 | abort( "A thread other than the owner attempted to release an owner lock" );
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
|
---|
110 | $thread * t = &dropHead( blocked_threads );
|
---|
111 | owner = t;
|
---|
112 | recursion_count = ( t ? 1 : 0 );
|
---|
113 | wait_count--;
|
---|
114 | unpark( t );
|
---|
115 | }
|
---|
116 |
|
---|
117 | void unlock( blocking_lock & this ) with( this ) {
|
---|
118 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
119 | unlock_error_check( this );
|
---|
120 | recursion_count--;
|
---|
121 | if ( recursion_count == 0 ) {
|
---|
122 | pop_and_set_new_owner( this );
|
---|
123 | }
|
---|
124 | unlock( lock );
|
---|
125 | }
|
---|
126 |
|
---|
127 | size_t wait_count( blocking_lock & this ) with( this ) {
|
---|
128 | return wait_count;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void set_recursion_count( blocking_lock & this, size_t recursion ) with( this ) {
|
---|
132 | recursion_count = recursion;
|
---|
133 | }
|
---|
134 |
|
---|
135 | size_t get_recursion_count( blocking_lock & this ) with( this ) {
|
---|
136 | return recursion_count;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void add_( blocking_lock & this, $thread * t ) with( this ) {
|
---|
140 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
141 | if ( owner != 0p ) {
|
---|
142 | addTail( blocked_threads, *t );
|
---|
143 | wait_count++;
|
---|
144 | unlock( lock );
|
---|
145 | } else {
|
---|
146 | owner = t;
|
---|
147 | recursion_count = 1;
|
---|
148 | unpark( t );
|
---|
149 | unlock( lock );
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | void remove_( blocking_lock & this ) with( this ) {
|
---|
154 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
155 | unlock_error_check( this );
|
---|
156 | pop_and_set_new_owner( this );
|
---|
157 | unlock( lock );
|
---|
158 | }
|
---|
159 |
|
---|
160 | ///////////////////////////////////////////////////////////////////
|
---|
161 | //// Overloaded routines for traits
|
---|
162 | ///////////////////////////////////////////////////////////////////
|
---|
163 |
|
---|
164 | // This is temporary until an inheritance bug is fixed
|
---|
165 |
|
---|
166 | void lock( single_acquisition_lock & this ){ lock( (blocking_lock &)this ); }
|
---|
167 | void unlock( single_acquisition_lock & this ){ unlock( (blocking_lock &)this ); }
|
---|
168 | void add_( single_acquisition_lock & this, struct $thread * t ){ add_( (blocking_lock &)this, t ); }
|
---|
169 | void remove_( single_acquisition_lock & this ){ remove_( (blocking_lock &)this ); }
|
---|
170 | void set_recursion_count( single_acquisition_lock & this, size_t recursion ){ set_recursion_count( (blocking_lock &)this, recursion ); }
|
---|
171 | size_t get_recursion_count( single_acquisition_lock & this ){ return get_recursion_count( (blocking_lock &)this ); }
|
---|
172 |
|
---|
173 | void lock( owner_lock & this ){ lock( (blocking_lock &)this ); }
|
---|
174 | void unlock( owner_lock & this ){ unlock( (blocking_lock &)this ); }
|
---|
175 | void add_( owner_lock & this, struct $thread * t ){ add_( (blocking_lock &)this, t ); }
|
---|
176 | void remove_( owner_lock & this ){ remove_( (blocking_lock &)this ); }
|
---|
177 | void set_recursion_count( owner_lock & this, size_t recursion ){ set_recursion_count( (blocking_lock &)this, recursion ); }
|
---|
178 | size_t get_recursion_count( owner_lock & this ){ return get_recursion_count( (blocking_lock &)this ); }
|
---|
179 |
|
---|
180 | void lock( multiple_acquisition_lock & this ){ lock( (blocking_lock &)this ); }
|
---|
181 | void unlock( multiple_acquisition_lock & this ){ unlock( (blocking_lock &)this ); }
|
---|
182 | void add_( multiple_acquisition_lock & this, struct $thread * t ){ add_( (blocking_lock &)this, t ); }
|
---|
183 | void remove_( multiple_acquisition_lock & this ){ remove_( (blocking_lock &)this ); }
|
---|
184 | void set_recursion_count( multiple_acquisition_lock & this, size_t recursion ){ set_recursion_count( (blocking_lock &)this, recursion ); }
|
---|
185 | size_t get_recursion_count( multiple_acquisition_lock & this ){ return get_recursion_count( (blocking_lock &)this ); }
|
---|
186 |
|
---|
187 | ///////////////////////////////////////////////////////////////////
|
---|
188 | //// condition variable
|
---|
189 | ///////////////////////////////////////////////////////////////////
|
---|
190 |
|
---|
191 | forall(dtype L | is_blocking_lock(L)) {
|
---|
192 |
|
---|
193 | void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
|
---|
194 | // This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin.
|
---|
195 | lock( cond->lock __cfaabi_dbg_ctx2 );
|
---|
196 |
|
---|
197 | if ( i->listed ) { // is thread on queue
|
---|
198 | cond->last_thread = i; // REMOVE THIS AFTER DEBUG
|
---|
199 | remove( cond->blocked_threads, *i ); //remove this thread O(1)
|
---|
200 | cond->count--;
|
---|
201 | if( !i->lock ) {
|
---|
202 | unpark( i->t );
|
---|
203 | } else {
|
---|
204 | add_(*i->lock, i->t); // call lock's add_
|
---|
205 | }
|
---|
206 | }
|
---|
207 | unlock( cond->lock );
|
---|
208 | }
|
---|
209 |
|
---|
210 | void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
|
---|
211 |
|
---|
212 | void ?{}( condition_variable(L) & this ){
|
---|
213 | this.lock{};
|
---|
214 | this.blocked_threads{};
|
---|
215 | this.count = 0;
|
---|
216 | this.last_thread = 0p; // REMOVE AFTER DEBUG
|
---|
217 | }
|
---|
218 |
|
---|
219 | void ^?{}( condition_variable(L) & this ){ }
|
---|
220 |
|
---|
221 | void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback ) {
|
---|
222 | this.alarm_node{ callback, alarm, period };
|
---|
223 | }
|
---|
224 |
|
---|
225 | void ^?{}( alarm_node_wrap(L) & this ) { }
|
---|
226 |
|
---|
227 | void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
|
---|
228 | if(&popped != 0p) {
|
---|
229 | popped.listed = false;
|
---|
230 | count--;
|
---|
231 | if (popped.lock) {
|
---|
232 | add_(*popped.lock, popped.t);
|
---|
233 | } else {
|
---|
234 | unpark(popped.t);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | bool notify_one( condition_variable(L) & this ) with( this ) {
|
---|
240 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
241 | bool ret = !empty(blocked_threads);
|
---|
242 | process_popped(this, dropHead( blocked_threads ));
|
---|
243 | unlock( lock );
|
---|
244 | return ret;
|
---|
245 | }
|
---|
246 |
|
---|
247 | bool notify_all( condition_variable(L) & this ) with(this) {
|
---|
248 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
249 | bool ret = !empty(blocked_threads);
|
---|
250 | while( !empty(blocked_threads) ) {
|
---|
251 | process_popped(this, dropHead( blocked_threads ));
|
---|
252 | }
|
---|
253 | unlock( lock );
|
---|
254 | return ret;
|
---|
255 | }
|
---|
256 |
|
---|
257 | uintptr_t front( condition_variable(L) & this ) with(this) {
|
---|
258 | return empty(blocked_threads) ? NULL : head(blocked_threads).info;
|
---|
259 | }
|
---|
260 |
|
---|
261 | bool empty( condition_variable(L) & this ) with(this) { return empty(blocked_threads); }
|
---|
262 |
|
---|
263 | int counter( condition_variable(L) & this ) with(this) { return count; }
|
---|
264 |
|
---|
265 | size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) {
|
---|
266 | addTail( blocked_threads, *i );
|
---|
267 | count++;
|
---|
268 | i->listed = true;
|
---|
269 | size_t recursion_count = 0;
|
---|
270 | if (i->lock) {
|
---|
271 | recursion_count = get_recursion_count(*i->lock);
|
---|
272 | remove_( *i->lock );
|
---|
273 | }
|
---|
274 | return recursion_count;
|
---|
275 | }
|
---|
276 |
|
---|
277 | // helper for wait()'s' with no timeout
|
---|
278 | void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
|
---|
279 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
280 | size_t recursion_count = queue_and_get_recursion(this, &i);
|
---|
281 | unlock( lock );
|
---|
282 | park( ); // blocks here
|
---|
283 | if (i.lock) set_recursion_count(*i.lock, recursion_count); // resets recursion count here after waking
|
---|
284 | }
|
---|
285 |
|
---|
286 | // helper for wait()'s' with a timeout
|
---|
287 | void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Time t ) with(this) {
|
---|
288 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
289 | size_t recursion_count = queue_and_get_recursion(this, &info);
|
---|
290 | alarm_node_wrap(L) node_wrap = { t, 0`s, alarm_node_wrap_cast };
|
---|
291 | node_wrap.cond = &this;
|
---|
292 | node_wrap.i = &info;
|
---|
293 | register_self( &node_wrap.alarm_node );
|
---|
294 | unlock( lock );
|
---|
295 | park();
|
---|
296 | unregister_self( &node_wrap.alarm_node );
|
---|
297 | if (info.lock) set_recursion_count(*info.lock, recursion_count);
|
---|
298 | }
|
---|
299 |
|
---|
300 | void wait( condition_variable(L) & this ) with(this) {
|
---|
301 | info_thread( L ) i = { active_thread() };
|
---|
302 | queue_info_thread( this, i );
|
---|
303 | }
|
---|
304 |
|
---|
305 | void wait( condition_variable(L) & this, uintptr_t info ) with(this) {
|
---|
306 | info_thread( L ) i = { active_thread(), info };
|
---|
307 | queue_info_thread( this, i );
|
---|
308 | }
|
---|
309 |
|
---|
310 | void wait( condition_variable(L) & this, Duration duration ) with(this) {
|
---|
311 | info_thread( L ) i = { active_thread() };
|
---|
312 | queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
|
---|
313 | }
|
---|
314 |
|
---|
315 | void wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) {
|
---|
316 | info_thread( L ) i = { active_thread(), info };
|
---|
317 | queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
|
---|
318 | }
|
---|
319 |
|
---|
320 | void wait( condition_variable(L) & this, Time time ) with(this) {
|
---|
321 | info_thread( L ) i = { active_thread() };
|
---|
322 | queue_info_thread_timeout(this, i, time);
|
---|
323 | }
|
---|
324 |
|
---|
325 | void wait( condition_variable(L) & this, uintptr_t info, Time time ) with(this) {
|
---|
326 | info_thread( L ) i = { active_thread(), info };
|
---|
327 | queue_info_thread_timeout(this, i, time);
|
---|
328 | }
|
---|
329 |
|
---|
330 | void wait( condition_variable(L) & this, L & l ) with(this) {
|
---|
331 | info_thread(L) i = { active_thread() };
|
---|
332 | i.lock = &l;
|
---|
333 | queue_info_thread( this, i );
|
---|
334 | }
|
---|
335 |
|
---|
336 | void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) {
|
---|
337 | info_thread(L) i = { active_thread(), info };
|
---|
338 | i.lock = &l;
|
---|
339 | queue_info_thread( this, i );
|
---|
340 | }
|
---|
341 |
|
---|
342 | void wait( condition_variable(L) & this, L & l, Duration duration ) with(this) {
|
---|
343 | info_thread(L) i = { active_thread() };
|
---|
344 | i.lock = &l;
|
---|
345 | queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
|
---|
346 | }
|
---|
347 |
|
---|
348 | void wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) {
|
---|
349 | info_thread(L) i = { active_thread(), info };
|
---|
350 | i.lock = &l;
|
---|
351 | queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
|
---|
352 | }
|
---|
353 |
|
---|
354 | void wait( condition_variable(L) & this, L & l, Time time ) with(this) {
|
---|
355 | info_thread(L) i = { active_thread() };
|
---|
356 | i.lock = &l;
|
---|
357 | queue_info_thread_timeout(this, i, time );
|
---|
358 | }
|
---|
359 |
|
---|
360 | void wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ) with(this) {
|
---|
361 | info_thread(L) i = { active_thread(), info };
|
---|
362 | i.lock = &l;
|
---|
363 | queue_info_thread_timeout(this, i, time );
|
---|
364 | }
|
---|
365 | }
|
---|