I never really worked with mutexes before, but i need to control access to protected resources. Looking through the new C++11 stuff, i cooked up this class:
class CMutex
{
public:
class Lockable
{
friend class CMutex;
std::atomic_flag flag;
public:
Lockable()
{
flag.clear();
}
};
private:
Lockable * resource;
CMutex(const CMutex &);
public:
CMutex(Lockable * l)
{
resource = l;
acquire(l);
}
CMutex(Lockable & l)
{
resource = &l;
acquire(l);
}
CMutex()
: resource(nullptr)
{
}
~CMutex()
{
if (resource)
release(resource);
}
void acquire(Lockable * l)
{
if (!resource)
resource = l;
if (!spinLock(2000, resource))
//explode here
return;
}
void acquire(Lockable & l)
{
acquire(&l);
}
private:
void release(Lockable * l)
{
if (l)
l->flag.clear();
}
static bool spinLock(int ms, Lockable * bVal)
{
using namespace Misc;
long start;
int ret;
loop:
start = QuickTime();
while (bVal->flag.test_and_set()) {
if ((QuickTime() - start) > ms)
goto time_out;
// yield thread
Delay(0);
}
// normal exitpoint
return true;
// deadlock occurs
time_out:
// handle error ...
}
}
Usage like so:
class MyClass : public CMutex::lockable
{
...
void doStuff()
{
// lock data
CMutex(this);
// do stuff
...
// mutex should automagically be RAII released here
}
...
};
First of all, I'm interested in whether this concept actually works how it should (given the implementation of std::atomic etc.)?
Secondly, I noticed that it correctly obtains the lock, however it releases it instantly. I guess i should give the lock a name?
CMutex lock(this);
However, isn't the compiler free to destruct the object before the scope is left as an optimization provided it can guarantee that i wont interact more with the object? This would defeat the purpose of this construct, if i can't guarantee that the destructor only will be called at scope exit.
Regards