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

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

Updated accepted index to live in the waitfor caller stack.
Waitfor no longer blocks if all when clauses are false.
Waitfor now properly zeroes out acceptables before use.

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