Implementing recursive mutexes.
If you come from a POSIX environment, you'll find Symbian's support for synchronization primitives rather simplistic. This is in some way due to Symbian's preference for single threaded applications using the Active Object idiom, thus making the use of multithreading often unnecessary. Sometimes, the need for recursive (reentrant) mutexes arises (note though that POSIX mutexes aren't recursive by default). Here is a possible implementation of a wrapper class over a RMutex:
class TRecursiveMutex
{
public:
TRecursiveMutex();
~TRecursiveMutex();
void Acquire();
void Release();
private:
RMutex iMutex;
TThreadId iOwner;
TInt iCount;
};
TRecursiveMutex::TRecursiveMutex() : iCount(0)
{
iMutex.CreateLocal();
}
TRecursiveMutex::~TRecursiveMutex()
{
iMutex.Close();
}
void TRecursiveMutex::Acquire()
{
TThreadId id = RThread().Id();
if (iOwner == id)
{
++iCount;
}
else
{
iMutex.Wait();
iCount = 1;
iOwner = id;
}
}
void TRecursiveMutex::Release()
{
if (--iCount == 0)
{
iOwner = 0;
iMutex.Signal();
}
}
You'll notice this is a bare bones implementation. Potential things to be added (at least on debug builds) could be checking
thread ownership and iCount being 0 upon destruction.
All emulator builds have been compiled & linked using Microsoft Visual C++ 6.0 compiler. In some cases, some small changes might be required to make it work with your toolchain. I'm not responsible for your use of the information contained in or linked from these web pages.