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: Optimal timing slice , Thread qn


Thanks Andrew. I've understood your mail,However if we remove both
yield in write and read threads : Why is it that only the write thread
gets to run. Shouldn't the flow be : write runs.. releases mutex..and
assuming data came for the serial port meantime, read should get the 
control and run ...release mutex ....give control to write ...so on.
Anyhow to answer your question: I thought I needed a mutex for printf
as its mentioned in the documentation that C Library functions like
printf need to be protected by mutex. 2nd reason is that I thought I
needed a mutex to protect the serial port resource from simultaneous
read and write. Is this a wrong/unnecessary use of mutex?



On Wed, 19 Jan 2005 21:07:29 +0100, Andrew Lunn <andrew@lunn.ch> wrote:
> > //functions definitions
> > void threadWriteRoutine(cyg_addrword_t data)
> > {
> >       while(1)
> >       {
> >               cyg_mutex_lock(&printfSerialIoMutex);
> >               err = cyg_io_write (ttyHdl,outputString,&outputLen);
> >               cyg_mutex_unlock(&printfSerialIoMutex);
> >               cyg_thread_yield(); //give execution to thread at same priority level
> >       }
> > }
> >
> > void threadReadRoutine(cyg_addrword_t data)
> > {
> >       while(1)
> >       {
> >               cyg_mutex_lock(&printfSerialIoMutex);
> >               err = cyg_io_read (ttyHdl,readBuffer,&readLen);
> >               printf("Read Byte %s\n\r",readBuffer);
> >               cyg_mutex_unlock(&printfSerialIoMutex);
> >               cyg_thread_yield(); //give execution to thread at same priority level
> >       }
> > }
> 
> Lets ignore the yield()s for the moment.  If you just dry run this in
> you head you will see the problem.
> 
> The read routine runs, locks the mutex and then blocks on the
> read. The write routine gets to run, but gets blocked on the
> mutex. Everything stops. Some time later something arrives on the
> serial port. The cyg_io_read returns, the mutex is unlocked, then
> around the loop again, locks the mutex, blocks on the read.
> 
> Since the write thread is running at the same priority as the read
> thread, unlocking the mutex by the read thread does not cause a
> context switch to the write thread.
> 
> Note that time slicing places no role here. Time slicing only happens
> when a thread runs continually for a full slice. In this code, the
> thread is always blocked in the read, so does not consume its slice.
> 
> With the yields, after unlocking the mutex a context switch will
> happen. The read thread goes to sleep and the write thread gets to
> run. It gets the mutex, writes out the date, unlocks the mutex and
> then yields. The read thread gets to run, locks the mutex, and then
> blocks in the read....
> 
> I hope you know understand what is happening.
> 
> Question: Why the mutex?
> 
>         Andrew
> 
>

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


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