source: libcfa/src/concurrency/locks.cfa@ 15c2545

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 15c2545 was 797a193, checked in by caparsons <caparson@…>, 5 years ago

cleaned up locks code and added comments

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