source: src/libcfa/concurrency/monitor.c @ 19c43b7

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 19c43b7 was 19c43b7, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added test to check correct statment is executed after waitfor

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