From 777e97de2ea05e8a96af50c87704d720535cc7db Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Wed, 4 Jul 2012 20:22:54 +0200 Subject: [PATCH] Make Channels more thread-safe (I know, I know..) --HG-- branch : minor --- src/common/Object.h | 4 ++-- src/modules/thread/Channel.cpp | 19 ++++++++++++++++--- src/modules/thread/Channel.h | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/common/Object.h b/src/common/Object.h index 301fce214..28691d8da 100644 --- a/src/common/Object.h +++ b/src/common/Object.h @@ -56,14 +56,14 @@ public: * Retains the Object, i.e. increases the * reference count by one. **/ - void retain(); + virtual void retain(); /** * Releases one reference to the Object, i.e. decrements the * reference count by one, and potentially deletes the Object * if there are no more references. **/ - void release(); + virtual void release(); private: diff --git a/src/modules/thread/Channel.cpp b/src/modules/thread/Channel.cpp index 69930eca3..7d9f2137a 100644 --- a/src/modules/thread/Channel.cpp +++ b/src/modules/thread/Channel.cpp @@ -65,10 +65,7 @@ namespace thread delete mutex; delete cond; if (named) - { - Lock l(namedChannelMutex); namedChannels.erase(name); - } } void Channel::push(Variant *var) @@ -133,6 +130,11 @@ namespace thread void Channel::clear() { Lock l(mutex); + + // We're already empty. + if (queue.empty()) + return; + while (!queue.empty()) { queue.front()->release(); @@ -144,5 +146,16 @@ namespace thread if (named) release(); } + + void Channel::release() + { + if (named) + namedChannelMutex->lock(); + + Object::release(); + + if (named) + namedChannelMutex->unlock(); + } } // thread } // love diff --git a/src/modules/thread/Channel.h b/src/modules/thread/Channel.h index 09db2c86c..94d63c1b5 100644 --- a/src/modules/thread/Channel.h +++ b/src/modules/thread/Channel.h @@ -54,6 +54,8 @@ namespace thread Variant *peek(); int count(); void clear(); + + void release(); }; // Channel } // thread } // love