Ignore:
Timestamp:
Jun 23, 2017, 10:12:04 AM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
d43cd01
Parents:
aa3d77b
Message:

preemption works for threads

File:
1 edited

Legend:

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

    raa3d77b r1c273d0  
    4444
    4545extern "C" {
    46         void __enter_monitor_desc(monitor_desc * this) {
    47                 lock( &this->lock );
    48                 thread_desc * thrd = this_thread();
    49 
    50                 LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
     46        void __enter_monitor_desc( monitor_desc * this ) {
     47                lock( &this->lock, __PRETTY_FUNCTION__ );
     48                thread_desc * thrd = this_thread;
     49
     50                // LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
    5151
    5252                if( !this->owner ) {
     
    6262                        //Some one else has the monitor, wait in line for it
    6363                        append( &this->entry_queue, thrd );
    64                         LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
     64                        // LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
    6565                        BlockInternal( &this->lock );
    6666
     
    7575        // leave pseudo code :
    7676        //      TODO
    77         void __leave_monitor_desc(monitor_desc * this) {
    78                 lock( &this->lock );
    79 
    80                 LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
    81                 verifyf( this_thread() == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread(), this->owner, this->recursion );
     77        void __leave_monitor_desc( monitor_desc * this ) {
     78                lock( &this->lock, __PRETTY_FUNCTION__ );
     79
     80                // LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i). ", this_thread, this, this->owner, this->recursion);
     81                verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
    8282
    8383                //Leaving a recursion level, decrement the counter
     
    9696                unlock( &this->lock );
    9797
    98                 LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
     98                // LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
    9999
    100100                //We need to wake-up the thread
    101                 ScheduleThread( new_owner );
     101                WakeThread( new_owner );
     102        }
     103
     104        void __leave_thread_monitor( thread_desc * thrd ) {
     105                monitor_desc * this = &thrd->mon;
     106                lock( &this->lock, __PRETTY_FUNCTION__ );
     107
     108                disable_interrupts();
     109
     110                thrd->cor.state = Halted;
     111
     112                verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
     113
     114                //Leaving a recursion level, decrement the counter
     115                this->recursion -= 1;
     116
     117                //If we haven't left the last level of recursion
     118                //it means we don't need to do anything
     119                if( this->recursion != 0) {
     120                        unlock( &this->lock );
     121                        return;
     122                }
     123
     124                thread_desc * new_owner = next_thread( this );
     125
     126                //We can now let other threads in safely
     127                unlock( &this->lock );
     128
     129                //We need to wake-up the thread
     130                if( new_owner) ScheduleThread( new_owner );
    102131        }
    103132}
     
    121150        enter( this->m, this->count );
    122151
    123         this->prev_mntrs = this_thread()->current_monitors;
    124         this->prev_count = this_thread()->current_monitor_count;
    125 
    126         this_thread()->current_monitors      = m;
    127         this_thread()->current_monitor_count = count;
     152        this->prev_mntrs = this_thread->current_monitors;
     153        this->prev_count = this_thread->current_monitor_count;
     154
     155        this_thread->current_monitors      = m;
     156        this_thread->current_monitor_count = count;
    128157}
    129158
     
    131160        leave( this->m, this->count );
    132161
    133         this_thread()->current_monitors      = this->prev_mntrs;
    134         this_thread()->current_monitor_count = this->prev_count;
     162        this_thread->current_monitors      = this->prev_mntrs;
     163        this_thread->current_monitor_count = this->prev_count;
    135164}
    136165
     
    174203        LIB_DEBUG_PRINT_SAFE("count %i\n", count);
    175204
    176         __condition_node_t waiter = { this_thread(), count, user_info };
     205        __condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
    177206
    178207        __condition_criterion_t criteria[count];
     
    234263        //Some more checking in debug
    235264        LIB_DEBUG_DO(
    236                 thread_desc * this_thrd = this_thread();
     265                thread_desc * this_thrd = this_thread;
    237266                if ( this->monitor_count != this_thrd->current_monitor_count ) {
    238267                        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 );
     
    286315
    287316        //create creteria
    288         __condition_node_t waiter = { this_thread(), count, 0 };
     317        __condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
    289318
    290319        __condition_criterion_t criteria[count];
     
    335364// Internal scheduling
    336365void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
    337         // thread_desc * this = this_thread();
     366        // thread_desc * this = this_thread;
    338367
    339368        // unsigned short count = this->current_monitor_count;
     
    393422static inline void lock_all( spinlock ** locks, unsigned short count ) {
    394423        for( int i = 0; i < count; i++ ) {
    395                 lock( locks[i] );
     424                lock( locks[i], __PRETTY_FUNCTION__ );
    396425        }
    397426}
     
    400429        for( int i = 0; i < count; i++ ) {
    401430                spinlock * l = &source[i]->lock;
    402                 lock( l );
     431                lock( l, __PRETTY_FUNCTION__ );
    403432                if(locks) locks[i] = l;
    404433        }
     
    457486
    458487static inline void brand_condition( condition * this ) {
    459         thread_desc * thrd = this_thread();
     488        thread_desc * thrd = this_thread;
    460489        if( !this->monitors ) {
    461490                LIB_DEBUG_PRINT_SAFE("Branding\n");
Note: See TracChangeset for help on using the changeset viewer.