source: src/libcfa/concurrency/monitor.c@ 5363fdf

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 5363fdf was daacf82, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Added test for validate single monitor barging avoidance for waitfor statments

  • Property mode set to 100644
File size: 24.1 KB
RevLine 
[f07e037]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//
[84c52a8]7// monitor_desc.c --
[f07e037]8//
9// Author : Thierry Delisle
10// Created On : Thd Feb 23 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[38ef0de]12// Last Modified On : Mon Jul 31 14:59:05 2017
13// Update Count : 3
[f07e037]14//
15
16#include "monitor"
17
[a933dcf4]18#include <stdlib>
19
[5ea06d6]20#include "libhdr.h"
[2ac095d]21#include "kernel_private.h"
[f07e037]22
[0c78741]23//-----------------------------------------------------------------------------
24// Forward declarations
[daacf82]25static inline void set_owner ( monitor_desc * this, thread_desc * owner );
26static inline void set_owner ( monitor_desc ** storage, short count, thread_desc * owner );
27static inline void set_mask ( monitor_desc ** storage, short count, const __waitfor_mask_t & mask );
28static inline void reset_mask( monitor_desc * this );
[6ff4507]29
[0c78741]30static inline thread_desc * next_thread( monitor_desc * this );
[6ae8c92]31static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
[0c78741]32
33static inline void lock_all( spinlock ** locks, unsigned short count );
34static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count );
35static inline void unlock_all( spinlock ** locks, unsigned short count );
36static inline void unlock_all( monitor_desc ** locks, unsigned short count );
37
[6ff4507]38static inline void save ( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks );
39static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*in */ recursions, __waitfor_mask_t * /*in */ masks );
[0c78741]40
[97e3296]41static inline void init ( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
42static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
43
[6ff4507]44static inline thread_desc * check_condition ( __condition_criterion_t * );
45static inline void brand_condition ( condition * );
[6ae8c92]46static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc ** monitors, int count );
[b18830e]47
[6ff4507]48forall(dtype T | sized( T ))
49static inline short insert_unique( T ** array, short & size, T * val );
50static inline short count_max ( const __waitfor_mask_t & mask );
51static inline short aggregate ( monitor_desc ** storage, const __waitfor_mask_t & mask );
[97e3296]52
53//-----------------------------------------------------------------------------
54// Useful defines
[6ff4507]55#define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
56 __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
57 __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
58 init( count, monitors, &waiter, criteria ); /* Link everything together */ \
59
60#define wait_ctx_primed(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
61 __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
62 __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
63 init_push( count, monitors, &waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \
64
65#define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \
66 monitor_desc ** monitors = mons; /* Save the targeted monitors */ \
67 unsigned short count = cnt; /* Save the count to a local variable */ \
68 unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \
69 __waitfor_mask_t masks[ count ]; /* Save the current waitfor masks to restore them later */ \
70 spinlock * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \
71
72#define monitor_save save ( monitors, count, locks, recursions, masks )
73#define monitor_restore restore( monitors, count, locks, recursions, masks )
74
[97e3296]75
[0c78741]76//-----------------------------------------------------------------------------
77// Enter/Leave routines
[690f13c]78
79
[cb0e6de]80extern "C" {
[97e3296]81 // Enter single monitor
[a843067]82 static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
[97e3296]83 // Lock the monitor spinlock, lock_yield to reduce contention
[b227f68]84 lock_yield( &this->lock DEBUG_CTX2 );
[1c273d0]85 thread_desc * thrd = this_thread;
[f07e037]86
[90c4df0]87 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
88
[cb0e6de]89 if( !this->owner ) {
[97e3296]90 // No one has the monitor, just take it
[cd348e7]91 set_owner( this, thrd );
[90c4df0]92
93 LIB_DEBUG_PRINT_SAFE("Kernel : mon is free \n");
[cb0e6de]94 }
95 else if( this->owner == thrd) {
[97e3296]96 // We already have the monitor, just not how many times we took it
[4aa2fb2]97 verify( this->recursion > 0 );
[cb0e6de]98 this->recursion += 1;
[90c4df0]99
100 LIB_DEBUG_PRINT_SAFE("Kernel : mon already owned \n");
[cb0e6de]101 }
[6ae8c92]102 else if( is_accepted( this, group) ) {
[97e3296]103 // Some one was waiting for us, enter
104 set_owner( this, thrd );
[90c4df0]105
[daacf82]106 // Reset mask
107 reset_mask( this );
108
[90c4df0]109 LIB_DEBUG_PRINT_SAFE("Kernel : mon accepts \n");
[97e3296]110 }
[cb0e6de]111 else {
[90c4df0]112 LIB_DEBUG_PRINT_SAFE("Kernel : blocking \n");
113
[97e3296]114 // Some one else has the monitor, wait in line for it
[cb0e6de]115 append( &this->entry_queue, thrd );
[82ff5845]116 BlockInternal( &this->lock );
[cc7f4b1]117
[90c4df0]118 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered mon %p\n", thrd, this);
119
[97e3296]120 // BlockInternal will unlock spinlock, no need to unlock ourselves
[2ac095d]121 return;
[cb0e6de]122 }
[f07e037]123
[90c4df0]124 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered mon %p\n", thrd, this);
125
[97e3296]126 // Release the lock and leave
[cb0e6de]127 unlock( &this->lock );
[5ea06d6]128 return;
[cb0e6de]129 }
[f07e037]130
[97e3296]131 // Leave single monitor
[1c273d0]132 void __leave_monitor_desc( monitor_desc * this ) {
[97e3296]133 // Lock the monitor spinlock, lock_yield to reduce contention
[b227f68]134 lock_yield( &this->lock DEBUG_CTX2 );
[f07e037]135
[a843067]136 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
137
138 verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", this_thread, this->owner, this->recursion, this );
[cc7f4b1]139
[97e3296]140 // Leaving a recursion level, decrement the counter
[cb0e6de]141 this->recursion -= 1;
[f07e037]142
[97e3296]143 // If we haven't left the last level of recursion
144 // it means we don't need to do anything
[690f13c]145 if( this->recursion != 0) {
146 unlock( &this->lock );
147 return;
148 }
[f07e037]149
[97e3296]150 // Get the next thread, will be null on low contention monitor
[0c78741]151 thread_desc * new_owner = next_thread( this );
[5ea06d6]152
[97e3296]153 // We can now let other threads in safely
[cb0e6de]154 unlock( &this->lock );
[51f3798]155
[690f13c]156 //We need to wake-up the thread
[1c273d0]157 WakeThread( new_owner );
158 }
159
[97e3296]160 // Leave the thread monitor
161 // last routine called by a thread.
162 // Should never return
[1c273d0]163 void __leave_thread_monitor( thread_desc * thrd ) {
[b18830e]164 monitor_desc * this = &thrd->self_mon;
[97e3296]165
166 // Lock the monitor now
[b227f68]167 lock_yield( &this->lock DEBUG_CTX2 );
[1c273d0]168
169 disable_interrupts();
170
[b18830e]171 thrd->self_cor.state = Halted;
[1c273d0]172
[a843067]173 verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
[1c273d0]174
[97e3296]175 // Leaving a recursion level, decrement the counter
[1c273d0]176 this->recursion -= 1;
177
[97e3296]178 // If we haven't left the last level of recursion
179 // it must mean there is an error
180 if( this->recursion != 0) { abortf("Thread internal monitor has unbalanced recursion"); }
[1c273d0]181
[97e3296]182 // Fetch the next thread, can be null
[1c273d0]183 thread_desc * new_owner = next_thread( this );
184
[97e3296]185 // Leave the thread, this will unlock the spinlock
186 // Use leave thread instead of BlockInternal which is
187 // specialized for this case and supports null new_owner
[f2b12406]188 LeaveThread( &this->lock, new_owner );
[97e3296]189
190 // Control flow should never reach here!
[cc7f4b1]191 }
[2781e65]192}
193
[97e3296]194// Enter multiple monitor
195// relies on the monitor array being sorted
[6ae8c92]196static inline void enter( __monitor_group_t monitors ) {
[b18830e]197 for(int i = 0; i < monitors.size; i++) {
[a843067]198 __enter_monitor_desc( monitors.list[i], monitors );
[97e3296]199 }
[2781e65]200}
201
[97e3296]202// Leave multiple monitor
203// relies on the monitor array being sorted
[5ea06d6]204static inline void leave(monitor_desc ** monitors, int count) {
[0c78741]205 for(int i = count - 1; i >= 0; i--) {
206 __leave_monitor_desc( monitors[i] );
[2781e65]207 }
[5ea06d6]208}
209
[97e3296]210// Ctor for monitor guard
211// Sorts monitors before entering
[6b224a52]212void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() ) {
[97e3296]213 // Store current array
[242a902]214 this.m = m;
215 this.count = count;
[97e3296]216
217 // Sort monitors based on address -> TODO use a sort specialized for small numbers
[242a902]218 qsort(this.m, count);
[5ea06d6]219
[97e3296]220 // Save previous thread context
[b18830e]221 this.prev_mntrs = this_thread->monitors.list;
222 this.prev_count = this_thread->monitors.size;
223 this.prev_func = this_thread->monitors.func;
[5ea06d6]224
[97e3296]225 // Update thread context (needed for conditions)
[b18830e]226 this_thread->monitors.list = m;
227 this_thread->monitors.size = count;
228 this_thread->monitors.func = func;
[90c4df0]229
[19c43b7]230 // LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count);
[a843067]231
[90c4df0]232 // Enter the monitors in order
[6ae8c92]233 __monitor_group_t group = {this.m, this.count, func};
[b18830e]234 enter( group );
[a843067]235
[19c43b7]236 // LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n");
[5ea06d6]237}
238
[6b224a52]239
[97e3296]240// Dtor for monitor guard
[242a902]241void ^?{}( monitor_guard_t & this ) {
[19c43b7]242 // LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count);
[a843067]243
[97e3296]244 // Leave the monitors in order
[242a902]245 leave( this.m, this.count );
[5ea06d6]246
[19c43b7]247 // LIB_DEBUG_PRINT_SAFE("MGUARD : left\n");
[a843067]248
[97e3296]249 // Restore thread context
[b18830e]250 this_thread->monitors.list = this.prev_mntrs;
251 this_thread->monitors.size = this.prev_count;
252 this_thread->monitors.func = this.prev_func;
[5ea06d6]253}
254
[97e3296]255//-----------------------------------------------------------------------------
256// Internal scheduling types
[242a902]257void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
258 this.waiting_thread = waiting_thread;
259 this.count = count;
260 this.next = NULL;
261 this.user_info = user_info;
[be3d020]262}
263
[242a902]264void ?{}(__condition_criterion_t & this ) {
265 this.ready = false;
266 this.target = NULL;
267 this.owner = NULL;
268 this.next = NULL;
[be3d020]269}
270
[242a902]271void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ) {
272 this.ready = false;
273 this.target = target;
274 this.owner = owner;
275 this.next = NULL;
[ad1a8dd]276}
277
[5ea06d6]278//-----------------------------------------------------------------------------
279// Internal scheduling
[be3d020]280void wait( condition * this, uintptr_t user_info = 0 ) {
[0c78741]281 brand_condition( this );
[5ea06d6]282
[97e3296]283 // Check that everything is as expected
[0c78741]284 assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
[4aa2fb2]285 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
286 verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
[5ea06d6]287
[97e3296]288 // Create storage for monitor context
289 monitor_ctx( this->monitors, this->monitor_count );
[0c78741]290
[97e3296]291 // Create the node specific to this wait operation
292 wait_ctx( this_thread, user_info );
[0c78741]293
[97e3296]294 // Append the current wait operation to the ones already queued on the condition
295 // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
296 append( &this->blocked, &waiter );
[0c78741]297
[6ff4507]298 // Lock all monitors (aggregates the locks as well)
[97e3296]299 lock_all( monitors, locks, count );
[5ea06d6]300
[97e3296]301 // Find the next thread(s) to run
[6ff4507]302 short thread_count = 0;
[0c78741]303 thread_desc * threads[ count ];
[ad1a8dd]304 for(int i = 0; i < count; i++) {
305 threads[i] = 0;
306 }
307
[a843067]308 // Save monitor states
309 monitor_save;
310
[97e3296]311 // Remove any duplicate threads
[0c78741]312 for( int i = 0; i < count; i++) {
[97e3296]313 thread_desc * new_owner = next_thread( monitors[i] );
[6ff4507]314 insert_unique( threads, thread_count, new_owner );
[5ea06d6]315 }
316
[a843067]317 // Everything is ready to go to sleep
318 BlockInternal( locks, count, threads, thread_count );
319
320 // We are back, restore the owners and recursions
321 monitor_restore;
[5ea06d6]322}
323
[be3d020]324bool signal( condition * this ) {
[97e3296]325 if( is_empty( this ) ) { return false; }
[5ea06d6]326
327 //Check that everything is as expected
[4aa2fb2]328 verify( this->monitors );
329 verify( this->monitor_count != 0 );
[0c78741]330
[44264c5]331 //Some more checking in debug
[5ea06d6]332 LIB_DEBUG_DO(
[1c273d0]333 thread_desc * this_thrd = this_thread;
[b18830e]334 if ( this->monitor_count != this_thrd->monitors.size ) {
335 abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->monitors.size );
[97e3296]336 }
[0c78741]337
338 for(int i = 0; i < this->monitor_count; i++) {
[b18830e]339 if ( this->monitors[i] != this_thrd->monitors.list[i] ) {
340 abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->monitors.list[i] );
[97e3296]341 }
[0c78741]342 }
[5ea06d6]343 );
344
[97e3296]345 unsigned short count = this->monitor_count;
346
347 // Lock all monitors
[0c78741]348 lock_all( this->monitors, NULL, count );
349
[44264c5]350 //Pop the head of the waiting queue
[0c78741]351 __condition_node_t * node = pop_head( &this->blocked );
[44264c5]352
353 //Add the thread to the proper AS stack
[0c78741]354 for(int i = 0; i < count; i++) {
355 __condition_criterion_t * crit = &node->criteria[i];
356 assert( !crit->ready );
357 push( &crit->target->signal_stack, crit );
[5ea06d6]358 }
[0c78741]359
[44264c5]360 //Release
[0c78741]361 unlock_all( this->monitors, count );
[be3d020]362
363 return true;
[5ea06d6]364}
365
[be3d020]366bool signal_block( condition * this ) {
[97e3296]367 if( !this->blocked.head ) { return false; }
[44264c5]368
369 //Check that everything is as expected
[4aa2fb2]370 verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
371 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
[44264c5]372
[97e3296]373 // Create storage for monitor context
374 monitor_ctx( this->monitors, this->monitor_count );
[44264c5]375
[97e3296]376 // Lock all monitors (aggregates the locks them as well)
377 lock_all( monitors, locks, count );
[44264c5]378
[97e3296]379 // Create the node specific to this wait operation
380 wait_ctx_primed( this_thread, 0 )
[44264c5]381
382 //save contexts
[6ff4507]383 monitor_save;
[44264c5]384
385 //Find the thread to run
386 thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
[6ff4507]387 set_owner( monitors, count, signallee );
[44264c5]388
389 //Everything is ready to go to sleep
[82ff5845]390 BlockInternal( locks, count, &signallee, 1 );
[44264c5]391
[c81ebf9]392
[97e3296]393 // WE WOKE UP
[c81ebf9]394
395
[6ff4507]396 //We are back, restore the masks and recursions
397 monitor_restore;
[be3d020]398
399 return true;
400}
401
[97e3296]402// Access the user_info of the thread waiting at the front of the queue
[be3d020]403uintptr_t front( condition * this ) {
[2ac095d]404 verifyf( !is_empty(this),
[4aa2fb2]405 "Attempt to access user data on an empty condition.\n"
406 "Possible cause is not checking if the condition is empty before reading stored data."
[be3d020]407 );
408 return this->blocked.head->user_info;
[44264c5]409}
410
[c81ebf9]411//-----------------------------------------------------------------------------
[b18830e]412// External scheduling
413// cases to handle :
414// - target already there :
415// block and wake
416// - dtor already there
417// put thread on signaller stack
418// - non-blocking
419// return else
420// - timeout
421// return timeout
422// - block
423// setup mask
424// block
[6ae8c92]425void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
[b18830e]426 // This statment doesn't have a contiguous list of monitors...
427 // Create one!
[6ae8c92]428 short max = count_max( mask );
[b18830e]429 monitor_desc * mon_storage[max];
[6ae8c92]430 short actual_count = aggregate( mon_storage, mask );
[97e3296]431
[daacf82]432 if(actual_count == 0) return;
[19c43b7]433
434 LIB_DEBUG_PRINT_SAFE("Kernel : waitfor internal proceeding\n");
[4cc9b13]435
[97e3296]436 // Create storage for monitor context
[b18830e]437 monitor_ctx( mon_storage, actual_count );
[c81ebf9]438
[6ff4507]439 // Lock all monitors (aggregates the locks as well)
[97e3296]440 lock_all( monitors, locks, count );
[c81ebf9]441
[b18830e]442 {
443 // Check if the entry queue
[6ae8c92]444 thread_desc * next; int index;
445 [next, index] = search_entry_queue( mask, monitors, count );
[b18830e]446
447 if( next ) {
[19c43b7]448 *mask.accepted = index;
[6ae8c92]449 if( mask.clauses[index].is_dtor ) {
[b18830e]450 #warning case not implemented
451 }
452 else {
[19c43b7]453 LIB_DEBUG_PRINT_SAFE("Kernel : thread present, baton-passing\n");
454
455 // Create the node specific to this wait operation
456 wait_ctx_primed( this_thread, 0 );
457
458 // Save monitor states
459 monitor_save;
460
461 // Set the owners to be the next thread
462 set_owner( monitors, count, next );
463
464 // Everything is ready to go to sleep
465 BlockInternal( locks, count, &next, 1 );
466
467 // We are back, restore the owners and recursions
468 monitor_restore;
469
470 LIB_DEBUG_PRINT_SAFE("Kernel : thread present, returned\n");
[b18830e]471 }
472
[19c43b7]473 LIB_DEBUG_PRINT_SAFE("Kernel : accepted %d\n", *mask.accepted);
474
475 return;
[90c4df0]476 }
477 }
478
[c81ebf9]479
[4cc9b13]480 if( duration == 0 ) {
[19c43b7]481 LIB_DEBUG_PRINT_SAFE("Kernel : non-blocking, exiting\n");
482
[4cc9b13]483 unlock_all( locks, count );
[19c43b7]484
485 LIB_DEBUG_PRINT_SAFE("Kernel : accepted %d\n", *mask.accepted);
[4cc9b13]486 return;
487 }
[b18830e]488
489
490 verifyf( duration < 0, "Timeout on waitfor statments not supported yet.");
491
[19c43b7]492 LIB_DEBUG_PRINT_SAFE("Kernel : blocking waitfor\n");
493
494 // Create the node specific to this wait operation
495 wait_ctx_primed( this_thread, 0 );
[b18830e]496
[6ff4507]497 monitor_save;
[6ae8c92]498 set_mask( monitors, count, mask );
[c81ebf9]499
[daacf82]500 for(int i = 0; i < count; i++) {
501 verify( monitors[i]->owner == this_thread );
502 }
503
[19c43b7]504 //Everything is ready to go to sleep
505 BlockInternal( locks, count );
506
507
508 // WE WOKE UP
509
510
511 //We are back, restore the masks and recursions
512 monitor_restore;
513
514 LIB_DEBUG_PRINT_SAFE("Kernel : exiting\n");
515
516 LIB_DEBUG_PRINT_SAFE("Kernel : accepted %d\n", *mask.accepted);
[c81ebf9]517}
518
[0c78741]519//-----------------------------------------------------------------------------
520// Utilities
521
522static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
[19c43b7]523 // LIB_DEBUG_PRINT_SAFE("Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
[a843067]524
[0c78741]525 //Pass the monitor appropriately
526 this->owner = owner;
527
528 //We are passing the monitor to someone else, which means recursion level is not 0
529 this->recursion = owner ? 1 : 0;
530}
531
[6ff4507]532static inline void set_owner( monitor_desc ** monitors, short count, thread_desc * owner ) {
533 for( int i = 0; i < count; i++ ) {
534 set_owner( monitors[i], owner );
535 }
536}
537
538static inline void set_mask( monitor_desc ** storage, short count, const __waitfor_mask_t & mask ) {
539 for(int i = 0; i < count; i++) {
540 storage[i]->mask = mask;
541 }
542}
543
[daacf82]544static inline void reset_mask( monitor_desc * this ) {
545 this->mask.accepted = NULL;
546 this->mask.clauses = NULL;
547 this->mask.size = 0;
548}
549
[0c78741]550static inline thread_desc * next_thread( monitor_desc * this ) {
551 //Check the signaller stack
552 __condition_criterion_t * urgent = pop( &this->signal_stack );
553 if( urgent ) {
554 //The signaller stack is not empty,
555 //regardless of if we are ready to baton pass,
556 //we need to set the monitor as in use
557 set_owner( this, urgent->owner->waiting_thread );
558
559 return check_condition( urgent );
560 }
561
562 // No signaller thread
563 // Get the next thread in the entry_queue
564 thread_desc * new_owner = pop_head( &this->entry_queue );
565 set_owner( this, new_owner );
566
567 return new_owner;
568}
569
[6ff4507]570static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & group ) {
571 __acceptable_t * it = this->mask.clauses; // Optim
572 int count = this->mask.size;
573
574 // Check if there are any acceptable functions
[a843067]575 if( !it ) return false;
[6ff4507]576
577 // If this isn't the first monitor to test this, there is no reason to repeat the test.
578 if( this != group[0] ) return group[0]->mask.accepted >= 0;
579
580 // For all acceptable functions check if this is the current function.
581 for( short i = 0; i < count; i++, it++ ) {
582 if( *it == group ) {
583 *this->mask.accepted = i;
584 return true;
585 }
586 }
587
588 // No function matched
589 return false;
590}
591
[97e3296]592static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
593 for(int i = 0; i < count; i++) {
[6b224a52]594 (criteria[i]){ monitors[i], waiter };
[97e3296]595 }
596
597 waiter->criteria = criteria;
598}
599
600static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
601 for(int i = 0; i < count; i++) {
[6b224a52]602 (criteria[i]){ monitors[i], waiter };
[97e3296]603 push( &criteria[i].target->signal_stack, &criteria[i] );
604 }
605
606 waiter->criteria = criteria;
607}
608
[0c78741]609static inline void lock_all( spinlock ** locks, unsigned short count ) {
610 for( int i = 0; i < count; i++ ) {
[b227f68]611 lock_yield( locks[i] DEBUG_CTX2 );
[0c78741]612 }
613}
614
615static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
616 for( int i = 0; i < count; i++ ) {
617 spinlock * l = &source[i]->lock;
[b227f68]618 lock_yield( l DEBUG_CTX2 );
[0c78741]619 if(locks) locks[i] = l;
620 }
621}
622
623static inline void unlock_all( spinlock ** locks, unsigned short count ) {
624 for( int i = 0; i < count; i++ ) {
625 unlock( locks[i] );
626 }
627}
628
629static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
630 for( int i = 0; i < count; i++ ) {
631 unlock( &locks[i]->lock );
632 }
633}
634
[daacf82]635static inline void save( monitor_desc ** ctx, short count, __attribute((unused)) spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
[0c78741]636 for( int i = 0; i < count; i++ ) {
637 recursions[i] = ctx[i]->recursion;
[6ff4507]638 masks[i] = ctx[i]->mask;
[0c78741]639 }
640}
641
[6ff4507]642static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
643 lock_all( locks, count );
[0c78741]644 for( int i = 0; i < count; i++ ) {
645 ctx[i]->recursion = recursions[i];
[6ff4507]646 ctx[i]->mask = masks[i];
[0c78741]647 }
[6ff4507]648 unlock_all( locks, count );
[0c78741]649}
650
651// Function has 2 different behavior
652// 1 - Marks a monitors as being ready to run
653// 2 - Checks if all the monitors are ready to run
654// if so return the thread to run
655static inline thread_desc * check_condition( __condition_criterion_t * target ) {
656 __condition_node_t * node = target->owner;
657 unsigned short count = node->count;
658 __condition_criterion_t * criteria = node->criteria;
659
660 bool ready2run = true;
661
662 for( int i = 0; i < count; i++ ) {
[44264c5]663
[b227f68]664 // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
[0c78741]665 if( &criteria[i] == target ) {
666 criteria[i].ready = true;
[b227f68]667 // LIB_DEBUG_PRINT_SAFE( "True\n" );
[0c78741]668 }
669
670 ready2run = criteria[i].ready && ready2run;
671 }
672
[b227f68]673 // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
[0c78741]674 return ready2run ? node->waiting_thread : NULL;
675}
676
677static inline void brand_condition( condition * this ) {
[1c273d0]678 thread_desc * thrd = this_thread;
[0c78741]679 if( !this->monitors ) {
[b227f68]680 // LIB_DEBUG_PRINT_SAFE("Branding\n");
[b18830e]681 assertf( thrd->monitors.list != NULL, "No current monitor to brand condition %p", thrd->monitors.list );
682 this->monitor_count = thrd->monitors.size;
[a933dcf4]683
684 this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
685 for( int i = 0; i < this->monitor_count; i++ ) {
[b18830e]686 this->monitors[i] = thrd->monitors.list[i];
[a933dcf4]687 }
[0c78741]688 }
689}
690
[6ae8c92]691static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc ** monitors, int count ) {
[90c4df0]692
693 __thread_queue_t * entry_queue = &monitors[0]->entry_queue;
694
695 // For each thread in the entry-queue
696 for( thread_desc ** thrd_it = &entry_queue->head;
697 *thrd_it;
[4cc9b13]698 thrd_it = &(*thrd_it)->next
699 ) {
[90c4df0]700 // For each acceptable check if it matches
[4cc9b13]701 int i = 0;
[6ae8c92]702 __acceptable_t * end = mask.clauses + mask.size;
703 for( __acceptable_t * it = mask.clauses; it != end; it++, i++ ) {
[90c4df0]704 // Check if we have a match
[aaa4f93]705 if( *it == (*thrd_it)->monitors ) {
[90c4df0]706
707 // If we have a match return it
708 // after removeing it from the entry queue
[b18830e]709 return [remove( entry_queue, thrd_it ), i];
[90c4df0]710 }
711 }
712 }
713
[b18830e]714 return [0, -1];
715}
716
[6ff4507]717forall(dtype T | sized( T ))
718static inline short insert_unique( T ** array, short & size, T * val ) {
719 if( !val ) return size;
720
721 for(int i = 0; i <= size; i++) {
722 if( array[i] == val ) return size;
723 }
724
725 array[size] = val;
726 size = size + 1;
727 return size;
728}
729
[6ae8c92]730static inline short count_max( const __waitfor_mask_t & mask ) {
[b18830e]731 short max = 0;
[6ae8c92]732 for( int i = 0; i < mask.size; i++ ) {
[aaa4f93]733 max += mask.clauses[i].size;
[b18830e]734 }
735 return max;
[97e3296]736}
[b18830e]737
[6ae8c92]738static inline short aggregate( monitor_desc ** storage, const __waitfor_mask_t & mask ) {
[6ff4507]739 short size = 0;
740 for( int i = 0; i < mask.size; i++ ) {
741 for( int j = 0; j < mask.clauses[i].size; j++) {
742 insert_unique( storage, size, mask.clauses[i].list[j] );
743 }
[b18830e]744 }
[6ff4507]745 qsort( storage, size );
746 return size;
[b18830e]747}
748
[242a902]749void ?{}( __condition_blocked_queue_t & this ) {
750 this.head = NULL;
751 this.tail = &this.head;
[0c78741]752}
753
754void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
[4aa2fb2]755 verify(this->tail != NULL);
[0c78741]756 *this->tail = c;
757 this->tail = &c->next;
758}
759
760__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
761 __condition_node_t * head = this->head;
762 if( head ) {
763 this->head = head->next;
764 if( !head->next ) {
765 this->tail = &this->head;
766 }
767 head->next = NULL;
768 }
769 return head;
[4aa2fb2]770}
[6b0b624]771
772// Local Variables: //
773// mode: c //
774// tab-width: 4 //
775// End: //
Note: See TracBrowser for help on using the repository browser.