Browse Source

Make Channels more thread-safe (I know, I know..)

--HG--
branch : minor
Bart van Strien 13 years ago
parent
commit
777e97de2e
3 changed files with 20 additions and 5 deletions
  1. 2 2
      src/common/Object.h
  2. 16 3
      src/modules/thread/Channel.cpp
  3. 2 0
      src/modules/thread/Channel.h

+ 2 - 2
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:
 

+ 16 - 3
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

+ 2 - 0
src/modules/thread/Channel.h

@@ -54,6 +54,8 @@ namespace thread
 		Variant *peek();
 		int count();
 		void clear();
+
+		void release();
 	}; // Channel
 } // thread
 } // love