Channel:push now returns an id value. Added Channel:hasRead(id), which returns true if the value that the id represents has already been popped, demanded, or cleared from the Channel. Resolves issue #1104.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-03-09 22:04:14 -04:00
parent e515cf076e
commit 140faf0cae
3 changed files with 29 additions and 35 deletions
+11 -29
View File
@@ -22,34 +22,11 @@
#include <map>
#include <string>
namespace
{
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;
uslong t, c;
t.u = target;
c.u = current;
return !(t.i < 0 && c.i > 0);
}
}
namespace love
{
namespace thread
{
static std::map<std::string, Channel *> namedChannels;
static Mutex *namedChannelMutex;
@@ -61,7 +38,6 @@ Channel *Channel::getChannel(const std::string &name)
Lock lock(namedChannelMutex);
auto it = namedChannels.find(name);
if (it != namedChannels.end())
{
it->second->retain();
@@ -96,7 +72,7 @@ Channel::~Channel()
}
}
unsigned long Channel::push(const Variant &var)
uint64 Channel::push(const Variant &var)
{
Lock l(mutex);
@@ -114,9 +90,9 @@ unsigned long Channel::push(const Variant &var)
void Channel::supply(const Variant &var)
{
Lock l(mutex);
unsigned long id = push(var);
uint64 id = push(var);
while (!past(id, received))
while (received < id)
cond->wait(mutex);
}
@@ -160,12 +136,18 @@ bool Channel::peek(Variant *var)
return true;
}
int Channel::getCount()
int Channel::getCount() const
{
Lock l(mutex);
return (int) queue.size();
}
bool Channel::hasRead(uint64 id) const
{
Lock l(mutex);
return received >= id;
}
void Channel::clear()
{
Lock l(mutex);