Sunday, September 7, 2008

Theory On RMutex

----------------------------------Theory on RMutex--------------------------------------

---->What is Mutex? Mutual exclusion object is called Mutex. When you are going to handle a particular non sharable resource, it’s necessary to use mutex to support non sharable resource sharing feature. For example in our system, UART port is non sharable. Only one thread can hold at any point of time. Consider this situation; In Symbian phone a thread is holding a UART port for transferring data to system. After some time another thread needs to use UART port for some other purpose, but already a thread holding the UART port so unless until the thread releases the port this particular thread cannot access the UART port. How does the second thread come to know once the first thread released the UART? Here RMutex comes into picture. When a thread or process is going to use the non-sharable resource it should create or use (already exiting Mutex object of that particular resource) a mutual exclusion object and who are all waiting for a particular resource, they should also use the same mutex, because when a thread releasing a non sharable resource the kernel will notify to all other thread which are all waiting for that resource (i.e.) which are all waiting on the RMutex object. If multiple objects are waiting for a particular resource that time priority and scheduling will come into picture.





----> Consider already kernel created an RMutex object for UART resource. If a thread wants to use the UART resource it should first wait on that Mutex object before acquiring the UART resource, it means, it should check, weather the UART resource is free or other thread is holding the UART.

Openning a MUTEX object:
RMutex iMutexname ;
Err = iMutexname .OpenGlobal(UART_Mutext_Name);
If (Err == KErrNone)
{
RDebug::Printf(“UART mutex open failed\n”);
}

---->Before going to use the UART that particular thread should check, weather the UART resource is free or not

iMutexname.Wait();

---->The above function will not return unless until other thread releases the UART resource. Once this thread acquired the UART resource it can do its work. If this thread wants to free UART resource it should call
iMutexname.Signal();

---->This SWI call will inform other threads, who all are waiting for the UART resource.

NOTE: Before signaling the resource to free, programmer should check weather this thread acquired the resource or not using IsHeld() function

If (iMutexname.IsHeld())
iMutexname.Signal();


---->If you are not checking, sometime it will end up with KERN-EXEC 1, It means, the thread is not acquired the mutext but it’s trying to free the mutex that it not possible by kernel.
Ohhhhhhhhhhhh:-)

No comments: