source: src/libcfa/concurrency/monitor.c@ dbe7756

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 dbe7756 was a843067, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed several errors in monitor.c
Update debug prints
Added proper sched-ext-parse test

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