Fix race conditions and deadlocks in Channels (hopefully) (issue #554)

Also, this is a giant commit because of the official Code Style (tm) was applied.

--HG--
branch : minor
This commit is contained in:
Bart van Strien
2013-01-30 17:15:12 +01:00
parent be2363fc00
commit c7c5d0ed6b
15 changed files with 414 additions and 367 deletions
+52 -27
View File
@@ -24,55 +24,63 @@
namespace
{
union uslong
{
unsigned long u;
long i;
};
union uslong
{
unsigned long u;
long i;
};
// target <= current, but semi-wrapsafe, one wrap, anyway
inline bool past(unsigned int target, unsigned int current)
{
if (target > current)
return false;
if (target == current)
return true;
// target <= current, but semi-wrapsafe, one wrap, anyway
inline bool past(unsigned int target, unsigned int current)
{
if (target > current)
return false;
if (target == current)
return true;
uslong t, c;
t.u = target;
c.u = current;
uslong t, c;
t.u = target;
c.u = current;
return !(t.i < 0 && c.i > 0);
}
return !(t.i < 0 && c.i > 0);
}
}
namespace love
{
namespace thread
{
static std::map<std::string, Channel*> namedChannels;
static std::map<std::string, Channel *> namedChannels;
static Mutex *namedChannelMutex;
Channel *Channel::getChannel(const std::string &name)
{
if (!namedChannelMutex)
namedChannelMutex = newMutex();
Lock l(namedChannelMutex);
if (!namedChannels.count(name))
namedChannels[name] = new Channel(name);
else
namedChannels[name]->retain();
return namedChannels[name];
}
Channel::Channel()
: named(false), sent(0), received(0)
: named(false)
, sent(0)
, received(0)
{
mutex = newMutex();
cond = newConditional();
}
Channel::Channel(const std::string &name)
: named(true), name(name), sent(0), received(0)
: named(true)
, name(name)
, sent(0)
, received(0)
{
mutex = newMutex();
cond = newConditional();
@@ -88,6 +96,7 @@ Channel::~Channel()
delete mutex;
delete cond;
if (named)
namedChannels.erase(name);
}
@@ -96,12 +105,15 @@ unsigned long Channel::push(Variant *var)
{
if (!var)
return 0;
Lock l(mutex);
var->retain();
// Keep a reference to ourselves
// if we're non-empty and named.
if (named && queue.empty())
retain();
queue.push(var);
cond->broadcast();
@@ -113,13 +125,12 @@ void Channel::supply(Variant *var)
if (!var)
return;
mutex->lock();
unsigned long id = push(var);
mutex->lock();
while (!past(id, received))
{
cond->wait(mutex);
}
mutex->unlock();
}
@@ -139,18 +150,18 @@ Variant *Channel::pop()
// if we're empty and named.
if (named && queue.empty())
release();
return var;
} // NOTE: Returns a retained Variant
Variant *Channel::demand()
{
Variant *var;
mutex->lock();
while (!(var = pop()))
{
mutex->lock();
cond->wait(mutex);
mutex->unlock();
}
mutex->unlock();
return var;
}
@@ -185,12 +196,26 @@ void Channel::clear()
queue.pop();
}
// Finish all the supply waits
received = sent;
cond->broadcast();
// Once again, release our own
// reference if we're named.
if (named)
release();
}
void Channel::lockMutex()
{
mutex->lock();
}
void Channel::unlockMutex()
{
mutex->unlock();
}
void Channel::retain()
{
if (named)