1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2021 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 | // locks.hfa -- LIBCFATHREAD
|
---|
8 | // Runtime locks that used with the runtime thread system.
|
---|
9 | //
|
---|
10 | // Author : Colby Alexander Parsons
|
---|
11 | // Created On : Thu Jan 21 19:46:50 2021
|
---|
12 | // Last Modified By :
|
---|
13 | // Last Modified On :
|
---|
14 | // Update Count :
|
---|
15 | //
|
---|
16 |
|
---|
17 | #define __cforall_thread__
|
---|
18 |
|
---|
19 | #include "locks.hfa"
|
---|
20 | #include "kernel_private.hfa"
|
---|
21 |
|
---|
22 | #include <kernel.hfa>
|
---|
23 | #include <stdlib.hfa>
|
---|
24 |
|
---|
25 | //-----------------------------------------------------------------------------
|
---|
26 | // info_thread
|
---|
27 | forall(L & | is_blocking_lock(L)) {
|
---|
28 | struct info_thread {
|
---|
29 | // used to put info_thread on a dl queue
|
---|
30 | inline dlink(info_thread(L));
|
---|
31 |
|
---|
32 | // waiting thread
|
---|
33 | struct $thread * t;
|
---|
34 |
|
---|
35 | // shadow field
|
---|
36 | uintptr_t info;
|
---|
37 |
|
---|
38 | // lock that is passed to wait() (if one is passed)
|
---|
39 | L * lock;
|
---|
40 |
|
---|
41 | // true when signalled and false when timeout wakes thread
|
---|
42 | bool signalled;
|
---|
43 | };
|
---|
44 | P9_EMBEDDED( info_thread(L), dlink(info_thread(L)) )
|
---|
45 |
|
---|
46 | void ?{}( info_thread(L) & this, $thread * t, uintptr_t info, L * l ) {
|
---|
47 | this.t = t;
|
---|
48 | this.info = info;
|
---|
49 | this.lock = l;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void ^?{}( info_thread(L) & this ) {}
|
---|
53 | }
|
---|
54 |
|
---|
55 | //-----------------------------------------------------------------------------
|
---|
56 | // Blocking Locks
|
---|
57 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ) {
|
---|
58 | this.lock{};
|
---|
59 | this.blocked_threads{};
|
---|
60 | this.wait_count = 0;
|
---|
61 | this.multi_acquisition = multi_acquisition;
|
---|
62 | this.strict_owner = strict_owner;
|
---|
63 | this.owner = 0p;
|
---|
64 | this.recursion_count = 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void ^?{}( blocking_lock & this ) {}
|
---|
68 |
|
---|
69 |
|
---|
70 | void lock( blocking_lock & this ) with( this ) {
|
---|
71 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
72 | $thread * thrd = active_thread();
|
---|
73 |
|
---|
74 | // single acquisition lock is held by current thread
|
---|
75 | /* paranoid */ verifyf( owner != thrd || multi_acquisition, "Single acquisition lock holder (%p) attempted to reacquire the lock %p resulting in a deadlock.", owner, &this );
|
---|
76 |
|
---|
77 | // lock is held by some other thread
|
---|
78 | if ( owner != 0p && owner != thrd ) {
|
---|
79 | insert_last( blocked_threads, *thrd );
|
---|
80 | wait_count++;
|
---|
81 | unlock( lock );
|
---|
82 | park( );
|
---|
83 | }
|
---|
84 | // multi acquisition lock is held by current thread
|
---|
85 | else if ( owner == thrd && multi_acquisition ) {
|
---|
86 | recursion_count++;
|
---|
87 | unlock( lock );
|
---|
88 | }
|
---|
89 | // lock isn't held
|
---|
90 | else {
|
---|
91 | owner = thrd;
|
---|
92 | recursion_count = 1;
|
---|
93 | unlock( lock );
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | bool try_lock( blocking_lock & this ) with( this ) {
|
---|
98 | bool ret = false;
|
---|
99 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
100 |
|
---|
101 | // lock isn't held
|
---|
102 | if ( owner == 0p ) {
|
---|
103 | owner = active_thread();
|
---|
104 | recursion_count = 1;
|
---|
105 | ret = true;
|
---|
106 | }
|
---|
107 | // multi acquisition lock is held by current thread
|
---|
108 | else if ( owner == active_thread() && multi_acquisition ) {
|
---|
109 | recursion_count++;
|
---|
110 | ret = true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | unlock( lock );
|
---|
114 | return ret;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
|
---|
118 | $thread * t = &try_pop_front( blocked_threads );
|
---|
119 | owner = t;
|
---|
120 | recursion_count = ( t ? 1 : 0 );
|
---|
121 | wait_count--;
|
---|
122 | unpark( t );
|
---|
123 | }
|
---|
124 |
|
---|
125 | void unlock( blocking_lock & this ) with( this ) {
|
---|
126 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
127 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
|
---|
128 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner , "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
|
---|
129 | /* paranoid */ verifyf( recursion_count == 1 || multi_acquisition, "Thread %p attempted to release owner lock %p which is not recursive but has a recursive count of %zu", active_thread(), &this, recursion_count );
|
---|
130 |
|
---|
131 | // if recursion count is zero release lock and set new owner if one is waiting
|
---|
132 | recursion_count--;
|
---|
133 | if ( recursion_count == 0 ) {
|
---|
134 | pop_and_set_new_owner( this );
|
---|
135 | }
|
---|
136 | unlock( lock );
|
---|
137 | }
|
---|
138 |
|
---|
139 | size_t wait_count( blocking_lock & this ) with( this ) {
|
---|
140 | return wait_count;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void on_notify( blocking_lock & this, $thread * t ) with( this ) {
|
---|
144 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
145 | // lock held
|
---|
146 | if ( owner != 0p ) {
|
---|
147 | insert_last( blocked_threads, *t );
|
---|
148 | wait_count++;
|
---|
149 | unlock( lock );
|
---|
150 | }
|
---|
151 | // lock not held
|
---|
152 | else {
|
---|
153 | owner = t;
|
---|
154 | recursion_count = 1;
|
---|
155 | unpark( t );
|
---|
156 | unlock( lock );
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | size_t on_wait( blocking_lock & this ) with( this ) {
|
---|
161 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
162 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
|
---|
163 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner, "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
|
---|
164 |
|
---|
165 | size_t ret = recursion_count;
|
---|
166 |
|
---|
167 | pop_and_set_new_owner( this );
|
---|
168 | unlock( lock );
|
---|
169 | return ret;
|
---|
170 | }
|
---|
171 |
|
---|
172 | void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) {
|
---|
173 | recursion_count = recursion;
|
---|
174 | }
|
---|
175 |
|
---|
176 | //-----------------------------------------------------------------------------
|
---|
177 | // alarm node wrapper
|
---|
178 | forall(L & | is_blocking_lock(L)) {
|
---|
179 | struct alarm_node_wrap {
|
---|
180 | alarm_node_t alarm_node;
|
---|
181 | condition_variable(L) * cond;
|
---|
182 | info_thread(L) * info_thd;
|
---|
183 | };
|
---|
184 |
|
---|
185 | void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) {
|
---|
186 | this.alarm_node{ callback, alarm, period };
|
---|
187 | this.cond = c;
|
---|
188 | this.info_thd = i;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void ^?{}( alarm_node_wrap(L) & this ) { }
|
---|
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 | // this check is necessary to avoid a race condition since this timeout handler
|
---|
198 | // may still be called after a thread has been removed from the queue but
|
---|
199 | // before the alarm is unregistered
|
---|
200 | if ( (*info_thd)`isListed ) { // is thread on queue
|
---|
201 | info_thd->signalled = false;
|
---|
202 | // remove this thread O(1)
|
---|
203 | remove( *info_thd );
|
---|
204 | cond->count--;
|
---|
205 | if( info_thd->lock ) {
|
---|
206 | // call lock's on_notify if a lock was passed
|
---|
207 | on_notify(*info_thd->lock, info_thd->t);
|
---|
208 | } else {
|
---|
209 | // otherwise wake thread
|
---|
210 | unpark( info_thd->t );
|
---|
211 | }
|
---|
212 | }
|
---|
213 | unlock( cond->lock );
|
---|
214 | }
|
---|
215 |
|
---|
216 | // this casts the alarm node to our wrapped type since we used type erasure
|
---|
217 | void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
|
---|
218 | }
|
---|
219 |
|
---|
220 | //-----------------------------------------------------------------------------
|
---|
221 | // condition variable
|
---|
222 | forall(L & | is_blocking_lock(L)) {
|
---|
223 |
|
---|
224 | void ?{}( condition_variable(L) & this ){
|
---|
225 | this.lock{};
|
---|
226 | this.blocked_threads{};
|
---|
227 | this.count = 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | void ^?{}( condition_variable(L) & this ){ }
|
---|
231 |
|
---|
232 | void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
|
---|
233 | if(&popped != 0p) {
|
---|
234 | popped.signalled = true;
|
---|
235 | count--;
|
---|
236 | if (popped.lock) {
|
---|
237 | // if lock passed call on_notify
|
---|
238 | on_notify(*popped.lock, popped.t);
|
---|
239 | } else {
|
---|
240 | // otherwise wake thread
|
---|
241 | unpark(popped.t);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | bool notify_one( condition_variable(L) & this ) with( this ) {
|
---|
247 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
248 | bool ret = ! blocked_threads`isEmpty;
|
---|
249 | process_popped(this, try_pop_front( blocked_threads ));
|
---|
250 | unlock( lock );
|
---|
251 | return ret;
|
---|
252 | }
|
---|
253 |
|
---|
254 | bool notify_all( condition_variable(L) & this ) with(this) {
|
---|
255 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
256 | bool ret = ! blocked_threads`isEmpty;
|
---|
257 | while( ! blocked_threads`isEmpty ) {
|
---|
258 | process_popped(this, try_pop_front( blocked_threads ));
|
---|
259 | }
|
---|
260 | unlock( lock );
|
---|
261 | return ret;
|
---|
262 | }
|
---|
263 |
|
---|
264 | uintptr_t front( condition_variable(L) & this ) with(this) {
|
---|
265 | return blocked_threads`isEmpty ? NULL : blocked_threads`first.info;
|
---|
266 | }
|
---|
267 |
|
---|
268 | bool empty( condition_variable(L) & this ) with(this) {
|
---|
269 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
270 | bool ret = blocked_threads`isEmpty;
|
---|
271 | unlock( lock );
|
---|
272 | return ret;
|
---|
273 | }
|
---|
274 |
|
---|
275 | int counter( condition_variable(L) & this ) with(this) { return count; }
|
---|
276 |
|
---|
277 | size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) {
|
---|
278 | // add info_thread to waiting queue
|
---|
279 | insert_last( blocked_threads, *i );
|
---|
280 | count++;
|
---|
281 | size_t recursion_count = 0;
|
---|
282 | if (i->lock) {
|
---|
283 | // if lock was passed get recursion count to reset to after waking thread
|
---|
284 | recursion_count = on_wait( *i->lock );
|
---|
285 | }
|
---|
286 | return recursion_count;
|
---|
287 | }
|
---|
288 |
|
---|
289 | // helper for wait()'s' with no timeout
|
---|
290 | void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
|
---|
291 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
292 | size_t recursion_count = queue_and_get_recursion(this, &i);
|
---|
293 | unlock( lock );
|
---|
294 |
|
---|
295 | // blocks here
|
---|
296 | park( );
|
---|
297 |
|
---|
298 | // resets recursion count here after waking
|
---|
299 | if (i.lock) on_wakeup(*i.lock, recursion_count);
|
---|
300 | }
|
---|
301 |
|
---|
302 | #define WAIT( u, l ) \
|
---|
303 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
304 | queue_info_thread( this, i );
|
---|
305 |
|
---|
306 | // helper for wait()'s' with a timeout
|
---|
307 | void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
|
---|
308 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
309 | size_t recursion_count = queue_and_get_recursion(this, &info);
|
---|
310 | alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
|
---|
311 | register_self( &node_wrap.alarm_node );
|
---|
312 | unlock( lock );
|
---|
313 |
|
---|
314 | // blocks here
|
---|
315 | park();
|
---|
316 |
|
---|
317 | // unregisters alarm so it doesn't go off if this happens first
|
---|
318 | unregister_self( &node_wrap.alarm_node );
|
---|
319 |
|
---|
320 | // resets recursion count here after waking
|
---|
321 | if (info.lock) on_wakeup(*info.lock, recursion_count);
|
---|
322 | }
|
---|
323 |
|
---|
324 | #define WAIT_TIME( u, l, t ) \
|
---|
325 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
326 | queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \
|
---|
327 | return i.signalled;
|
---|
328 |
|
---|
329 | void wait( condition_variable(L) & this ) with(this) { WAIT( 0, 0p ) }
|
---|
330 | void wait( condition_variable(L) & this, uintptr_t info ) with(this) { WAIT( info, 0p ) }
|
---|
331 | void wait( condition_variable(L) & this, L & l ) with(this) { WAIT( 0, &l ) }
|
---|
332 | void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { WAIT( info, &l ) }
|
---|
333 |
|
---|
334 | bool wait( condition_variable(L) & this, Duration duration ) with(this) { WAIT_TIME( 0 , 0p , duration ) }
|
---|
335 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, 0p , duration ) }
|
---|
336 | bool wait( condition_variable(L) & this, L & l, Duration duration ) with(this) { WAIT_TIME( 0 , &l , duration ) }
|
---|
337 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) }
|
---|
338 | }
|
---|
339 |
|
---|
340 | //-----------------------------------------------------------------------------
|
---|
341 | // Semaphore
|
---|
342 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
343 | (this.lock){};
|
---|
344 | this.count = count;
|
---|
345 | (this.waiting){};
|
---|
346 | }
|
---|
347 | void ^?{}(semaphore & this) {}
|
---|
348 |
|
---|
349 | bool P(semaphore & this) with( this ){
|
---|
350 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
351 | count -= 1;
|
---|
352 | if ( count < 0 ) {
|
---|
353 | // queue current task
|
---|
354 | append( waiting, active_thread() );
|
---|
355 |
|
---|
356 | // atomically release spin lock and block
|
---|
357 | unlock( lock );
|
---|
358 | park();
|
---|
359 | return true;
|
---|
360 | }
|
---|
361 | else {
|
---|
362 | unlock( lock );
|
---|
363 | return false;
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | $thread * V (semaphore & this, const bool doUnpark ) with( this ) {
|
---|
368 | $thread * thrd = 0p;
|
---|
369 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
370 | count += 1;
|
---|
371 | if ( count <= 0 ) {
|
---|
372 | // remove task at head of waiting list
|
---|
373 | thrd = pop_head( waiting );
|
---|
374 | }
|
---|
375 |
|
---|
376 | unlock( lock );
|
---|
377 |
|
---|
378 | // make new owner
|
---|
379 | if( doUnpark ) unpark( thrd );
|
---|
380 |
|
---|
381 | return thrd;
|
---|
382 | }
|
---|
383 |
|
---|
384 | bool V(semaphore & this) with( this ) {
|
---|
385 | $thread * thrd = V(this, true);
|
---|
386 | return thrd != 0p;
|
---|
387 | }
|
---|
388 |
|
---|
389 | bool V(semaphore & this, unsigned diff) with( this ) {
|
---|
390 | $thread * thrd = 0p;
|
---|
391 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
392 | int release = max(-count, (int)diff);
|
---|
393 | count += diff;
|
---|
394 | for(release) {
|
---|
395 | unpark( pop_head( waiting ) );
|
---|
396 | }
|
---|
397 |
|
---|
398 | unlock( lock );
|
---|
399 |
|
---|
400 | return thrd != 0p;
|
---|
401 | }
|
---|