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

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 a843067 was a843067, checked in by Thierry Delisle <tdelisle@…>, 5 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
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#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
79
80//-----------------------------------------------------------------------------
81// Enter/Leave routines
82
83
84extern "C" {
85        // Enter single monitor
86        static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
87                // Lock the monitor spinlock, lock_yield to reduce contention
88                lock_yield( &this->lock DEBUG_CTX2 );
89                thread_desc * thrd = this_thread;
90
91                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
92
93                if( !this->owner ) {
94                        // No one has the monitor, just take it
95                        set_owner( this, thrd );
96
97                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon is free \n");
98                }
99                else if( this->owner == thrd) {
100                        // We already have the monitor, just not how many times we took it
101                        verify( this->recursion > 0 );
102                        this->recursion += 1;
103
104                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon already owned \n");
105                }
106                else if( is_accepted( this, group) ) {
107                        // Some one was waiting for us, enter
108                        set_owner( this, thrd );
109
110                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon accepts \n");
111                }
112                else {
113                        LIB_DEBUG_PRINT_SAFE("Kernel :  blocking \n");
114
115                        // Some one else has the monitor, wait in line for it
116                        append( &this->entry_queue, thrd );
117                        BlockInternal( &this->lock );
118
119                        LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
120
121                        // BlockInternal will unlock spinlock, no need to unlock ourselves
122                        return;
123                }
124
125                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
126
127                // Release the lock and leave
128                unlock( &this->lock );
129                return;
130        }
131
132        // Leave single monitor
133        void __leave_monitor_desc( monitor_desc * this ) {
134                // Lock the monitor spinlock, lock_yield to reduce contention
135                lock_yield( &this->lock DEBUG_CTX2 );
136
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 );
140
141                // Leaving a recursion level, decrement the counter
142                this->recursion -= 1;
143
144                // If we haven't left the last level of recursion
145                // it means we don't need to do anything
146                if( this->recursion != 0) {
147                        unlock( &this->lock );
148                        return;
149                }
150
151                // Get the next thread, will be null on low contention monitor
152                thread_desc * new_owner = next_thread( this );
153
154                // We can now let other threads in safely
155                unlock( &this->lock );
156
157                //We need to wake-up the thread
158                WakeThread( new_owner );
159        }
160
161        // Leave the thread monitor
162        // last routine called by a thread.
163        // Should never return
164        void __leave_thread_monitor( thread_desc * thrd ) {
165                monitor_desc * this = &thrd->self_mon;
166
167                // Lock the monitor now
168                lock_yield( &this->lock DEBUG_CTX2 );
169
170                disable_interrupts();
171
172                thrd->self_cor.state = Halted;
173
174                verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
175
176                // Leaving a recursion level, decrement the counter
177                this->recursion -= 1;
178
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"); }
182
183                // Fetch the next thread, can be null
184                thread_desc * new_owner = next_thread( this );
185
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
189                LeaveThread( &this->lock, new_owner );
190
191                // Control flow should never reach here!
192        }
193}
194
195// Enter multiple monitor
196// relies on the monitor array being sorted
197static inline void enter( __monitor_group_t monitors ) {
198        for(int i = 0; i < monitors.size; i++) {
199                __enter_monitor_desc( monitors.list[i], monitors );
200        }
201}
202
203// Leave multiple monitor
204// relies on the monitor array being sorted
205static inline void leave(monitor_desc ** monitors, int count) {
206        for(int i = count - 1; i >= 0; i--) {
207                __leave_monitor_desc( monitors[i] );
208        }
209}
210
211// Ctor for monitor guard
212// Sorts monitors before entering
213void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() ) {
214        // Store current array
215        this.m = m;
216        this.count = count;
217
218        // Sort monitors based on address -> TODO use a sort specialized for small numbers
219        qsort(this.m, count);
220
221        // Save previous thread context
222        this.prev_mntrs = this_thread->monitors.list;
223        this.prev_count = this_thread->monitors.size;
224        this.prev_func  = this_thread->monitors.func;
225
226        // Update thread context (needed for conditions)
227        this_thread->monitors.list = m;
228        this_thread->monitors.size = count;
229        this_thread->monitors.func = func;
230
231        LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count);
232
233        // Enter the monitors in order
234        __monitor_group_t group = {this.m, this.count, func};
235        enter( group );
236
237        LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n");
238}
239
240
241// Dtor for monitor guard
242void ^?{}( monitor_guard_t & this ) {
243        LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count);
244
245        // Leave the monitors in order
246        leave( this.m, this.count );
247
248        LIB_DEBUG_PRINT_SAFE("MGUARD : left\n");
249
250        // Restore thread context
251        this_thread->monitors.list = this.prev_mntrs;
252        this_thread->monitors.size = this.prev_count;
253        this_thread->monitors.func = this.prev_func;
254}
255
256//-----------------------------------------------------------------------------
257// Internal scheduling types
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;
263}
264
265void ?{}(__condition_criterion_t & this ) {
266        this.ready  = false;
267        this.target = NULL;
268        this.owner  = NULL;
269        this.next   = NULL;
270}
271
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;
277}
278
279//-----------------------------------------------------------------------------
280// Internal scheduling
281void wait( condition * this, uintptr_t user_info = 0 ) {
282        brand_condition( this );
283
284        // Check that everything is as expected
285        assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
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 );
288
289        // Create storage for monitor context
290        monitor_ctx( this->monitors, this->monitor_count );
291
292        // Create the node specific to this wait operation
293        wait_ctx( this_thread, user_info );
294
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 );
298
299        // Lock all monitors (aggregates the locks as well)
300        lock_all( monitors, locks, count );
301
302        // Find the next thread(s) to run
303        short thread_count = 0;
304        thread_desc * threads[ count ];
305        for(int i = 0; i < count; i++) {
306                threads[i] = 0;
307        }
308
309        // Save monitor states
310        monitor_save;
311
312        // Remove any duplicate threads
313        for( int i = 0; i < count; i++) {
314                thread_desc * new_owner = next_thread( monitors[i] );
315                insert_unique( threads, thread_count, new_owner );
316        }
317
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;
323}
324
325bool signal( condition * this ) {
326        if( is_empty( this ) ) { return false; }
327
328        //Check that everything is as expected
329        verify( this->monitors );
330        verify( this->monitor_count != 0 );
331
332        //Some more checking in debug
333        LIB_DEBUG_DO(
334                thread_desc * this_thrd = this_thread;
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 );
337                }
338
339                for(int i = 0; i < this->monitor_count; i++) {
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] );
342                        }
343                }
344        );
345
346        unsigned short count = this->monitor_count;
347
348        // Lock all monitors
349        lock_all( this->monitors, NULL, count );
350
351        //Pop the head of the waiting queue
352        __condition_node_t * node = pop_head( &this->blocked );
353
354        //Add the thread to the proper AS stack
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 );
359        }
360
361        //Release
362        unlock_all( this->monitors, count );
363
364        return true;
365}
366
367bool signal_block( condition * this ) {
368        if( !this->blocked.head ) { return false; }
369
370        //Check that everything is as expected
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 );
373
374        // Create storage for monitor context
375        monitor_ctx( this->monitors, this->monitor_count );
376
377        // Lock all monitors (aggregates the locks them as well)
378        lock_all( monitors, locks, count );
379
380        // Create the node specific to this wait operation
381        wait_ctx_primed( this_thread, 0 )
382
383        //save contexts
384        monitor_save;
385
386        //Find the thread to run
387        thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
388        set_owner( monitors, count, signallee );
389
390        //Everything is ready to go to sleep
391        BlockInternal( locks, count, &signallee, 1 );
392
393
394        // WE WOKE UP
395
396
397        //We are back, restore the masks and recursions
398        monitor_restore;
399
400        return true;
401}
402
403// Access the user_info of the thread waiting at the front of the queue
404uintptr_t front( condition * this ) {
405        verifyf( !is_empty(this),
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."
408        );
409        return this->blocked.head->user_info;
410}
411
412//-----------------------------------------------------------------------------
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
426void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
427        // This statment doesn't have a contiguous list of monitors...
428        // Create one!
429        short max = count_max( mask );
430        monitor_desc * mon_storage[max];
431        short actual_count = aggregate( mon_storage, mask );
432
433        // Create storage for monitor context
434        monitor_ctx( mon_storage, actual_count );
435
436        // Lock all monitors (aggregates the locks as well)
437        lock_all( monitors, locks, count );
438
439        {
440                // Check if the entry queue
441                thread_desc * next; int index;
442                [next, index] = search_entry_queue( mask, monitors, count );
443
444                if( next ) {
445                        if( mask.clauses[index].is_dtor ) {
446                                #warning case not implemented
447                        }
448                        else {
449                                blockAndWake( &next, 1 );
450                        }
451
452                        return index;
453                }
454        }
455
456
457        if( duration == 0 ) return -1;
458
459
460        verifyf( duration < 0, "Timeout on waitfor statments not supported yet.");
461
462
463        monitor_save;
464        set_mask( monitors, count, mask );
465
466        BlockInternal( locks, count );       // Everything is ready to go to sleep
467        //WE WOKE UP
468        monitor_restore;                     //We are back, restore the masks and recursions
469}
470
471//-----------------------------------------------------------------------------
472// Utilities
473
474static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
475        LIB_DEBUG_PRINT_SAFE("Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
476
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
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
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
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
521        if( !it ) return false;
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
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++) {
540                (criteria[i]){ monitors[i], waiter };
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++) {
548                (criteria[i]){ monitors[i], waiter };
549                push( &criteria[i].target->signal_stack, &criteria[i] );
550        }
551
552        waiter->criteria = criteria;
553}
554
555static inline void lock_all( spinlock ** locks, unsigned short count ) {
556        for( int i = 0; i < count; i++ ) {
557                lock_yield( locks[i] DEBUG_CTX2 );
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;
564                lock_yield( l DEBUG_CTX2 );
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
581static inline void save   ( monitor_desc ** ctx, short count, __attribute((unused)) spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
582        for( int i = 0; i < count; i++ ) {
583                recursions[i] = ctx[i]->recursion;
584                masks[i]      = ctx[i]->mask;
585        }
586}
587
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 );
590        for( int i = 0; i < count; i++ ) {
591                ctx[i]->recursion = recursions[i];
592                ctx[i]->mask      = masks[i];
593        }
594        unlock_all( locks, count );
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++ ) {
609
610                // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
611                if( &criteria[i] == target ) {
612                        criteria[i].ready = true;
613                        // LIB_DEBUG_PRINT_SAFE( "True\n" );
614                }
615
616                ready2run = criteria[i].ready && ready2run;
617        }
618
619        // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
620        return ready2run ? node->waiting_thread : NULL;
621}
622
623static inline void brand_condition( condition * this ) {
624        thread_desc * thrd = this_thread;
625        if( !this->monitors ) {
626                // LIB_DEBUG_PRINT_SAFE("Branding\n");
627                assertf( thrd->monitors.list != NULL, "No current monitor to brand condition %p", thrd->monitors.list );
628                this->monitor_count = thrd->monitors.size;
629
630                this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
631                for( int i = 0; i < this->monitor_count; i++ ) {
632                        this->monitors[i] = thrd->monitors.list[i];
633                }
634        }
635}
636
637static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc ** monitors, int count ) {
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
647                int i;
648                __acceptable_t * end = mask.clauses + mask.size;
649                for( __acceptable_t * it = mask.clauses; it != end; it++, i++ ) {
650                        // Check if we have a match
651                        if( *it == (*thrd_it)->monitors ) {
652
653                                // If we have a match return it
654                                // after removeing it from the entry queue
655                                return [remove( entry_queue, thrd_it ), i];
656                        }
657                }
658        }
659
660        return [0, -1];
661}
662
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
676static inline short count_max( const __waitfor_mask_t & mask ) {
677        short max = 0;
678        for( int i = 0; i < mask.size; i++ ) {
679                max += mask.clauses[i].size;
680        }
681        return max;
682}
683
684static inline short aggregate( monitor_desc ** storage, const __waitfor_mask_t & mask ) {
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                }
690        }
691        qsort( storage, size );
692        return size;
693}
694
695void ?{}( __condition_blocked_queue_t & this ) {
696        this.head = NULL;
697        this.tail = &this.head;
698}
699
700void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
701        verify(this->tail != NULL);
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;
716}
717
718// Local Variables: //
719// mode: c //
720// tab-width: 4 //
721// End: //
Note: See TracBrowser for help on using the repository browser.