This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: a confusion regarding role of schedlock


On Fri, 2002-09-13 at 01:36, Brij Bihari Pandey wrote:
> Thanks Rich,
> 
> but my confusion is about - we took sched lock as we
> didn't want the threadswitch to happen, yet because of
> reschedule the threadswitch will happen.
> 
> brij

Oh.  That's easy then.  The question is WHY don't you want the
reschedule.

Here is more of the semaphore wait function:

cyg_bool Cyg_Counting_Semaphore::wait()
{
    cyg_bool result = true;
    Cyg_Thread *self = Cyg_Thread::self();
    
    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );
        
    while( count == 0 && result )
    {
        self->set_sleep_reason( Cyg_Thread::WAIT );
        
        self->sleep();
        
        queue.enqueue( self );

        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );

        // Allow other threads to run
        Cyg_Scheduler::reschedule();
....

The scheduler is locked to prevent a race condition where one thread
tries to acquire a semaphore at the same time another is trying to
acquire the same semaphore.

For example:

* The semaphore counts is 1

* Thread A reads the semaphore count, and determines the semaphore is
available.

* Thread A is then pre-empted at this point, and thread B also reads the
same semaphore's count and determines the semaphore is available

* Thread B then decrements the semaphore count and continues to run

* Thread A, since it was pre-empted doesn't realize that thread B got
the semaphore, and it also gets the semaphore since when it read the
count, the semaphore's count was 1 when it checked the value.  It's
actually 0 now since thread B got the semaphore.

So both threads get the semaphore, but only decrement the semaphore
count by 1.  You might not think this is likely to ever happen, but it
always eventually does.

Hope that helps.  If you still need explanation, feel free to write
privately and I'll explain in detail when I get a chance.  The
eCosCentric guys know the system better than I do though still.

-Rich

> > > cyg_bool Cyg_Counting_Semaphore::wait()
> > > {
> > > -- ..... --     
> > >      // Prevent preemption
> > >      Cyg_Scheduler::lock();
> > > -- ..... -- 
> > >          // Allow other threads to run
> > >          Cyg_Scheduler::reschedule();
> > > -- ..... --
> > >      // Unlock the scheduler
> > >      Cyg_Scheduler::unlock();
> > > -- ..... --
> > > }
> 
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! News - Today's headlines
> http://news.yahoo.com
> 
> -- 
> Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
> and search the list archive: http://sources.redhat.com/ml/ecos-discuss
> 



-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]