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 02: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
> 
> > > 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();
> > > -- ..... --
> > > }

You need to look at the function as a whole - the parts you
cut out give the answer!  The reason for taking the scheduler 
lock is to prevent other threads from modifying the semaphore's 
data structures whilst this thread is manipulating them.  The 
purpose of the wait() function is to wait until the semaphore 
has a positive value, and then decrement it.  If it has a value 
of 0, the the calling thread must suspend (give up the CPU),
until some other thread increments the value via post().  Thus,
at the point of the reschedule() call above, the thread needs
to release the scheduler lock and let other threads run (so
they will have a chance to make that post() call).  Once this
is restarted, reschedule() will retake the lock and return to
the wait() function and try again.

Yes, to answer your other question, this happens all the time 
in multi-threaded systems - protecting data structures is of
paramount importance when you can't know what thread will be
running or when.

-- 
------------------------------------------------------------
Gary Thomas                  |
eCosCentric, Ltd.            |  
+1 (970) 229-1963            |  eCos & RedBoot experts
gthomas@ecoscentric.com      |
http://www.ecoscentric.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


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