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


> //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]