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

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

Changed lib-side waitfor to use a mask type instead of a pointer and an int. The accepted index is now in the mask type, everything else points to it

  • Property mode set to 100644
File size: 21.9 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 // DON'T unlock, ask the kernel to do it
[5ea06d6]283
[97e3296]284 // Save monitor state
285 save_recursion( monitors, recursions, count );
[5ea06d6]286
[97e3296]287 // Find the next thread(s) to run
[ad1a8dd]288 unsigned short thread_count = 0;
[0c78741]289 thread_desc * threads[ count ];
[ad1a8dd]290 for(int i = 0; i < count; i++) {
291 threads[i] = 0;
292 }
293
[97e3296]294 // Remove any duplicate threads
[0c78741]295 for( int i = 0; i < count; i++) {
[97e3296]296 thread_desc * new_owner = next_thread( monitors[i] );
[ad1a8dd]297 thread_count = insert_unique( threads, thread_count, new_owner );
[5ea06d6]298 }
299
[9c59cd4]300 // Everything is ready to go to sleep
[82ff5845]301 BlockInternal( locks, count, threads, thread_count );
[5ea06d6]302
[c81ebf9]303
[97e3296]304 // WE WOKE UP
[5ea06d6]305
306
[97e3296]307 // We are back, restore the owners and recursions
[9c59cd4]308 lock_all( locks, count );
[97e3296]309 restore_recursion( monitors, recursions, count );
[9c59cd4]310 unlock_all( locks, count );
[5ea06d6]311}
312
[be3d020]313bool signal( condition * this ) {
[97e3296]314 if( is_empty( this ) ) { return false; }
[5ea06d6]315
316 //Check that everything is as expected
[4aa2fb2]317 verify( this->monitors );
318 verify( this->monitor_count != 0 );
[0c78741]319
[44264c5]320 //Some more checking in debug
[5ea06d6]321 LIB_DEBUG_DO(
[1c273d0]322 thread_desc * this_thrd = this_thread;
[b18830e]323 if ( this->monitor_count != this_thrd->monitors.size ) {
324 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]325 }
[0c78741]326
327 for(int i = 0; i < this->monitor_count; i++) {
[b18830e]328 if ( this->monitors[i] != this_thrd->monitors.list[i] ) {
329 abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->monitors.list[i] );
[97e3296]330 }
[0c78741]331 }
[5ea06d6]332 );
333
[97e3296]334 unsigned short count = this->monitor_count;
335
336 // Lock all monitors
[0c78741]337 lock_all( this->monitors, NULL, count );
338
[44264c5]339 //Pop the head of the waiting queue
[0c78741]340 __condition_node_t * node = pop_head( &this->blocked );
[44264c5]341
342 //Add the thread to the proper AS stack
[0c78741]343 for(int i = 0; i < count; i++) {
344 __condition_criterion_t * crit = &node->criteria[i];
345 assert( !crit->ready );
346 push( &crit->target->signal_stack, crit );
[5ea06d6]347 }
[0c78741]348
[44264c5]349 //Release
[0c78741]350 unlock_all( this->monitors, count );
[be3d020]351
352 return true;
[5ea06d6]353}
354
[be3d020]355bool signal_block( condition * this ) {
[97e3296]356 if( !this->blocked.head ) { return false; }
[44264c5]357
358 //Check that everything is as expected
[4aa2fb2]359 verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
360 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
[44264c5]361
[97e3296]362 // Create storage for monitor context
363 monitor_ctx( this->monitors, this->monitor_count );
[44264c5]364
[97e3296]365 // Lock all monitors (aggregates the locks them as well)
366 lock_all( monitors, locks, count );
[44264c5]367
[97e3296]368 // Create the node specific to this wait operation
369 wait_ctx_primed( this_thread, 0 )
[44264c5]370
371 //save contexts
[97e3296]372 save_recursion( monitors, recursions, count );
[44264c5]373
374 //Find the thread to run
375 thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
376 for(int i = 0; i < count; i++) {
[97e3296]377 set_owner( monitors[i], signallee );
[44264c5]378 }
379
380 //Everything is ready to go to sleep
[82ff5845]381 BlockInternal( locks, count, &signallee, 1 );
[44264c5]382
[c81ebf9]383
[97e3296]384 // WE WOKE UP
[c81ebf9]385
386
[44264c5]387 //We are back, restore the owners and recursions
388 lock_all( locks, count );
[97e3296]389 restore_recursion( monitors, recursions, count );
[44264c5]390 unlock_all( locks, count );
[be3d020]391
392 return true;
393}
394
[97e3296]395// Access the user_info of the thread waiting at the front of the queue
[be3d020]396uintptr_t front( condition * this ) {
[2ac095d]397 verifyf( !is_empty(this),
[4aa2fb2]398 "Attempt to access user data on an empty condition.\n"
399 "Possible cause is not checking if the condition is empty before reading stored data."
[be3d020]400 );
401 return this->blocked.head->user_info;
[44264c5]402}
403
[c81ebf9]404//-----------------------------------------------------------------------------
[b18830e]405// External scheduling
406// cases to handle :
407// - target already there :
408// block and wake
409// - dtor already there
410// put thread on signaller stack
411// - non-blocking
412// return else
413// - timeout
414// return timeout
415// - block
416// setup mask
417// block
[6ae8c92]418void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
[b18830e]419 // This statment doesn't have a contiguous list of monitors...
420 // Create one!
[6ae8c92]421 short max = count_max( mask );
[b18830e]422 monitor_desc * mon_storage[max];
[6ae8c92]423 short actual_count = aggregate( mon_storage, mask );
[97e3296]424
425 // Create storage for monitor context
[b18830e]426 monitor_ctx( mon_storage, actual_count );
[c81ebf9]427
[97e3296]428 // Lock all monitors (aggregates the lock them as well)
429 lock_all( monitors, locks, count );
[c81ebf9]430
[b18830e]431 {
432 // Check if the entry queue
[6ae8c92]433 thread_desc * next; int index;
434 [next, index] = search_entry_queue( mask, monitors, count );
[b18830e]435
436 if( next ) {
[6ae8c92]437 if( mask.clauses[index].is_dtor ) {
[b18830e]438 #warning case not implemented
439 }
440 else {
441 save_recursion( monitors, recursions, count );
[c81ebf9]442
[b18830e]443 // Everything is ready to go to sleep
444 BlockInternal( locks, count, &next, 1 );
[c81ebf9]445
[90c4df0]446
[b18830e]447 //WE WOKE UP
[90c4df0]448
[b18830e]449
450 //We are back, restore the owners and recursions
451 lock_all( locks, count );
452 restore_recursion( monitors, recursions, count );
453 unlock_all( locks, count );
454 }
455
456 return index;
[90c4df0]457 }
458 }
459
[c81ebf9]460
[b18830e]461 if( duration == 0 ) return -1;
462
463
464 verifyf( duration < 0, "Timeout on waitfor statments not supported yet.");
465
466
[97e3296]467 save_recursion( monitors, recursions, count );
[6ae8c92]468 set_mask( monitors, count, mask );
[c81ebf9]469
[90c4df0]470
[97e3296]471 // Everything is ready to go to sleep
[b18830e]472 BlockInternal( locks, count );
[c81ebf9]473
474
[97e3296]475 //WE WOKE UP
[c81ebf9]476
477
[97e3296]478 //We are back, restore the owners and recursions
479 lock_all( locks, count );
480 restore_recursion( monitors, recursions, count );
481 unlock_all( locks, count );
[c81ebf9]482
[6ae8c92]483 return mask.accepted;
[c81ebf9]484}
485
[0c78741]486//-----------------------------------------------------------------------------
487// Utilities
488
489static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
490 //Pass the monitor appropriately
491 this->owner = owner;
492
493 //We are passing the monitor to someone else, which means recursion level is not 0
494 this->recursion = owner ? 1 : 0;
495}
496
497static inline thread_desc * next_thread( monitor_desc * this ) {
498 //Check the signaller stack
499 __condition_criterion_t * urgent = pop( &this->signal_stack );
500 if( urgent ) {
501 //The signaller stack is not empty,
502 //regardless of if we are ready to baton pass,
503 //we need to set the monitor as in use
504 set_owner( this, urgent->owner->waiting_thread );
505
506 return check_condition( urgent );
507 }
508
509 // No signaller thread
510 // Get the next thread in the entry_queue
511 thread_desc * new_owner = pop_head( &this->entry_queue );
512 set_owner( this, new_owner );
513
514 return new_owner;
515}
516
[97e3296]517static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
518 for(int i = 0; i < count; i++) {
[6b224a52]519 (criteria[i]){ monitors[i], waiter };
[97e3296]520 }
521
522 waiter->criteria = criteria;
523}
524
525static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
526 for(int i = 0; i < count; i++) {
[6b224a52]527 (criteria[i]){ monitors[i], waiter };
[97e3296]528 push( &criteria[i].target->signal_stack, &criteria[i] );
529 }
530
531 waiter->criteria = criteria;
532}
533
[0c78741]534static inline void lock_all( spinlock ** locks, unsigned short count ) {
535 for( int i = 0; i < count; i++ ) {
[b227f68]536 lock_yield( locks[i] DEBUG_CTX2 );
[0c78741]537 }
538}
539
540static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
541 for( int i = 0; i < count; i++ ) {
542 spinlock * l = &source[i]->lock;
[b227f68]543 lock_yield( l DEBUG_CTX2 );
[0c78741]544 if(locks) locks[i] = l;
545 }
546}
547
548static inline void unlock_all( spinlock ** locks, unsigned short count ) {
549 for( int i = 0; i < count; i++ ) {
550 unlock( locks[i] );
551 }
552}
553
554static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
555 for( int i = 0; i < count; i++ ) {
556 unlock( &locks[i]->lock );
557 }
558}
559
560
561static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
562 for( int i = 0; i < count; i++ ) {
563 recursions[i] = ctx[i]->recursion;
564 }
565}
566
567static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
568 for( int i = 0; i < count; i++ ) {
569 ctx[i]->recursion = recursions[i];
570 }
571}
572
573// Function has 2 different behavior
574// 1 - Marks a monitors as being ready to run
575// 2 - Checks if all the monitors are ready to run
576// if so return the thread to run
577static inline thread_desc * check_condition( __condition_criterion_t * target ) {
578 __condition_node_t * node = target->owner;
579 unsigned short count = node->count;
580 __condition_criterion_t * criteria = node->criteria;
581
582 bool ready2run = true;
583
584 for( int i = 0; i < count; i++ ) {
[44264c5]585
[b227f68]586 // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
[0c78741]587 if( &criteria[i] == target ) {
588 criteria[i].ready = true;
[b227f68]589 // LIB_DEBUG_PRINT_SAFE( "True\n" );
[0c78741]590 }
591
592 ready2run = criteria[i].ready && ready2run;
593 }
594
[b227f68]595 // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
[0c78741]596 return ready2run ? node->waiting_thread : NULL;
597}
598
599static inline void brand_condition( condition * this ) {
[1c273d0]600 thread_desc * thrd = this_thread;
[0c78741]601 if( !this->monitors ) {
[b227f68]602 // LIB_DEBUG_PRINT_SAFE("Branding\n");
[b18830e]603 assertf( thrd->monitors.list != NULL, "No current monitor to brand condition %p", thrd->monitors.list );
604 this->monitor_count = thrd->monitors.size;
[a933dcf4]605
606 this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
607 for( int i = 0; i < this->monitor_count; i++ ) {
[b18830e]608 this->monitors[i] = thrd->monitors.list[i];
[a933dcf4]609 }
[0c78741]610 }
611}
612
613static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
[ad1a8dd]614 if( !val ) return end;
615
616 for(int i = 0; i <= end; i++) {
[0c78741]617 if( thrds[i] == val ) return end;
618 }
619
620 thrds[end] = val;
621 return end + 1;
622}
623
[6ae8c92]624static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & group ) {
625 __acceptable_t * it = this->mask.clauses; // Optim
626 int count = this->mask.size;
[b18830e]627
628 // Check if there are any acceptable functions
[6ae8c92]629 if( !it ) return -1;
[6b224a52]630
[b18830e]631 // If this isn't the first monitor to test this, there is no reason to repeat the test.
[6ae8c92]632 if( this != group[0] ) return group[0]->mask.accepted >= 0;
[90c4df0]633
[b18830e]634 // For all acceptable functions check if this is the current function.
[6ae8c92]635 for( short i = 0; i < count; i++, it++ ) {
636 if( it->monitors == group ) {
637 *this->mask.accepted = i;
638 return true;
639 }
[b18830e]640 }
641
642 // No function matched
[6ae8c92]643 return false;
[90c4df0]644}
645
[6ae8c92]646static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc ** monitors, int count ) {
[90c4df0]647
648 __thread_queue_t * entry_queue = &monitors[0]->entry_queue;
649
650 // For each thread in the entry-queue
651 for( thread_desc ** thrd_it = &entry_queue->head;
652 *thrd_it;
653 thrd_it = &(*thrd_it)->next)
654 {
655 // For each acceptable check if it matches
[b18830e]656 int i;
[6ae8c92]657 __acceptable_t * end = mask.clauses + mask.size;
658 for( __acceptable_t * it = mask.clauses; it != end; it++, i++ ) {
[90c4df0]659 // Check if we have a match
[6ae8c92]660 if( it->monitors == (*thrd_it)->monitors ) {
[90c4df0]661
662 // If we have a match return it
663 // after removeing it from the entry queue
[b18830e]664 return [remove( entry_queue, thrd_it ), i];
[90c4df0]665 }
666 }
667 }
668
[b18830e]669 return [0, -1];
670}
671
[6ae8c92]672static inline short count_max( const __waitfor_mask_t & mask ) {
[b18830e]673 short max = 0;
[6ae8c92]674 for( int i = 0; i < mask.size; i++ ) {
675 max += mask.clauses[i].monitors.size;
[b18830e]676 }
677 return max;
[97e3296]678}
[b18830e]679
[6ae8c92]680static inline short aggregate( monitor_desc ** storage, const __waitfor_mask_t & mask ) {
[b18830e]681 #warning function not implemented
682 return 0;
683}
684
[6ae8c92]685static inline void set_mask( monitor_desc ** storage, short count, const __waitfor_mask_t & mask ) {
[b18830e]686 for(int i = 0; i < count; i++) {
[6ae8c92]687 storage[i]->mask = mask;
[b18830e]688 }
689}
690
691
[242a902]692void ?{}( __condition_blocked_queue_t & this ) {
693 this.head = NULL;
694 this.tail = &this.head;
[0c78741]695}
696
697void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
[4aa2fb2]698 verify(this->tail != NULL);
[0c78741]699 *this->tail = c;
700 this->tail = &c->next;
701}
702
703__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
704 __condition_node_t * head = this->head;
705 if( head ) {
706 this->head = head->next;
707 if( !head->next ) {
708 this->tail = &this->head;
709 }
710 head->next = NULL;
711 }
712 return head;
[4aa2fb2]713}
[6b0b624]714
715// Local Variables: //
716// mode: c //
717// tab-width: 4 //
718// End: //
Note: See TracBrowser for help on using the repository browser.