← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3060: use a lock-free queue for dwt async callbacks (experimental!)

 

------------------------------------------------------------
revno: 3060
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Fri 2012-09-21 20:35:34 +0200
message:
  use a lock-free queue for dwt async callbacks (experimental!)
modified:
  dcpp/atomic.h
  dwt/include/dwt/Application.h
  dwt/src/Application.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/atomic.h'
--- dcpp/atomic.h	2012-09-18 17:14:06 +0000
+++ dcpp/atomic.h	2012-09-21 18:35:34 +0000
@@ -21,6 +21,8 @@
 
 // GCC has issues with atomic - see https://bugs.launchpad.net/dcplusplus/+bug/735512
 /// @todo check this again when GCC improves their threading support
+/// @todo track the progress of <https://svn.boost.org/trac/boost/ticket/7403> when updating boost
+/// (failure to apply the patch makes MSVC lockfree lists not actually lock-free).
 #if defined(__GNUC__)
 
 #include <boost/atomic.hpp>

=== modified file 'dwt/include/dwt/Application.h'
--- dwt/include/dwt/Application.h	2012-07-19 21:15:09 +0000
+++ dwt/include/dwt/Application.h	2012-09-21 18:35:34 +0000
@@ -41,6 +41,8 @@
 #include "CommandLine.h"
 #include <functional>
 
+#include <boost/lockfree/queue.hpp>
+
 #ifdef _MSC_VER
 #ifdef _DEBUG
 // Enable memory leak detection with file/line tracing.
@@ -186,8 +188,7 @@
 	// The according signals we must raise, go in this vector.
 	std::vector<Callback> itsVSignals;
 
-	HANDLE taskMutex;
-	std::list<Callback> tasks;
+	boost::lockfree::queue<Callback> tasks;
 
 	FilterList filters;
 

=== modified file 'dwt/src/Application.cpp'
--- dwt/src/Application.cpp	2012-07-19 21:15:09 +0000
+++ dwt/src/Application.cpp	2012-09-21 18:35:34 +0000
@@ -74,13 +74,14 @@
 }
 
 Application::Application(int nCmdShow) :
-	itsCmdShow(nCmdShow), taskMutex(::CreateMutex(NULL, FALSE, NULL)), quit(false),
+	itsCmdShow(nCmdShow),
+	tasks(1024),
+	quit(false),
 	threadId(::GetCurrentThreadId())
 {
 }
 
 Application::~Application() {
-	::CloseHandle(taskMutex);
 }
 
 const CommandLine& Application::getCommandLine() const {
@@ -222,34 +223,17 @@
 	return itsCmdShow;
 }
 
-struct MutexLock {
-	MutexLock(HANDLE h_) : h(h_) {
-		::WaitForSingleObject(h, INFINITE);
-	}
-	~MutexLock() {
-		::ReleaseMutex(h);
-	}
-	HANDLE h;
-};
-
 bool Application::dispatchAsync() {
 	Callback callback;
-	{
-		MutexLock m(taskMutex);
-		if(tasks.empty()) {
-			return false;
-		}
-		callback = tasks.front();
-		tasks.pop_front();
+	if(tasks.pop(callback)) {
+		callback();
+		return true;
 	}
-
-	callback();
-	return true;
+	return false;
 }
 
 void Application::callAsync(const Callback& callback) {
-	MutexLock m(taskMutex);
-	tasks.push_back(callback);
+	tasks.push(callback);
 	wake();
 }