Ignore:
File:
1 edited

Legend:

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

    r09f357ec rb0c7419  
    117117
    118118                        // Some one else has the monitor, wait in line for it
     119                        /* paranoid */ verify( thrd->next == 0p );
    119120                        append( this->entry_queue, thrd );
    120 
    121                         BlockInternal( &this->lock );
     121                        /* paranoid */ verify( thrd->next == 1p );
     122
     123                        unlock( this->lock );
     124                        park();
    122125
    123126                        __cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this);
    124127
    125                         // BlockInternal will unlock spinlock, no need to unlock ourselves
     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 );
    126129                        return;
    127130                }
    128131
    129132                __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 );
    130136
    131137                // Release the lock and leave
     
    149155                        set_owner( this, thrd );
    150156
     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
    151159                        unlock( this->lock );
    152160                        return;
     
    166174                        // Wake the thread that is waiting for this
    167175                        __condition_criterion_t * urgent = pop( this->signal_stack );
    168                         verify( urgent );
     176                        /* paranoid */ verify( urgent );
    169177
    170178                        // Reset mask
     
    175183
    176184                        // Some one else has the monitor, wait for him to finish and then run
    177                         BlockInternal( &this->lock, urgent->owner->waiting_thread );
     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();
    178193
    179194                        // Some one was waiting for us, enter
    180                         set_owner( this, thrd );
     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 );
    181196                }
    182197                else {
     
    187202
    188203                        // Some one else has the monitor, wait in line for it
     204                        /* paranoid */ verify( thrd->next == 0p );
    189205                        append( this->entry_queue, thrd );
    190                         BlockInternal( &this->lock );
    191 
    192                         // BlockInternal will unlock spinlock, no need to unlock ourselves
     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 );
    193213                        return;
    194214                }
     
    205225                __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner);
    206226
    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 );
     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 );
    208228
    209229                // Leaving a recursion level, decrement the counter
     
    221241                thread_desc * new_owner = next_thread( this );
    222242
     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
    223247                // We can now let other threads in safely
    224248                unlock( this->lock );
    225249
    226250                //We need to wake-up the thread
    227                 WakeThread( new_owner );
     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 );
    228253        }
    229254
     
    252277                disable_interrupts();
    253278
    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 );
     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 );
    257282
    258283                // Leaving a recursion level, decrement the counter
     
    266291                thread_desc * new_owner = next_thread( this );
    267292
    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 );
     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();
    272306
    273307                // Control flow should never reach here!
     
    400434        // Append the current wait operation to the ones already queued on the condition
    401435        // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
     436        /* paranoid */ verify( waiter.next == 0p );
    402437        append( this.blocked, &waiter );
     438        /* paranoid */ verify( waiter.next == 1p );
    403439
    404440        // Lock all monitors (aggregates the locks as well)
     
    419455        }
    420456
     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
    421467        // Everything is ready to go to sleep
    422         BlockInternal( locks, count, threads, thread_count );
     468        park();
    423469
    424470        // We are back, restore the owners and recursions
     
    490536        //Find the thread to run
    491537        thread_desc * signallee = pop_head( this.blocked )->waiting_thread;
     538        /* paranoid */ verify( signallee->next == 0p );
    492539        set_owner( monitors, count, signallee );
    493540
    494541        __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
    495542
     543        // unlock all the monitors
     544        unlock_all( locks, count );
     545
     546        // unpark the thread we signalled
     547        unpark( signallee );
     548
    496549        //Everything is ready to go to sleep
    497         BlockInternal( locks, count, &signallee, 1 );
     550        park();
    498551
    499552
     
    592645                                set_owner( monitors, count, next );
    593646
    594                                 // Everything is ready to go to sleep
    595                                 BlockInternal( locks, count, &next, 1 );
     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();
    596655
    597656                                // We are back, restore the owners and recursions
     
    631690        }
    632691
     692        // unlock all the monitors
     693        unlock_all( locks, count );
     694
    633695        //Everything is ready to go to sleep
    634         BlockInternal( locks, count );
     696        park();
    635697
    636698
     
    650712
    651713static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
    652         // __cfaabi_dbg_print_safe( "Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
     714        /* paranoid */ verify( this->lock.lock );
    653715
    654716        //Pass the monitor appropriately
     
    660722
    661723static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner ) {
    662         monitors[0]->owner     = owner;
    663         monitors[0]->recursion = 1;
     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;
    664728        for( __lock_size_t i = 1; i < count; i++ ) {
    665                 monitors[i]->owner     = owner;
    666                 monitors[i]->recursion = 0;
     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;
    667733        }
    668734}
     
    688754                //regardless of if we are ready to baton pass,
    689755                //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 );
    690757                set_owner( this,  urgent->owner->waiting_thread );
    691758
     
    696763        // Get the next thread in the entry_queue
    697764        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 );
    698767        set_owner( this, new_owner );
    699768
     
    841910        // For each thread in the entry-queue
    842911        for(    thread_desc ** thrd_it = &entry_queue.head;
    843                 *thrd_it;
     912                *thrd_it != 1p;
    844913                thrd_it = &(*thrd_it)->next
    845914        ) {
Note: See TracChangeset for help on using the changeset viewer.