source: libcfa/src/concurrency/monitor.cfa@ 70ead46a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 70ead46a was ac2b598, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Changed descriptors for concurrency to use $ prefix instead of trailing _desc

  • Property mode set to 100644
File size: 31.8 KB
Line 
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// $monitor.c --
8//
9// Author : Thierry Delisle
10// Created On : Thd Feb 23 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Dec 4 07:55:14 2019
13// Update Count : 10
14//
15
16#define __cforall_thread__
17
18#include "monitor.hfa"
19
20#include <stdlib.hfa>
21#include <inttypes.h>
22
23#include "kernel_private.hfa"
24
25#include "bits/algorithm.hfa"
26
27//-----------------------------------------------------------------------------
28// Forward declarations
29static inline void __set_owner ( $monitor * this, $thread * owner );
30static inline void __set_owner ( $monitor * storage [], __lock_size_t count, $thread * owner );
31static inline void set_mask ( $monitor * storage [], __lock_size_t count, const __waitfor_mask_t & mask );
32static inline void reset_mask( $monitor * this );
33
34static inline $thread * next_thread( $monitor * this );
35static inline bool is_accepted( $monitor * this, const __monitor_group_t & monitors );
36
37static inline void lock_all ( __spinlock_t * locks [], __lock_size_t count );
38static inline void lock_all ( $monitor * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count );
39static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count );
40static inline void unlock_all( $monitor * locks [], __lock_size_t count );
41
42static inline void save ( $monitor * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
43static inline void restore( $monitor * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
44
45static inline void init ( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
46static inline void init_push( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
47
48static inline $thread * check_condition ( __condition_criterion_t * );
49static inline void brand_condition ( condition & );
50static inline [$thread *, int] search_entry_queue( const __waitfor_mask_t &, $monitor * monitors [], __lock_size_t count );
51
52forall(dtype T | sized( T ))
53static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val );
54static inline __lock_size_t count_max ( const __waitfor_mask_t & mask );
55static inline __lock_size_t aggregate ( $monitor * storage [], const __waitfor_mask_t & mask );
56
57//-----------------------------------------------------------------------------
58// Useful defines
59#define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
60 __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
61 __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
62 init( count, monitors, waiter, criteria ); /* Link everything together */ \
63
64#define wait_ctx_primed(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
65 __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
66 __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
67 init_push( count, monitors, waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \
68
69#define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \
70 $monitor ** monitors = mons; /* Save the targeted monitors */ \
71 __lock_size_t count = cnt; /* Save the count to a local variable */ \
72 unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \
73 __waitfor_mask_t masks [ count ]; /* Save the current waitfor masks to restore them later */ \
74 __spinlock_t * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \
75
76#define monitor_save save ( monitors, count, locks, recursions, masks )
77#define monitor_restore restore( monitors, count, locks, recursions, masks )
78
79
80//-----------------------------------------------------------------------------
81// Enter/Leave routines
82// Enter single monitor
83static void __enter( $monitor * this, const __monitor_group_t & group ) {
84 // Lock the monitor spinlock
85 lock( this->lock __cfaabi_dbg_ctx2 );
86 // Interrupts disable inside critical section
87 $thread * thrd = kernelTLS.this_thread;
88
89 __cfaabi_dbg_print_safe( "Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
90
91 if( !this->owner ) {
92 // No one has the monitor, just take it
93 __set_owner( this, thrd );
94
95 __cfaabi_dbg_print_safe( "Kernel : mon is free \n" );
96 }
97 else if( this->owner == thrd) {
98 // We already have the monitor, just note how many times we took it
99 this->recursion += 1;
100
101 __cfaabi_dbg_print_safe( "Kernel : mon already owned \n" );
102 }
103 else if( is_accepted( this, group) ) {
104 // Some one was waiting for us, enter
105 __set_owner( this, thrd );
106
107 // Reset mask
108 reset_mask( this );
109
110 __cfaabi_dbg_print_safe( "Kernel : mon accepts \n" );
111 }
112 else {
113 __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
114
115 // Some one else has the monitor, wait in line for it
116 /* paranoid */ verify( thrd->next == 0p );
117 append( this->entry_queue, thrd );
118 /* paranoid */ verify( thrd->next == 1p );
119
120 unlock( this->lock );
121 park();
122
123 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
124
125 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
126 return;
127 }
128
129 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
130
131 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
132 /* paranoid */ verify( this->lock.lock );
133
134 // Release the lock and leave
135 unlock( this->lock );
136 return;
137}
138
139static void __dtor_enter( $monitor * this, fptr_t func ) {
140 // Lock the monitor spinlock
141 lock( this->lock __cfaabi_dbg_ctx2 );
142 // Interrupts disable inside critical section
143 $thread * thrd = kernelTLS.this_thread;
144
145 __cfaabi_dbg_print_safe( "Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);
146
147
148 if( !this->owner ) {
149 __cfaabi_dbg_print_safe( "Kernel : Destroying free mon %p\n", this);
150
151 // No one has the monitor, just take it
152 __set_owner( this, thrd );
153
154 verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
155
156 unlock( this->lock );
157 return;
158 }
159 else if( this->owner == thrd) {
160 // We already have the monitor... but where about to destroy it so the nesting will fail
161 // Abort!
162 abort( "Attempt to destroy monitor %p by thread \"%.256s\" (%p) in nested mutex.", this, thrd->self_cor.name, thrd );
163 }
164
165 __lock_size_t count = 1;
166 $monitor ** monitors = &this;
167 __monitor_group_t group = { &this, 1, func };
168 if( is_accepted( this, group) ) {
169 __cfaabi_dbg_print_safe( "Kernel : mon accepts dtor, block and signal it \n" );
170
171 // Wake the thread that is waiting for this
172 __condition_criterion_t * urgent = pop( this->signal_stack );
173 /* paranoid */ verify( urgent );
174
175 // Reset mask
176 reset_mask( this );
177
178 // Create the node specific to this wait operation
179 wait_ctx_primed( thrd, 0 )
180
181 // Some one else has the monitor, wait for him to finish and then run
182 unlock( this->lock );
183
184 // Release the next thread
185 /* paranoid */ verifyf( urgent->owner->waiting_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
186 unpark( urgent->owner->waiting_thread );
187
188 // Park current thread waiting
189 park();
190
191 // Some one was waiting for us, enter
192 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
193 }
194 else {
195 __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
196
197 wait_ctx( thrd, 0 )
198 this->dtor_node = &waiter;
199
200 // Some one else has the monitor, wait in line for it
201 /* paranoid */ verify( thrd->next == 0p );
202 append( this->entry_queue, thrd );
203 /* paranoid */ verify( thrd->next == 1p );
204 unlock( this->lock );
205
206 // Park current thread waiting
207 park();
208
209 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
210 return;
211 }
212
213 __cfaabi_dbg_print_safe( "Kernel : Destroying %p\n", this);
214
215}
216
217// Leave single monitor
218void __leave( $monitor * this ) {
219 // Lock the monitor spinlock
220 lock( this->lock __cfaabi_dbg_ctx2 );
221
222 __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner);
223
224 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
225
226 // Leaving a recursion level, decrement the counter
227 this->recursion -= 1;
228
229 // If we haven't left the last level of recursion
230 // it means we don't need to do anything
231 if( this->recursion != 0) {
232 __cfaabi_dbg_print_safe( "Kernel : recursion still %d\n", this->recursion);
233 unlock( this->lock );
234 return;
235 }
236
237 // Get the next thread, will be null on low contention monitor
238 $thread * new_owner = next_thread( this );
239
240 // Check the new owner is consistent with who we wake-up
241 // new_owner might be null even if someone owns the monitor when the owner is still waiting for another monitor
242 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
243
244 // We can now let other threads in safely
245 unlock( this->lock );
246
247 //We need to wake-up the thread
248 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
249 unpark( new_owner );
250}
251
252// Leave single monitor for the last time
253void __dtor_leave( $monitor * this ) {
254 __cfaabi_dbg_debug_do(
255 if( TL_GET( this_thread ) != this->owner ) {
256 abort( "Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, TL_GET( this_thread ), this->owner);
257 }
258 if( this->recursion != 1 ) {
259 abort( "Destroyed monitor %p has %d outstanding nested calls.\n", this, this->recursion - 1);
260 }
261 )
262}
263
264extern "C" {
265 // Leave the thread monitor
266 // last routine called by a thread.
267 // Should never return
268 void __cfactx_thrd_leave() {
269 $thread * thrd = TL_GET( this_thread );
270 $monitor * this = &thrd->self_mon;
271
272 // Lock the monitor now
273 lock( this->lock __cfaabi_dbg_ctx2 );
274
275 disable_interrupts();
276
277 thrd->state = Halted;
278
279 /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
280
281 // Leaving a recursion level, decrement the counter
282 this->recursion -= 1;
283
284 // If we haven't left the last level of recursion
285 // it must mean there is an error
286 if( this->recursion != 0) { abort( "Thread internal monitor has unbalanced recursion" ); }
287
288 // Fetch the next thread, can be null
289 $thread * new_owner = next_thread( this );
290
291 // Release the monitor lock
292 unlock( this->lock );
293
294 // Unpark the next owner if needed
295 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
296 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
297 /* paranoid */ verify( ! kernelTLS.this_processor->destroyer );
298 /* paranoid */ verify( thrd->state == Halted );
299
300 kernelTLS.this_processor->destroyer = new_owner;
301
302 // Leave the thread
303 __leave_thread();
304
305 // Control flow should never reach here!
306 }
307}
308
309// Enter multiple monitor
310// relies on the monitor array being sorted
311static inline void enter( __monitor_group_t monitors ) {
312 for( __lock_size_t i = 0; i < monitors.size; i++) {
313 __enter( monitors[i], monitors );
314 }
315}
316
317// Leave multiple monitor
318// relies on the monitor array being sorted
319static inline void leave($monitor * monitors [], __lock_size_t count) {
320 for( __lock_size_t i = count - 1; i >= 0; i--) {
321 __leave( monitors[i] );
322 }
323}
324
325// Ctor for monitor guard
326// Sorts monitors before entering
327void ?{}( monitor_guard_t & this, $monitor * m [], __lock_size_t count, fptr_t func ) {
328 $thread * thrd = TL_GET( this_thread );
329
330 // Store current array
331 this.m = m;
332 this.count = count;
333
334 // Sort monitors based on address
335 __libcfa_small_sort(this.m, count);
336
337 // Save previous thread context
338 this.prev = thrd->monitors;
339
340 // Update thread context (needed for conditions)
341 (thrd->monitors){m, count, func};
342
343 // __cfaabi_dbg_print_safe( "MGUARD : enter %d\n", count);
344
345 // Enter the monitors in order
346 __monitor_group_t group = {this.m, this.count, func};
347 enter( group );
348
349 // __cfaabi_dbg_print_safe( "MGUARD : entered\n" );
350}
351
352
353// Dtor for monitor guard
354void ^?{}( monitor_guard_t & this ) {
355 // __cfaabi_dbg_print_safe( "MGUARD : leaving %d\n", this.count);
356
357 // Leave the monitors in order
358 leave( this.m, this.count );
359
360 // __cfaabi_dbg_print_safe( "MGUARD : left\n" );
361
362 // Restore thread context
363 TL_GET( this_thread )->monitors = this.prev;
364}
365
366// Ctor for monitor guard
367// Sorts monitors before entering
368void ?{}( monitor_dtor_guard_t & this, $monitor * m [], fptr_t func ) {
369 // optimization
370 $thread * thrd = TL_GET( this_thread );
371
372 // Store current array
373 this.m = *m;
374
375 // Save previous thread context
376 this.prev = thrd->monitors;
377
378 // Update thread context (needed for conditions)
379 (thrd->monitors){m, 1, func};
380
381 __dtor_enter( this.m, func );
382}
383
384// Dtor for monitor guard
385void ^?{}( monitor_dtor_guard_t & this ) {
386 // Leave the monitors in order
387 __dtor_leave( this.m );
388
389 // Restore thread context
390 TL_GET( this_thread )->monitors = this.prev;
391}
392
393//-----------------------------------------------------------------------------
394// Internal scheduling types
395void ?{}(__condition_node_t & this, $thread * waiting_thread, __lock_size_t count, uintptr_t user_info ) {
396 this.waiting_thread = waiting_thread;
397 this.count = count;
398 this.next = 0p;
399 this.user_info = user_info;
400}
401
402void ?{}(__condition_criterion_t & this ) with( this ) {
403 ready = false;
404 target = 0p;
405 owner = 0p;
406 next = 0p;
407}
408
409void ?{}(__condition_criterion_t & this, $monitor * target, __condition_node_t & owner ) {
410 this.ready = false;
411 this.target = target;
412 this.owner = &owner;
413 this.next = 0p;
414}
415
416//-----------------------------------------------------------------------------
417// Internal scheduling
418void wait( condition & this, uintptr_t user_info = 0 ) {
419 brand_condition( this );
420
421 // Check that everything is as expected
422 assertf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors );
423 verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
424 verifyf( this.monitor_count < 32u, "Excessive monitor count (%"PRIiFAST16")", this.monitor_count );
425
426 // Create storage for monitor context
427 monitor_ctx( this.monitors, this.monitor_count );
428
429 // Create the node specific to this wait operation
430 wait_ctx( TL_GET( this_thread ), user_info );
431
432 // Append the current wait operation to the ones already queued on the condition
433 // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
434 /* paranoid */ verify( waiter.next == 0p );
435 append( this.blocked, &waiter );
436 /* paranoid */ verify( waiter.next == 1p );
437
438 // Lock all monitors (aggregates the locks as well)
439 lock_all( monitors, locks, count );
440
441 // Find the next thread(s) to run
442 __lock_size_t thread_count = 0;
443 $thread * threads[ count ];
444 __builtin_memset( threads, 0, sizeof( threads ) );
445
446 // Save monitor states
447 monitor_save;
448
449 // Remove any duplicate threads
450 for( __lock_size_t i = 0; i < count; i++) {
451 $thread * new_owner = next_thread( monitors[i] );
452 insert_unique( threads, thread_count, new_owner );
453 }
454
455 // Unlock the locks, we don't need them anymore
456 for(int i = 0; i < count; i++) {
457 unlock( *locks[i] );
458 }
459
460 // Wake the threads
461 for(int i = 0; i < thread_count; i++) {
462 unpark( threads[i] );
463 }
464
465 // Everything is ready to go to sleep
466 park();
467
468 // We are back, restore the owners and recursions
469 monitor_restore;
470}
471
472bool signal( condition & this ) {
473 if( is_empty( this ) ) { return false; }
474
475 //Check that everything is as expected
476 verify( this.monitors );
477 verify( this.monitor_count != 0 );
478
479 //Some more checking in debug
480 __cfaabi_dbg_debug_do(
481 $thread * this_thrd = TL_GET( this_thread );
482 if ( this.monitor_count != this_thrd->monitors.size ) {
483 abort( "Signal on condition %p made with different number of monitor(s), expected %zi got %zi", &this, this.monitor_count, this_thrd->monitors.size );
484 }
485
486 for(int i = 0; i < this.monitor_count; i++) {
487 if ( this.monitors[i] != this_thrd->monitors[i] ) {
488 abort( "Signal on condition %p made with different monitor, expected %p got %p", &this, this.monitors[i], this_thrd->monitors[i] );
489 }
490 }
491 );
492
493 __lock_size_t count = this.monitor_count;
494
495 // Lock all monitors
496 lock_all( this.monitors, 0p, count );
497
498 //Pop the head of the waiting queue
499 __condition_node_t * node = pop_head( this.blocked );
500
501 //Add the thread to the proper AS stack
502 for(int i = 0; i < count; i++) {
503 __condition_criterion_t * crit = &node->criteria[i];
504 assert( !crit->ready );
505 push( crit->target->signal_stack, crit );
506 }
507
508 //Release
509 unlock_all( this.monitors, count );
510
511 return true;
512}
513
514bool signal_block( condition & this ) {
515 if( !this.blocked.head ) { return false; }
516
517 //Check that everything is as expected
518 verifyf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors );
519 verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
520
521 // Create storage for monitor context
522 monitor_ctx( this.monitors, this.monitor_count );
523
524 // Lock all monitors (aggregates the locks them as well)
525 lock_all( monitors, locks, count );
526
527
528 // Create the node specific to this wait operation
529 wait_ctx_primed( kernelTLS.this_thread, 0 )
530
531 //save contexts
532 monitor_save;
533
534 //Find the thread to run
535 $thread * signallee = pop_head( this.blocked )->waiting_thread;
536 /* paranoid */ verify( signallee->next == 0p );
537 __set_owner( monitors, count, signallee );
538
539 __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
540
541 // unlock all the monitors
542 unlock_all( locks, count );
543
544 // unpark the thread we signalled
545 unpark( signallee );
546
547 //Everything is ready to go to sleep
548 park();
549
550
551 // WE WOKE UP
552
553
554 __cfaabi_dbg_print_buffer_local( "Kernel : signal_block returned\n" );
555
556 //We are back, restore the masks and recursions
557 monitor_restore;
558
559 return true;
560}
561
562// Access the user_info of the thread waiting at the front of the queue
563uintptr_t front( condition & this ) {
564 verifyf( !is_empty(this),
565 "Attempt to access user data on an empty condition.\n"
566 "Possible cause is not checking if the condition is empty before reading stored data."
567 );
568 return ((typeof(this.blocked.head))this.blocked.head)->user_info;
569}
570
571//-----------------------------------------------------------------------------
572// External scheduling
573// cases to handle :
574// - target already there :
575// block and wake
576// - dtor already there
577// put thread on signaller stack
578// - non-blocking
579// return else
580// - timeout
581// return timeout
582// - block
583// setup mask
584// block
585void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
586 // This statment doesn't have a contiguous list of monitors...
587 // Create one!
588 __lock_size_t max = count_max( mask );
589 $monitor * mon_storage[max];
590 __builtin_memset( mon_storage, 0, sizeof( mon_storage ) );
591 __lock_size_t actual_count = aggregate( mon_storage, mask );
592
593 __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max);
594
595 if(actual_count == 0) return;
596
597 __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n" );
598
599 // Create storage for monitor context
600 monitor_ctx( mon_storage, actual_count );
601
602 // Lock all monitors (aggregates the locks as well)
603 lock_all( monitors, locks, count );
604
605 {
606 // Check if the entry queue
607 $thread * next; int index;
608 [next, index] = search_entry_queue( mask, monitors, count );
609
610 if( next ) {
611 *mask.accepted = index;
612 __acceptable_t& accepted = mask[index];
613 if( accepted.is_dtor ) {
614 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" );
615 verifyf( accepted.size == 1, "ERROR: Accepted dtor has more than 1 mutex parameter." );
616
617 $monitor * mon2dtor = accepted[0];
618 verifyf( mon2dtor->dtor_node, "ERROR: Accepted monitor has no dtor_node." );
619
620 __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria;
621 push( mon2dtor->signal_stack, dtor_crit );
622
623 unlock_all( locks, count );
624 }
625 else {
626 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n" );
627
628 // Create the node specific to this wait operation
629 wait_ctx_primed( kernelTLS.this_thread, 0 );
630
631 // Save monitor states
632 monitor_save;
633
634 __cfaabi_dbg_print_buffer_local( "Kernel : baton of %"PRIdFAST16" monitors : ", count );
635 #ifdef __CFA_DEBUG_PRINT__
636 for( int i = 0; i < count; i++) {
637 __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
638 }
639 #endif
640 __cfaabi_dbg_print_buffer_local( "\n" );
641
642 // Set the owners to be the next thread
643 __set_owner( monitors, count, next );
644
645 // unlock all the monitors
646 unlock_all( locks, count );
647
648 // unpark the thread we signalled
649 unpark( next );
650
651 //Everything is ready to go to sleep
652 park();
653
654 // We are back, restore the owners and recursions
655 monitor_restore;
656
657 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n" );
658 }
659
660 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
661 return;
662 }
663 }
664
665
666 if( duration == 0 ) {
667 __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n" );
668
669 unlock_all( locks, count );
670
671 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
672 return;
673 }
674
675
676 verifyf( duration < 0, "Timeout on waitfor statments not supported yet." );
677
678 __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n" );
679
680 // Create the node specific to this wait operation
681 wait_ctx_primed( kernelTLS.this_thread, 0 );
682
683 monitor_save;
684 set_mask( monitors, count, mask );
685
686 for( __lock_size_t i = 0; i < count; i++) {
687 verify( monitors[i]->owner == kernelTLS.this_thread );
688 }
689
690 // unlock all the monitors
691 unlock_all( locks, count );
692
693 //Everything is ready to go to sleep
694 park();
695
696
697 // WE WOKE UP
698
699
700 //We are back, restore the masks and recursions
701 monitor_restore;
702
703 __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n" );
704
705 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
706}
707
708//-----------------------------------------------------------------------------
709// Utilities
710
711static inline void __set_owner( $monitor * this, $thread * owner ) {
712 /* paranoid */ verify( this->lock.lock );
713
714 //Pass the monitor appropriately
715 this->owner = owner;
716
717 //We are passing the monitor to someone else, which means recursion level is not 0
718 this->recursion = owner ? 1 : 0;
719}
720
721static inline void __set_owner( $monitor * monitors [], __lock_size_t count, $thread * owner ) {
722 /* paranoid */ verify ( monitors[0]->lock.lock );
723 /* paranoid */ verifyf( monitors[0]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[0]->owner, monitors[0]->recursion, monitors[0] );
724 monitors[0]->owner = owner;
725 monitors[0]->recursion = 1;
726 for( __lock_size_t i = 1; i < count; i++ ) {
727 /* paranoid */ verify ( monitors[i]->lock.lock );
728 /* paranoid */ verifyf( monitors[i]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[i]->owner, monitors[i]->recursion, monitors[i] );
729 monitors[i]->owner = owner;
730 monitors[i]->recursion = 0;
731 }
732}
733
734static inline void set_mask( $monitor * storage [], __lock_size_t count, const __waitfor_mask_t & mask ) {
735 for( __lock_size_t i = 0; i < count; i++) {
736 storage[i]->mask = mask;
737 }
738}
739
740static inline void reset_mask( $monitor * this ) {
741 this->mask.accepted = 0p;
742 this->mask.data = 0p;
743 this->mask.size = 0;
744}
745
746static inline $thread * next_thread( $monitor * this ) {
747 //Check the signaller stack
748 __cfaabi_dbg_print_safe( "Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top);
749 __condition_criterion_t * urgent = pop( this->signal_stack );
750 if( urgent ) {
751 //The signaller stack is not empty,
752 //regardless of if we are ready to baton pass,
753 //we need to set the monitor as in use
754 /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
755 __set_owner( this, urgent->owner->waiting_thread );
756
757 return check_condition( urgent );
758 }
759
760 // No signaller thread
761 // Get the next thread in the entry_queue
762 $thread * new_owner = pop_head( this->entry_queue );
763 /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
764 /* paranoid */ verify( !new_owner || new_owner->next == 0p );
765 __set_owner( this, new_owner );
766
767 return new_owner;
768}
769
770static inline bool is_accepted( $monitor * this, const __monitor_group_t & group ) {
771 __acceptable_t * it = this->mask.data; // Optim
772 __lock_size_t count = this->mask.size;
773
774 // Check if there are any acceptable functions
775 if( !it ) return false;
776
777 // If this isn't the first monitor to test this, there is no reason to repeat the test.
778 if( this != group[0] ) return group[0]->mask.accepted >= 0;
779
780 // For all acceptable functions check if this is the current function.
781 for( __lock_size_t i = 0; i < count; i++, it++ ) {
782 if( *it == group ) {
783 *this->mask.accepted = i;
784 return true;
785 }
786 }
787
788 // No function matched
789 return false;
790}
791
792static inline void init( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
793 for( __lock_size_t i = 0; i < count; i++) {
794 (criteria[i]){ monitors[i], waiter };
795 }
796
797 waiter.criteria = criteria;
798}
799
800static inline void init_push( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
801 for( __lock_size_t i = 0; i < count; i++) {
802 (criteria[i]){ monitors[i], waiter };
803 __cfaabi_dbg_print_safe( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] );
804 push( criteria[i].target->signal_stack, &criteria[i] );
805 }
806
807 waiter.criteria = criteria;
808}
809
810static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
811 for( __lock_size_t i = 0; i < count; i++ ) {
812 lock( *locks[i] __cfaabi_dbg_ctx2 );
813 }
814}
815
816static inline void lock_all( $monitor * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) {
817 for( __lock_size_t i = 0; i < count; i++ ) {
818 __spinlock_t * l = &source[i]->lock;
819 lock( *l __cfaabi_dbg_ctx2 );
820 if(locks) locks[i] = l;
821 }
822}
823
824static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) {
825 for( __lock_size_t i = 0; i < count; i++ ) {
826 unlock( *locks[i] );
827 }
828}
829
830static inline void unlock_all( $monitor * locks [], __lock_size_t count ) {
831 for( __lock_size_t i = 0; i < count; i++ ) {
832 unlock( locks[i]->lock );
833 }
834}
835
836static inline void save(
837 $monitor * ctx [],
838 __lock_size_t count,
839 __attribute((unused)) __spinlock_t * locks [],
840 unsigned int /*out*/ recursions [],
841 __waitfor_mask_t /*out*/ masks []
842) {
843 for( __lock_size_t i = 0; i < count; i++ ) {
844 recursions[i] = ctx[i]->recursion;
845 masks[i] = ctx[i]->mask;
846 }
847}
848
849static inline void restore(
850 $monitor * ctx [],
851 __lock_size_t count,
852 __spinlock_t * locks [],
853 unsigned int /*out*/ recursions [],
854 __waitfor_mask_t /*out*/ masks []
855) {
856 lock_all( locks, count );
857 for( __lock_size_t i = 0; i < count; i++ ) {
858 ctx[i]->recursion = recursions[i];
859 ctx[i]->mask = masks[i];
860 }
861 unlock_all( locks, count );
862}
863
864// Function has 2 different behavior
865// 1 - Marks a monitors as being ready to run
866// 2 - Checks if all the monitors are ready to run
867// if so return the thread to run
868static inline $thread * check_condition( __condition_criterion_t * target ) {
869 __condition_node_t * node = target->owner;
870 unsigned short count = node->count;
871 __condition_criterion_t * criteria = node->criteria;
872
873 bool ready2run = true;
874
875 for( int i = 0; i < count; i++ ) {
876
877 // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );
878 if( &criteria[i] == target ) {
879 criteria[i].ready = true;
880 // __cfaabi_dbg_print_safe( "True\n" );
881 }
882
883 ready2run = criteria[i].ready && ready2run;
884 }
885
886 __cfaabi_dbg_print_safe( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : 0p );
887 return ready2run ? node->waiting_thread : 0p;
888}
889
890static inline void brand_condition( condition & this ) {
891 $thread * thrd = TL_GET( this_thread );
892 if( !this.monitors ) {
893 // __cfaabi_dbg_print_safe( "Branding\n" );
894 assertf( thrd->monitors.data != 0p, "No current monitor to brand condition %p", thrd->monitors.data );
895 this.monitor_count = thrd->monitors.size;
896
897 this.monitors = ($monitor **)malloc( this.monitor_count * sizeof( *this.monitors ) );
898 for( int i = 0; i < this.monitor_count; i++ ) {
899 this.monitors[i] = thrd->monitors[i];
900 }
901 }
902}
903
904static inline [$thread *, int] search_entry_queue( const __waitfor_mask_t & mask, $monitor * monitors [], __lock_size_t count ) {
905
906 __queue_t($thread) & entry_queue = monitors[0]->entry_queue;
907
908 // For each thread in the entry-queue
909 for( $thread ** thrd_it = &entry_queue.head;
910 *thrd_it != 1p;
911 thrd_it = &(*thrd_it)->next
912 ) {
913 // For each acceptable check if it matches
914 int i = 0;
915 __acceptable_t * end = end (mask);
916 __acceptable_t * begin = begin(mask);
917 for( __acceptable_t * it = begin; it != end; it++, i++ ) {
918 // Check if we have a match
919 if( *it == (*thrd_it)->monitors ) {
920
921 // If we have a match return it
922 // after removeing it from the entry queue
923 return [remove( entry_queue, thrd_it ), i];
924 }
925 }
926 }
927
928 return [0, -1];
929}
930
931forall(dtype T | sized( T ))
932static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) {
933 if( !val ) return size;
934
935 for( __lock_size_t i = 0; i <= size; i++) {
936 if( array[i] == val ) return size;
937 }
938
939 array[size] = val;
940 size = size + 1;
941 return size;
942}
943
944static inline __lock_size_t count_max( const __waitfor_mask_t & mask ) {
945 __lock_size_t max = 0;
946 for( __lock_size_t i = 0; i < mask.size; i++ ) {
947 __acceptable_t & accepted = mask[i];
948 max += accepted.size;
949 }
950 return max;
951}
952
953static inline __lock_size_t aggregate( $monitor * storage [], const __waitfor_mask_t & mask ) {
954 __lock_size_t size = 0;
955 for( __lock_size_t i = 0; i < mask.size; i++ ) {
956 __acceptable_t & accepted = mask[i];
957 __libcfa_small_sort( accepted.data, accepted.size );
958 for( __lock_size_t j = 0; j < accepted.size; j++) {
959 insert_unique( storage, size, accepted[j] );
960 }
961 }
962 // TODO insertion sort instead of this
963 __libcfa_small_sort( storage, size );
964 return size;
965}
966
967// Local Variables: //
968// mode: c //
969// tab-width: 4 //
970// End: //
Note: See TracBrowser for help on using the repository browser.