Move named channel code to the thread module instead of having a static map. Fixes issue #1464.

This commit is contained in:
Alex Szpakowski
2018-12-31 22:36:36 -04:00
parent 364718fa46
commit 19890a65c3
5 changed files with 16 additions and 60 deletions
+1 -52
View File
@@ -19,8 +19,6 @@
**/
#include "Channel.h"
#include <map>
#include <string>
#include <timer/Timer.h>
@@ -30,60 +28,21 @@ namespace thread
{
love::Type Channel::type("Channel", &Object::type);
static std::map<std::string, Channel *> namedChannels;
static Mutex *namedChannelMutex;
Channel *Channel::getChannel(const std::string &name)
{
if (!namedChannelMutex)
namedChannelMutex = newMutex();
Lock lock(namedChannelMutex);
auto it = namedChannels.find(name);
if (it != namedChannels.end())
{
it->second->retain();
return it->second;
}
namedChannels[name] = new Channel(name);
return namedChannels[name];
}
Channel::Channel()
: named(false)
, sent(0)
, received(0)
{
}
Channel::Channel(const std::string &name)
: named(true)
, name(name)
, sent(0)
: sent(0)
, received(0)
{
}
Channel::~Channel()
{
if (named)
{
Lock l(namedChannelMutex);
namedChannels.erase(name);
}
}
uint64 Channel::push(const Variant &var)
{
Lock l(mutex);
// Keep a reference to ourselves
// if we're non-empty and named.
if (named && queue.empty())
retain();
queue.push(var);
cond->broadcast();
@@ -134,11 +93,6 @@ bool Channel::pop(Variant *var)
received++;
cond->broadcast();
// Release our reference to ourselves
// if we're empty and named.
if (named && queue.empty())
release();
return true;
}
@@ -208,11 +162,6 @@ void Channel::clear()
// 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()
-6
View File
@@ -23,7 +23,6 @@
// STL
#include <queue>
#include <string>
// LOVE
#include "common/Variant.h"
@@ -47,8 +46,6 @@ public:
Channel();
~Channel();
static Channel *getChannel(const std::string &name);
uint64 push(const Variant &var);
bool supply(const Variant &var); // blocking push
bool supply(const Variant &var, double timeout);
@@ -62,15 +59,12 @@ public:
private:
Channel(const std::string &name);
void lockMutex();
void unlockMutex();
MutexRef mutex;
ConditionalRef cond;
std::queue<Variant> queue;
bool named;
std::string name;
uint64 sent;
uint64 received;
+9 -1
View File
@@ -37,7 +37,15 @@ Channel *ThreadModule::newChannel()
Channel *ThreadModule::getChannel(const std::string &name)
{
return Channel::getChannel(name);
Lock lock(namedChannelMutex);
auto it = namedChannels.find(name);
if (it != namedChannels.end())
return it->second;
Channel *c = new Channel();
namedChannels[name].set(c, Acquire::NORETAIN);
return c;
}
const char *ThreadModule::getName() const
+6
View File
@@ -23,6 +23,7 @@
// STL
#include <string>
#include <map>
// LOVE
#include "common/Data.h"
@@ -51,6 +52,11 @@ public:
virtual const char *getName() const;
virtual ModuleType getModuleType() const { return M_THREAD; }
private:
std::map<std::string, StrongRef<Channel>> namedChannels;
MutexRef namedChannelMutex;
}; // ThreadModule
} // thread
-1
View File
@@ -94,7 +94,6 @@ int w_getChannel(lua_State *L)
std::string name = luax_checkstring(L, 1);
Channel *c = instance()->getChannel(name);
luax_pushtype(L, c);
c->release();
return 1;
}