source: src/libcfa/concurrency/monitor.c @ 90c4df0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 90c4df0 was 90c4df0, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Implemented search for external scheduling

  • 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 );
[97e3296]27static inline int is_accepted( thread_desc * owner, monitor_desc * this, monitor_desc ** group, int group_cnt, void (*func)() );
[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
[97e3296]44static inline thread_desc * search_entry_queue( __acceptable_t * acceptables, int acc_count, monitor_desc ** monitors, int count );
45
46//-----------------------------------------------------------------------------
47// Useful defines
48#define wait_ctx(thrd, user_info)                               /* Create the necessary information to use the signaller stack       */ \
49        __condition_node_t waiter = { thrd, count, user_info };   /* Create the node specific to this wait operation                   */ \
50        __condition_criterion_t criteria[count];                  /* Create the creteria this wait operation needs to wake up          */ \
51        init( count, monitors, &waiter, criteria );               /* Link everything together                                          */ \
52
53#define wait_ctx_primed(thrd, user_info)                        /* Create the necessary information to use the signaller stack       */ \
54        __condition_node_t waiter = { thrd, count, user_info };   /* Create the node specific to this wait operation                   */ \
55        __condition_criterion_t criteria[count];                  /* Create the creteria this wait operation needs to wake up          */ \
56        init_push( count, monitors, &waiter, criteria );          /* Link everything together and push it to the AS-Stack              */ \
57
58#define monitor_ctx( mons, cnt )              /* Define that create the necessary struct for internal/external scheduling operations */ \
59        monitor_desc ** monitors = mons;        /* Save the targeted monitors                                                          */ \
60        unsigned short count = cnt;             /* Save the count to a local variable                                                  */ \
61        unsigned int recursions[ count ];       /* Save the current recursion levels to restore them later                             */ \
62        spinlock *   locks     [ count ];       /* We need to pass-in an array of locks to BlockInternal                               */ \
63
[0c78741]64//-----------------------------------------------------------------------------
65// Enter/Leave routines
[690f13c]66
67
[cb0e6de]68extern "C" {
[97e3296]69        // Enter single monitor
70        static void __enter_monitor_desc( monitor_desc * this, monitor_desc ** group, int group_cnt, void (*func)() ) {
71                // Lock the monitor spinlock, lock_yield to reduce contention
[b227f68]72                lock_yield( &this->lock DEBUG_CTX2 );
[1c273d0]73                thread_desc * thrd = this_thread;
[f07e037]74
[90c4df0]75                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
76
[97e3296]77                this->accepted_index = -1;
[cb0e6de]78                if( !this->owner ) {
[97e3296]79                        // No one has the monitor, just take it
[cd348e7]80                        set_owner( this, thrd );
[90c4df0]81
82                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon is free \n");
[cb0e6de]83                }
84                else if( this->owner == thrd) {
[97e3296]85                        // We already have the monitor, just not how many times we took it
[4aa2fb2]86                        verify( this->recursion > 0 );
[cb0e6de]87                        this->recursion += 1;
[90c4df0]88
89                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon already owned \n");
[cb0e6de]90                }
[97e3296]91                else if( (this->accepted_index = is_accepted( thrd, this, group, group_cnt, func)) >= 0 ) {
92                        // Some one was waiting for us, enter
93                        set_owner( this, thrd );
[90c4df0]94
95                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon accepts \n");
[97e3296]96                }
[cb0e6de]97                else {
[90c4df0]98                        LIB_DEBUG_PRINT_SAFE("Kernel :  blocking \n");
99
[97e3296]100                        // Some one else has the monitor, wait in line for it
[cb0e6de]101                        append( &this->entry_queue, thrd );
[82ff5845]102                        BlockInternal( &this->lock );
[cc7f4b1]103
[90c4df0]104                        LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
105
[97e3296]106                        // BlockInternal will unlock spinlock, no need to unlock ourselves
[2ac095d]107                        return;
[cb0e6de]108                }
[f07e037]109
[90c4df0]110                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
111
[97e3296]112                // Release the lock and leave
[cb0e6de]113                unlock( &this->lock );
[5ea06d6]114                return;
[cb0e6de]115        }
[f07e037]116
[97e3296]117        // Leave single monitor
[1c273d0]118        void __leave_monitor_desc( monitor_desc * this ) {
[97e3296]119                // Lock the monitor spinlock, lock_yield to reduce contention
[b227f68]120                lock_yield( &this->lock DEBUG_CTX2 );
[f07e037]121
[1c273d0]122                verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
[cc7f4b1]123
[97e3296]124                // Leaving a recursion level, decrement the counter
[cb0e6de]125                this->recursion -= 1;
[f07e037]126
[97e3296]127                // If we haven't left the last level of recursion
128                // it means we don't need to do anything
[690f13c]129                if( this->recursion != 0) {
130                        unlock( &this->lock );
131                        return;
132                }
[f07e037]133
[97e3296]134                // Get the next thread, will be null on low contention monitor
[0c78741]135                thread_desc * new_owner = next_thread( this );
[5ea06d6]136
[97e3296]137                // We can now let other threads in safely
[cb0e6de]138                unlock( &this->lock );
[51f3798]139
[690f13c]140                //We need to wake-up the thread
[1c273d0]141                WakeThread( new_owner );
142        }
143
[97e3296]144        // Leave the thread monitor
145        // last routine called by a thread.
146        // Should never return
[1c273d0]147        void __leave_thread_monitor( thread_desc * thrd ) {
148                monitor_desc * this = &thrd->mon;
[97e3296]149
150                // Lock the monitor now
[b227f68]151                lock_yield( &this->lock DEBUG_CTX2 );
[1c273d0]152
153                disable_interrupts();
154
155                thrd->cor.state = Halted;
156
157                verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
158
[97e3296]159                // Leaving a recursion level, decrement the counter
[1c273d0]160                this->recursion -= 1;
161
[97e3296]162                // If we haven't left the last level of recursion
163                // it must mean there is an error
164                if( this->recursion != 0) { abortf("Thread internal monitor has unbalanced recursion"); }
[1c273d0]165
[97e3296]166                // Fetch the next thread, can be null
[1c273d0]167                thread_desc * new_owner = next_thread( this );
168
[97e3296]169                // Leave the thread, this will unlock the spinlock
170                // Use leave thread instead of BlockInternal which is
171                // specialized for this case and supports null new_owner
[f2b12406]172                LeaveThread( &this->lock, new_owner );
[97e3296]173
174                // Control flow should never reach here!
[cc7f4b1]175        }
[2781e65]176}
177
[97e3296]178// Enter multiple monitor
179// relies on the monitor array being sorted
180static inline void enter(monitor_desc ** monitors, int count, void (*func)() ) {
[0c78741]181        for(int i = 0; i < count; i++) {
[97e3296]182                __enter_monitor_desc( monitors[i], monitors, count, func );
183        }
184
185        int acc_idx = monitors[0]->accepted_index;
186        if( acc_idx >= 0 && monitors[0]->acceptables[ acc_idx ].run_preaccept ) {
187                assert( monitors[0]->pre_accept );
188                monitors[0]->pre_accept();
[2781e65]189        }
190}
191
[97e3296]192// Leave multiple monitor
193// relies on the monitor array being sorted
[5ea06d6]194static inline void leave(monitor_desc ** monitors, int count) {
[0c78741]195        for(int i = count - 1; i >= 0; i--) {
196                __leave_monitor_desc( monitors[i] );
[2781e65]197        }
[5ea06d6]198}
199
[97e3296]200// Ctor for monitor guard
201// Sorts monitors before entering
202void ?{}( monitor_guard_t * this, monitor_desc ** m, int count, void (*func)() ) {
203        // Store current array
[5ea06d6]204        this->m = m;
205        this->count = count;
[97e3296]206
207        // Sort monitors based on address -> TODO use a sort specialized for small numbers
[5ea06d6]208        qsort(this->m, count);
209
[97e3296]210        // Save previous thread context
[1c273d0]211        this->prev_mntrs = this_thread->current_monitors;
212        this->prev_count = this_thread->current_monitor_count;
[90c4df0]213        this->prev_func  = this_thread->current_monitor_func;
[5ea06d6]214
[97e3296]215        // Update thread context (needed for conditions)
[1c273d0]216        this_thread->current_monitors      = m;
217        this_thread->current_monitor_count = count;
[90c4df0]218        this_thread->current_monitor_func  = func;
219
220        // Enter the monitors in order
221        enter( this->m, this->count, func );
[5ea06d6]222}
223
[97e3296]224// Dtor for monitor guard
[5ea06d6]225void ^?{}( monitor_guard_t * this ) {
[97e3296]226        // Leave the monitors in order
[5ea06d6]227        leave( this->m, this->count );
228
[97e3296]229        // Restore thread context
[1c273d0]230        this_thread->current_monitors      = this->prev_mntrs;
231        this_thread->current_monitor_count = this->prev_count;
[90c4df0]232        this_thread->current_monitor_func  = this->prev_func;
[5ea06d6]233}
234
[97e3296]235//-----------------------------------------------------------------------------
236// Internal scheduling types
237
[be3d020]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;
243}
244
245void ?{}(__condition_criterion_t * this ) {
246        this->ready  = false;
247        this->target = NULL;
248        this->owner  = NULL;
249        this->next   = NULL;
250}
251
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;
[0c78741]323                if ( this->monitor_count != this_thrd->current_monitor_count ) {
324                        abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count );
[97e3296]325                }
[0c78741]326
327                for(int i = 0; i < this->monitor_count; i++) {
328                        if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
329                                abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[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//-----------------------------------------------------------------------------
405// Internal scheduling
[97e3296]406int __accept_internal( unsigned short acc_count, __acceptable_t * acceptables ) {
407        thread_desc * thrd = this_thread;
408
409        // Create storage for monitor context
410        monitor_ctx( acceptables->monitors, acceptables->count );
[c81ebf9]411
[97e3296]412        // Lock all monitors (aggregates the lock them as well)
413        lock_all( monitors, locks, count );
[c81ebf9]414
[97e3296]415        // Create the node specific to this wait operation
416        wait_ctx_primed( thrd, 0 );
[c81ebf9]417
[97e3296]418        // Check if the entry queue
419        thread_desc * next = search_entry_queue( acceptables, acc_count, monitors, count );
[c81ebf9]420
[90c4df0]421        LIB_DEBUG_PRINT_SAFE("Owner(s) :");
422        for(int i = 0; i < count; i++) {
423                LIB_DEBUG_PRINT_SAFE(" %p", monitors[i]->owner );
424        }
425        LIB_DEBUG_PRINT_SAFE("\n");
426
427        LIB_DEBUG_PRINT_SAFE("Passing mon to %p\n", next);
428
[97e3296]429        if( !next ) {
430                // Update acceptables on the current monitors
431                for(int i = 0; i < count; i++) {
432                        monitors[i]->acceptables = acceptables;
433                        monitors[i]->acceptable_count = acc_count;
434                }
435        }
[90c4df0]436        else {
437                for(int i = 0; i < count; i++) {
438                        set_owner( monitors[i], next );
439                }
440        }
441
[c81ebf9]442
[97e3296]443        save_recursion( monitors, recursions, count );
[c81ebf9]444
[90c4df0]445
[97e3296]446        // Everything is ready to go to sleep
447        BlockInternal( locks, count, &next, next ? 1 : 0 );
[c81ebf9]448
449
[97e3296]450        //WE WOKE UP
[c81ebf9]451
452
[97e3296]453        //We are back, restore the owners and recursions
454        lock_all( locks, count );
455        restore_recursion( monitors, recursions, count );
456        int acc_idx = monitors[0]->accepted_index;
457        unlock_all( locks, count );
[c81ebf9]458
[97e3296]459        return acc_idx;
[c81ebf9]460}
461
[0c78741]462//-----------------------------------------------------------------------------
463// Utilities
464
465static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
466        //Pass the monitor appropriately
467        this->owner = owner;
468
469        //We are passing the monitor to someone else, which means recursion level is not 0
470        this->recursion = owner ? 1 : 0;
471}
472
473static inline thread_desc * next_thread( monitor_desc * this ) {
474        //Check the signaller stack
475        __condition_criterion_t * urgent = pop( &this->signal_stack );
476        if( urgent ) {
477                //The signaller stack is not empty,
478                //regardless of if we are ready to baton pass,
479                //we need to set the monitor as in use
480                set_owner( this,  urgent->owner->waiting_thread );
481
482                return check_condition( urgent );
483        }
484
485        // No signaller thread
486        // Get the next thread in the entry_queue
487        thread_desc * new_owner = pop_head( &this->entry_queue );
488        set_owner( this, new_owner );
489
490        return new_owner;
491}
492
[97e3296]493static inline int is_accepted( thread_desc * owner, monitor_desc * this, monitor_desc ** group, int group_cnt, void (*func)() ) {
494        __acceptable_t* accs = this->acceptables; // Optim
495        int acc_cnt = this->acceptable_count;
496
497        // Check if there are any acceptable functions
498        if( !accs ) return -1;
499
500        // If this isn't the first monitor to test this, there is no reason to repeat the test.
501        if( this != group[0] ) return group[0]->accepted_index;
502
503        // For all acceptable functions check if this is the current function.
504        OUT_LOOP:
505        for( int i = 0; i < acc_cnt; i++ ) {
506                __acceptable_t * acc = &accs[i];
507
508                // if function matches, check the monitors
509                if( acc->func == func ) {
510
511                        // If the group count is different then it can't be a match
512                        if( acc->count != group_cnt ) return -1;
513
514                        // Check that all the monitors match
515                        for( int j = 0; j < group_cnt; j++ ) {
516                                // If not a match, check next function
517                                if( acc->monitors[j] != group[j] ) continue OUT_LOOP;
518                        }
519
520                        // It's a complete match, accept the call
521                        return i;
522                }
523        }
524
525        // No function matched
526        return -1;
527}
528
529static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
530        for(int i = 0; i < count; i++) {
531                (&criteria[i]){ monitors[i], waiter };
532        }
533
534        waiter->criteria = criteria;
535}
536
537static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
538        for(int i = 0; i < count; i++) {
539                (&criteria[i]){ monitors[i], waiter };
540                push( &criteria[i].target->signal_stack, &criteria[i] );
541        }
542
543        waiter->criteria = criteria;
544}
545
[0c78741]546static inline void lock_all( spinlock ** locks, unsigned short count ) {
547        for( int i = 0; i < count; i++ ) {
[b227f68]548                lock_yield( locks[i] DEBUG_CTX2 );
[0c78741]549        }
550}
551
552static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
553        for( int i = 0; i < count; i++ ) {
554                spinlock * l = &source[i]->lock;
[b227f68]555                lock_yield( l DEBUG_CTX2 );
[0c78741]556                if(locks) locks[i] = l;
557        }
558}
559
560static inline void unlock_all( spinlock ** locks, unsigned short count ) {
561        for( int i = 0; i < count; i++ ) {
562                unlock( locks[i] );
563        }
564}
565
566static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
567        for( int i = 0; i < count; i++ ) {
568                unlock( &locks[i]->lock );
569        }
570}
571
572
573static inline void save_recursion   ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
574        for( int i = 0; i < count; i++ ) {
575                recursions[i] = ctx[i]->recursion;
576        }
577}
578
579static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
580        for( int i = 0; i < count; i++ ) {
581                ctx[i]->recursion = recursions[i];
582        }
583}
584
585// Function has 2 different behavior
586// 1 - Marks a monitors as being ready to run
587// 2 - Checks if all the monitors are ready to run
588//     if so return the thread to run
589static inline thread_desc * check_condition( __condition_criterion_t * target ) {
590        __condition_node_t * node = target->owner;
591        unsigned short count = node->count;
592        __condition_criterion_t * criteria = node->criteria;
593
594        bool ready2run = true;
595
596        for(    int i = 0; i < count; i++ ) {
[44264c5]597
[b227f68]598                // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
[0c78741]599                if( &criteria[i] == target ) {
600                        criteria[i].ready = true;
[b227f68]601                        // LIB_DEBUG_PRINT_SAFE( "True\n" );
[0c78741]602                }
603
604                ready2run = criteria[i].ready && ready2run;
605        }
606
[b227f68]607        // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
[0c78741]608        return ready2run ? node->waiting_thread : NULL;
609}
610
611static inline void brand_condition( condition * this ) {
[1c273d0]612        thread_desc * thrd = this_thread;
[0c78741]613        if( !this->monitors ) {
[b227f68]614                // LIB_DEBUG_PRINT_SAFE("Branding\n");
[38ef0de]615                assertf( thrd->current_monitors != NULL, "No current monitor to brand condition %p", thrd->current_monitors );
[0c78741]616                this->monitor_count = thrd->current_monitor_count;
[a933dcf4]617
618                this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
619                for( int i = 0; i < this->monitor_count; i++ ) {
620                        this->monitors[i] = thrd->current_monitors[i];
621                }
[0c78741]622        }
623}
624
625static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
[ad1a8dd]626        if( !val ) return end;
627
628        for(int i = 0; i <= end; i++) {
[0c78741]629                if( thrds[i] == val ) return end;
630        }
631
632        thrds[end] = val;
633        return end + 1;
634}
635
[90c4df0]636static inline bool match( __acceptable_t * acc, thread_desc * thrd ) {
637        verify( thrd );
638        verify( acc );
639        if( acc->func != thrd->current_monitor_func ) return false;
640
641        return true;
642}
643
[97e3296]644static inline thread_desc * search_entry_queue( __acceptable_t * acceptables, int acc_count, 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
654                __acceptable_t * acc_end = acceptables + acc_count;
655                for( __acceptable_t * acc_it = acceptables; acc_it != acc_end; acc_it++ ) {
656                        // Check if we have a match
657                        if( match( acc_it, *thrd_it ) ) {
658
659                                // If we have a match return it
660                                // after removeing it from the entry queue
661                                return remove( entry_queue, thrd_it );
662                        }
663                }
664        }
665
[97e3296]666        return NULL;
667}
668
[0c78741]669void ?{}( __condition_blocked_queue_t * this ) {
670        this->head = NULL;
671        this->tail = &this->head;
672}
673
674void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
[4aa2fb2]675        verify(this->tail != NULL);
[0c78741]676        *this->tail = c;
677        this->tail = &c->next;
678}
679
680__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
681        __condition_node_t * head = this->head;
682        if( head ) {
683                this->head = head->next;
684                if( !head->next ) {
685                        this->tail = &this->head;
686                }
687                head->next = NULL;
688        }
689        return head;
[4aa2fb2]690}
[6b0b624]691
692// Local Variables: //
693// mode: c //
694// tab-width: 4 //
695// End: //
Note: See TracBrowser for help on using the repository browser.