Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/monitor.c

    rc2b9f21 r0cf5b79  
    1919#include <inttypes.h>
    2020
     21#include "libhdr.h"
    2122#include "kernel_private.h"
    2223
     
    9091        static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
    9192                // Lock the monitor spinlock
    92                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     93                DO_LOCK( this->lock DEBUG_CTX2 );
    9394                thread_desc * thrd = this_thread;
    9495
    95                 __cfaabi_dbg_print_safe("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
     96                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
    9697
    9798                if( !this->owner ) {
     
    99100                        set_owner( this, thrd );
    100101
    101                         __cfaabi_dbg_print_safe("Kernel :  mon is free \n");
     102                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon is free \n");
    102103                }
    103104                else if( this->owner == thrd) {
     
    105106                        this->recursion += 1;
    106107
    107                         __cfaabi_dbg_print_safe("Kernel :  mon already owned \n");
     108                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon already owned \n");
    108109                }
    109110                else if( is_accepted( this, group) ) {
     
    114115                        reset_mask( this );
    115116
    116                         __cfaabi_dbg_print_safe("Kernel :  mon accepts \n");
     117                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon accepts \n");
    117118                }
    118119                else {
    119                         __cfaabi_dbg_print_safe("Kernel :  blocking \n");
     120                        LIB_DEBUG_PRINT_SAFE("Kernel :  blocking \n");
    120121
    121122                        // Some one else has the monitor, wait in line for it
     
    123124                        BlockInternal( &this->lock );
    124125
    125                         __cfaabi_dbg_print_safe("Kernel : %10p Entered  mon %p\n", thrd, this);
     126                        LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
    126127
    127128                        // BlockInternal will unlock spinlock, no need to unlock ourselves
     
    129130                }
    130131
    131                 __cfaabi_dbg_print_safe("Kernel : %10p Entered  mon %p\n", thrd, this);
     132                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
    132133
    133134                // Release the lock and leave
     
    138139        static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) {
    139140                // Lock the monitor spinlock
    140                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     141                DO_LOCK( this->lock DEBUG_CTX2 );
    141142                thread_desc * thrd = this_thread;
    142143
    143                 __cfaabi_dbg_print_safe("Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);
     144                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);
    144145
    145146
    146147                if( !this->owner ) {
    147                         __cfaabi_dbg_print_safe("Kernel : Destroying free mon %p\n", this);
     148                        LIB_DEBUG_PRINT_SAFE("Kernel : Destroying free mon %p\n", this);
    148149
    149150                        // No one has the monitor, just take it
     
    163164                __monitor_group_t group = { &this, 1, func };
    164165                if( is_accepted( this, group) ) {
    165                         __cfaabi_dbg_print_safe("Kernel :  mon accepts dtor, block and signal it \n");
     166                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon accepts dtor, block and signal it \n");
    166167
    167168                        // Wake the thread that is waiting for this
     
    182183                }
    183184                else {
    184                         __cfaabi_dbg_print_safe("Kernel :  blocking \n");
     185                        LIB_DEBUG_PRINT_SAFE("Kernel :  blocking \n");
    185186
    186187                        wait_ctx( this_thread, 0 )
     
    195196                }
    196197
    197                 __cfaabi_dbg_print_safe("Kernel : Destroying %p\n", this);
     198                LIB_DEBUG_PRINT_SAFE("Kernel : Destroying %p\n", this);
    198199
    199200        }
     
    202203        void __leave_monitor_desc( monitor_desc * this ) {
    203204                // Lock the monitor spinlock, DO_LOCK to reduce contention
    204                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
    205 
    206                 __cfaabi_dbg_print_safe("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
     205                DO_LOCK( this->lock DEBUG_CTX2 );
     206
     207                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
    207208
    208209                verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", this_thread, this->owner, this->recursion, this );
     
    214215                // it means we don't need to do anything
    215216                if( this->recursion != 0) {
    216                         __cfaabi_dbg_print_safe("Kernel :  recursion still %d\n", this->recursion);
     217                        LIB_DEBUG_PRINT_SAFE("Kernel :  recursion still %d\n", this->recursion);
    217218                        unlock( this->lock );
    218219                        return;
     
    231232        // Leave single monitor for the last time
    232233        void __leave_dtor_monitor_desc( monitor_desc * this ) {
    233                 __cfaabi_dbg_debug_do(
     234                LIB_DEBUG_DO(
    234235                        if( this_thread != this->owner ) {
    235236                                abortf("Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, this_thread, this->owner);
     
    248249
    249250                // Lock the monitor now
    250                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     251                DO_LOCK( this->lock DEBUG_CTX2 );
    251252
    252253                disable_interrupts();
     
    307308        (this_thread->monitors){m, count, func};
    308309
    309         // __cfaabi_dbg_print_safe("MGUARD : enter %d\n", count);
     310        // LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count);
    310311
    311312        // Enter the monitors in order
     
    313314        enter( group );
    314315
    315         // __cfaabi_dbg_print_safe("MGUARD : entered\n");
     316        // LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n");
    316317}
    317318
     
    319320// Dtor for monitor guard
    320321void ^?{}( monitor_guard_t & this ) {
    321         // __cfaabi_dbg_print_safe("MGUARD : leaving %d\n", this.count);
     322        // LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count);
    322323
    323324        // Leave the monitors in order
    324325        leave( this.m, this.count );
    325326
    326         // __cfaabi_dbg_print_safe("MGUARD : left\n");
     327        // LIB_DEBUG_PRINT_SAFE("MGUARD : left\n");
    327328
    328329        // Restore thread context
     
    429430
    430431        //Some more checking in debug
    431         __cfaabi_dbg_debug_do(
     432        LIB_DEBUG_DO(
    432433                thread_desc * this_thrd = this_thread;
    433434                if ( this.monitor_count != this_thrd->monitors.size ) {
     
    486487        set_owner( monitors, count, signallee );
    487488
    488         __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
     489        LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
    489490
    490491        //Everything is ready to go to sleep
     
    495496
    496497
    497         __cfaabi_dbg_print_buffer_local( "Kernel :   signal_block returned\n" );
     498        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel :   signal_block returned\n" );
    498499
    499500        //We are back, restore the masks and recursions
     
    534535        __lock_size_t actual_count = aggregate( mon_storage, mask );
    535536
    536         __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max);
     537        LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max);
    537538
    538539        if(actual_count == 0) return;
    539540
    540         __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n");
     541        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : waitfor internal proceeding\n");
    541542
    542543        // Create storage for monitor context
     
    555556                        __acceptable_t& accepted = mask[index];
    556557                        if( accepted.is_dtor ) {
    557                                 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n");
     558                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : dtor already there\n");
    558559                                verifyf( accepted.size == 1,  "ERROR: Accepted dtor has more than 1 mutex parameter." );
    559560
     
    567568                        }
    568569                        else {
    569                                 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n");
     570                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : thread present, baton-passing\n");
    570571
    571572                                // Create the node specific to this wait operation
     
    575576                                monitor_save;
    576577
    577                                 __cfaabi_dbg_print_buffer_local( "Kernel :  baton of %d monitors : ", count );
     578                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel :  baton of %d monitors : ", count );
    578579                                #ifdef __CFA_DEBUG_PRINT__
    579580                                        for( int i = 0; i < count; i++) {
    580                                                 __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
     581                                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
    581582                                        }
    582583                                #endif
    583                                 __cfaabi_dbg_print_buffer_local( "\n");
     584                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "\n");
    584585
    585586                                // Set the owners to be the next thread
     
    592593                                monitor_restore;
    593594
    594                                 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n");
     595                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : thread present, returned\n");
    595596                        }
    596597
    597                         __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
     598                        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted);
    598599                        return;
    599600                }
     
    602603
    603604        if( duration == 0 ) {
    604                 __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n");
     605                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : non-blocking, exiting\n");
    605606
    606607                unlock_all( locks, count );
    607608
    608                 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
     609                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted);
    609610                return;
    610611        }
     
    613614        verifyf( duration < 0, "Timeout on waitfor statments not supported yet.");
    614615
    615         __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n");
     616        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : blocking waitfor\n");
    616617
    617618        // Create the node specific to this wait operation
     
    635636        monitor_restore;
    636637
    637         __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n");
    638 
    639         __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
     638        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : exiting\n");
     639
     640        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted);
    640641}
    641642
     
    644645
    645646static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
    646         // __cfaabi_dbg_print_safe("Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
     647        // LIB_DEBUG_PRINT_SAFE("Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
    647648
    648649        //Pass the monitor appropriately
     
    676677static inline thread_desc * next_thread( monitor_desc * this ) {
    677678        //Check the signaller stack
    678         __cfaabi_dbg_print_safe("Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top);
     679        LIB_DEBUG_PRINT_SAFE("Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top);
    679680        __condition_criterion_t * urgent = pop( this->signal_stack );
    680681        if( urgent ) {
     
    728729        for( __lock_size_t i = 0; i < count; i++) {
    729730                (criteria[i]){ monitors[i], waiter };
    730                 __cfaabi_dbg_print_safe( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] );
     731                LIB_DEBUG_PRINT_SAFE( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] );
    731732                push( criteria[i].target->signal_stack, &criteria[i] );
    732733        }
     
    737738static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
    738739        for( __lock_size_t i = 0; i < count; i++ ) {
    739                 DO_LOCK( *locks[i] __cfaabi_dbg_ctx2 );
     740                DO_LOCK( *locks[i] DEBUG_CTX2 );
    740741        }
    741742}
     
    744745        for( __lock_size_t i = 0; i < count; i++ ) {
    745746                __spinlock_t * l = &source[i]->lock;
    746                 DO_LOCK( *l __cfaabi_dbg_ctx2 );
     747                DO_LOCK( *l DEBUG_CTX2 );
    747748                if(locks) locks[i] = l;
    748749        }
     
    802803        for(    int i = 0; i < count; i++ ) {
    803804
    804                 // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );
     805                // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
    805806                if( &criteria[i] == target ) {
    806807                        criteria[i].ready = true;
    807                         // __cfaabi_dbg_print_safe( "True\n" );
     808                        // LIB_DEBUG_PRINT_SAFE( "True\n" );
    808809                }
    809810
     
    811812        }
    812813
    813         __cfaabi_dbg_print_safe( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );
     814        LIB_DEBUG_PRINT_SAFE( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );
    814815        return ready2run ? node->waiting_thread : NULL;
    815816}
     
    818819        thread_desc * thrd = this_thread;
    819820        if( !this.monitors ) {
    820                 // __cfaabi_dbg_print_safe("Branding\n");
     821                // LIB_DEBUG_PRINT_SAFE("Branding\n");
    821822                assertf( thrd->monitors.data != NULL, "No current monitor to brand condition %p", thrd->monitors.data );
    822823                this.monitor_count = thrd->monitors.size;
    823824
    824                 this.monitors = (monitor_desc **)malloc( this.monitor_count * sizeof( *this.monitors ) );
     825                this.monitors = malloc( this.monitor_count * sizeof( *this.monitors ) );
    825826                for( int i = 0; i < this.monitor_count; i++ ) {
    826827                        this.monitors[i] = thrd->monitors[i];
Note: See TracChangeset for help on using the changeset viewer.