Ignore:
File:
1 edited

Legend:

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

    rb0c7419 r09f357ec  
    117117
    118118                        // Some one else has the monitor, wait in line for it
    119                         /* paranoid */ verify( thrd->next == 0p );
    120119                        append( this->entry_queue, thrd );
    121                         /* paranoid */ verify( thrd->next == 1p );
    122 
    123                         unlock( this->lock );
    124                         park();
     120
     121                        BlockInternal( &this->lock );
    125122
    126123                        __cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this);
    127124
    128                         /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
     125                        // BlockInternal will unlock spinlock, no need to unlock ourselves
    129126                        return;
    130127                }
    131128
    132129                __cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this);
    133 
    134                 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
    135                 /* paranoid */ verify( this->lock.lock );
    136130
    137131                // Release the lock and leave
     
    155149                        set_owner( this, thrd );
    156150
    157                         verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
    158 
    159151                        unlock( this->lock );
    160152                        return;
     
    174166                        // Wake the thread that is waiting for this
    175167                        __condition_criterion_t * urgent = pop( this->signal_stack );
    176                         /* paranoid */ verify( urgent );
     168                        verify( urgent );
    177169
    178170                        // Reset mask
     
    183175
    184176                        // Some one else has the monitor, wait for him to finish and then run
    185                         unlock( this->lock );
    186 
    187                         // Release the next thread
    188                         /* paranoid */ verifyf( urgent->owner->waiting_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
    189                         unpark( urgent->owner->waiting_thread );
    190 
    191                         // Park current thread waiting
    192                         park();
     177                        BlockInternal( &this->lock, urgent->owner->waiting_thread );
    193178
    194179                        // Some one was waiting for us, enter
    195                         /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
     180                        set_owner( this, thrd );
    196181                }
    197182                else {
     
    202187
    203188                        // Some one else has the monitor, wait in line for it
    204                         /* paranoid */ verify( thrd->next == 0p );
    205189                        append( this->entry_queue, thrd );
    206                         /* paranoid */ verify( thrd->next == 1p );
    207                         unlock( this->lock );
    208 
    209                         // Park current thread waiting
    210                         park();
    211 
    212                         /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
     190                        BlockInternal( &this->lock );
     191
     192                        // BlockInternal will unlock spinlock, no need to unlock ourselves
    213193                        return;
    214194                }
     
    225205                __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner);
    226206
    227                 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
     207                verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
    228208
    229209                // Leaving a recursion level, decrement the counter
     
    241221                thread_desc * new_owner = next_thread( this );
    242222
    243                 // Check the new owner is consistent with who we wake-up
    244                 // new_owner might be null even if someone owns the monitor when the owner is still waiting for another monitor
    245                 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
    246 
    247223                // We can now let other threads in safely
    248224                unlock( this->lock );
    249225
    250226                //We need to wake-up the thread
    251                 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
    252                 unpark( new_owner );
     227                WakeThread( new_owner );
    253228        }
    254229
     
    277252                disable_interrupts();
    278253
    279                 thrd->state = Halted;
    280 
    281                 /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
     254                thrd->self_cor.state = Halted;
     255
     256                verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
    282257
    283258                // Leaving a recursion level, decrement the counter
     
    291266                thread_desc * new_owner = next_thread( this );
    292267
    293                 // Release the monitor lock
    294                 unlock( this->lock );
    295 
    296                 // Unpark the next owner if needed
    297                 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
    298                 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    299                 /* paranoid */ verify( ! kernelTLS.this_processor->destroyer );
    300                 /* paranoid */ verify( thrd->state == Halted );
    301 
    302                 kernelTLS.this_processor->destroyer = new_owner;
    303 
    304                 // Leave the thread
    305                 __leave_thread();
     268                // Leave the thread, this will unlock the spinlock
     269                // Use leave thread instead of BlockInternal which is
     270                // specialized for this case and supports null new_owner
     271                LeaveThread( &this->lock, new_owner );
    306272
    307273                // Control flow should never reach here!
     
    434400        // Append the current wait operation to the ones already queued on the condition
    435401        // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
    436         /* paranoid */ verify( waiter.next == 0p );
    437402        append( this.blocked, &waiter );
    438         /* paranoid */ verify( waiter.next == 1p );
    439403
    440404        // Lock all monitors (aggregates the locks as well)
     
    455419        }
    456420
    457         // Unlock the locks, we don't need them anymore
    458         for(int i = 0; i < count; i++) {
    459                 unlock( *locks[i] );
    460         }
    461 
    462         // Wake the threads
    463         for(int i = 0; i < thread_count; i++) {
    464                 unpark( threads[i] );
    465         }
    466 
    467421        // Everything is ready to go to sleep
    468         park();
     422        BlockInternal( locks, count, threads, thread_count );
    469423
    470424        // We are back, restore the owners and recursions
     
    536490        //Find the thread to run
    537491        thread_desc * signallee = pop_head( this.blocked )->waiting_thread;
    538         /* paranoid */ verify( signallee->next == 0p );
    539492        set_owner( monitors, count, signallee );
    540493
    541494        __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
    542495
    543         // unlock all the monitors
    544         unlock_all( locks, count );
    545 
    546         // unpark the thread we signalled
    547         unpark( signallee );
    548 
    549496        //Everything is ready to go to sleep
    550         park();
     497        BlockInternal( locks, count, &signallee, 1 );
    551498
    552499
     
    645592                                set_owner( monitors, count, next );
    646593
    647                                 // unlock all the monitors
    648                                 unlock_all( locks, count );
    649 
    650                                 // unpark the thread we signalled
    651                                 unpark( next );
    652 
    653                                 //Everything is ready to go to sleep
    654                                 park();
     594                                // Everything is ready to go to sleep
     595                                BlockInternal( locks, count, &next, 1 );
    655596
    656597                                // We are back, restore the owners and recursions
     
    690631        }
    691632
    692         // unlock all the monitors
    693         unlock_all( locks, count );
    694 
    695633        //Everything is ready to go to sleep
    696         park();
     634        BlockInternal( locks, count );
    697635
    698636
     
    712650
    713651static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
    714         /* paranoid */ verify( this->lock.lock );
     652        // __cfaabi_dbg_print_safe( "Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
    715653
    716654        //Pass the monitor appropriately
     
    722660
    723661static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner ) {
    724         /* paranoid */ verify ( monitors[0]->lock.lock );
    725         /* paranoid */ verifyf( monitors[0]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[0]->owner, monitors[0]->recursion, monitors[0] );
    726         monitors[0]->owner        = owner;
    727         monitors[0]->recursion    = 1;
     662        monitors[0]->owner     = owner;
     663        monitors[0]->recursion = 1;
    728664        for( __lock_size_t i = 1; i < count; i++ ) {
    729                 /* paranoid */ verify ( monitors[i]->lock.lock );
    730                 /* paranoid */ verifyf( monitors[i]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[i]->owner, monitors[i]->recursion, monitors[i] );
    731                 monitors[i]->owner        = owner;
    732                 monitors[i]->recursion    = 0;
     665                monitors[i]->owner     = owner;
     666                monitors[i]->recursion = 0;
    733667        }
    734668}
     
    754688                //regardless of if we are ready to baton pass,
    755689                //we need to set the monitor as in use
    756                 /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
    757690                set_owner( this,  urgent->owner->waiting_thread );
    758691
     
    763696        // Get the next thread in the entry_queue
    764697        thread_desc * new_owner = pop_head( this->entry_queue );
    765         /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
    766         /* paranoid */ verify( !new_owner || new_owner->next == 0p );
    767698        set_owner( this, new_owner );
    768699
     
    910841        // For each thread in the entry-queue
    911842        for(    thread_desc ** thrd_it = &entry_queue.head;
    912                 *thrd_it != 1p;
     843                *thrd_it;
    913844                thrd_it = &(*thrd_it)->next
    914845        ) {
Note: See TracChangeset for help on using the changeset viewer.