source: libcfa/src/concurrency/locks.cfa @ 797a193

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 797a193 was 797a193, checked in by caparsons <caparson@…>, 3 years ago

cleaned up locks code and added comments

  • Property mode set to 100644
File size: 12.5 KB
Line 
1#include "locks.hfa"
2#include "kernel_private.hfa"
3
4#include <kernel.hfa>
5#include <stdlib.hfa>
6
7///////////////////////////////////////////////////////////////////
8//// info_thread
9///////////////////////////////////////////////////////////////////
10
11forall(dtype L | is_blocking_lock(L)) {
12        void ?{}( info_thread(L) & this, $thread * t ) {
13                ((Seqable &) this){};
14                this.t = t;
15                this.lock = 0p;
16        }
17
18        void ?{}( info_thread(L) & this, $thread * t, uintptr_t info ) {
19                ((Seqable &) this){};
20                this.t = t;
21                this.info = info;
22                this.lock = 0p;
23        }
24
25        void ^?{}( info_thread(L) & this ){ }
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        }
34}
35
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
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 ) {}
57
58void lock( blocking_lock & this ) with( this ) {
59        lock( lock __cfaabi_dbg_ctx2 );
60        if ( owner == active_thread() && !multi_acquisition) { // single acquisition lock is held by current thread
61                abort("A single acquisition lock holder attempted to reacquire the lock resulting in a deadlock.");
62        } else if ( owner != 0p && owner != active_thread() ) { // lock is held by some other thread
63                addTail( blocked_threads, *active_thread() );
64                wait_count++;
65                unlock( lock );
66                park( );
67        } else if ( owner == active_thread() && multi_acquisition ) { // multi acquisition lock is held by current thread
68                recursion_count++;
69                unlock( lock );
70        } else { // lock isn't held
71                owner = active_thread();
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 );
80        if ( owner == 0p ) { // lock isn't held
81                owner = active_thread();
82                recursion_count = 1;
83                ret = true;
84        } else if ( owner == active_thread() && multi_acquisition ) { // multi acquisition lock is held by current thread
85                recursion_count++;
86                ret = true;
87        }
88        unlock( lock );
89        return ret;
90}
91
92void unlock_error_check( blocking_lock & this ) with( this ) {
93        if ( owner == 0p ){ // lock isn't held
94                abort( "There was an attempt to release a lock that isn't held" );
95        } else if ( strict_owner && owner != active_thread() ) {
96                abort( "A thread other than the owner attempted to release an owner lock" );
97        }
98}
99
100void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
101        $thread * t = &dropHead( blocked_threads );
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 );
111        recursion_count--;
112        if ( recursion_count == 0 ) { // if recursion count is zero release lock and set new owner if one is waiting
113                pop_and_set_new_owner( this );
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
130void on_notify( blocking_lock & this, $thread * t ) with( this ) {
131    lock( lock __cfaabi_dbg_ctx2 );
132        if ( owner != 0p ) { // lock held
133                addTail( blocked_threads, *t );
134                wait_count++;
135                unlock( lock );
136        } else {        // lock not held
137                owner = t;
138                recursion_count = 1;
139                unpark( t );
140                unlock( lock );
141        }
142}
143
144void on_wait( blocking_lock & this ) with( this ) {
145    lock( lock __cfaabi_dbg_ctx2 );
146        unlock_error_check( this );
147        pop_and_set_new_owner( this );
148        unlock( lock );
149}
150
151///////////////////////////////////////////////////////////////////
152//// Overloaded routines for traits
153///////////////////////////////////////////////////////////////////
154
155// These routines are temporary until an inheritance bug is fixed
156
157void lock( single_acquisition_lock & this ){ lock( (blocking_lock &)this ); }
158void unlock( single_acquisition_lock & this ){ unlock( (blocking_lock &)this ); }
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 ); }
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 ); }
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 ); }
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 ); }
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 ); }
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 ); }
177
178///////////////////////////////////////////////////////////////////
179//// condition variable
180///////////////////////////////////////////////////////////////////
181
182forall(dtype L | is_blocking_lock(L)) {
183
184        void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
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 );
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
192                i->signalled = false;
193                        remove( cond->blocked_threads, *i );    // remove this thread O(1)
194                        cond->count--;
195                        if( i->lock ) {
196                                on_notify(*i->lock, i->t);                      // call lock's on_notify if a lock was passed
197                } else {
198                        unpark( i->t );                                         // otherwise wake thread
199                }
200            }
201            unlock( cond->lock );
202        }
203
204        // this casts the alarm node to our wrapped type since we used type erasure
205        void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
206
207        void ?{}( condition_variable(L) & this ){
208                this.lock{};
209                this.blocked_threads{};
210                this.count = 0;
211        }
212
213        void ^?{}( condition_variable(L) & this ){ }
214
215        void ?{}( alarm_node_wrap(L) & this, Time alarm, Duration period, Alarm_Callback callback ) {
216                this.alarm_node{ callback, alarm, period };
217        }
218
219        void ^?{}( alarm_node_wrap(L) & this ) { }
220
221        void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
222                if(&popped != 0p) {
223                        popped.signalled = true;
224                        count--;
225                        if (popped.lock) {
226                                on_notify(*popped.lock, popped.t); // if lock passed call on_notify
227                        } else {
228                                unpark(popped.t); // otherwise wake thread
229                        }
230                }
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 ));
237                unlock( lock );
238                return ret;
239        }
240
241        bool notify_all( condition_variable(L) & this ) with(this) {
242                lock( lock __cfaabi_dbg_ctx2 );
243                bool ret = !empty(blocked_threads);
244                while( !empty(blocked_threads) ) {
245                        process_popped(this, dropHead( blocked_threads ));
246                }
247                unlock( lock );
248                return ret;
249        }
250
251        uintptr_t front( condition_variable(L) & this ) with(this) {
252                return empty(blocked_threads) ? NULL : head(blocked_threads).info;
253        }
254
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; }
258
259        size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) {
260                addTail( blocked_threads, *i ); // add info_thread to waiting queue
261                count++;
262                size_t recursion_count = 0;
263                if (i->lock) { // if lock was passed get recursion count to reset to after waking thread
264                        recursion_count = get_recursion_count(*i->lock);
265                        on_wait( *i->lock );
266                }
267                return recursion_count;
268        }
269
270        // helper for wait()'s' with no timeout
271        void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
272                lock( lock __cfaabi_dbg_ctx2 );
273                size_t recursion_count = queue_and_get_recursion(this, &i);
274                unlock( lock );
275                park( ); // blocks here
276                if (i.lock) set_recursion_count(*i.lock, recursion_count); // resets recursion count here after waking
277        }
278
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 );
282                size_t recursion_count = queue_and_get_recursion(this, &info);
283                alarm_node_wrap(L) node_wrap = { t, 0`s, alarm_node_wrap_cast };
284                node_wrap.cond = &this;
285                node_wrap.i = &info;
286                register_self( &node_wrap.alarm_node );
287                unlock( lock );
288                park();
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
291        }
292
293        void wait( condition_variable(L) & this ) with(this) {
294                info_thread( L ) i = { active_thread() };
295                queue_info_thread( this, i );
296        }
297
298        void wait( condition_variable(L) & this, uintptr_t info ) with(this) {
299                info_thread( L ) i = { active_thread(), info };
300                queue_info_thread( this, i );
301        }
302       
303        bool wait( condition_variable(L) & this, Duration duration ) with(this) {
304                info_thread( L ) i = { active_thread() };
305                queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
306                return i.signalled;
307        }
308
309        bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) {
310                info_thread( L ) i = { active_thread(), info };
311                queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
312                return i.signalled;
313        }
314
315        bool wait( condition_variable(L) & this, Time time ) with(this) {
316                info_thread( L ) i = { active_thread() };
317                queue_info_thread_timeout(this, i, time);
318                return i.signalled;
319        }
320
321        bool wait( condition_variable(L) & this, uintptr_t info, Time time ) with(this) {
322                info_thread( L ) i = { active_thread(), info };
323                queue_info_thread_timeout(this, i, time);
324                return i.signalled;
325        }
326
327        void wait( condition_variable(L) & this, L & l ) with(this) {
328                info_thread(L) i = { active_thread() };
329                i.lock = &l;
330                queue_info_thread( this, i );
331        }
332
333        void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) {
334                info_thread(L) i = { active_thread(), info };
335                i.lock = &l;
336                queue_info_thread( this, i );
337        }
338       
339        bool wait( condition_variable(L) & this, L & l, Duration duration ) with(this) {
340                info_thread(L) i = { active_thread() };
341                i.lock = &l;
342                queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
343                return i.signalled;
344        }
345       
346        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) {
347                info_thread(L) i = { active_thread(), info };
348                i.lock = &l;
349                queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
350                return i.signalled;
351        }
352       
353        bool wait( condition_variable(L) & this, L & l, Time time ) with(this) {
354                info_thread(L) i = { active_thread() };
355                i.lock = &l;
356                queue_info_thread_timeout(this, i, time );
357                return i.signalled;
358        }
359       
360        bool 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                return i.signalled;
365        }
366}
Note: See TracBrowser for help on using the repository browser.