Răsfoiți Sursa

Events use a mutex instead of a spin lock

Marko Pintera 11 ani în urmă
părinte
comite
fe3e1afbf0
2 a modificat fișierele cu 7 adăugiri și 21 ștergeri
  1. 1 4
      BoostPort.txt
  2. 6 17
      CamelotUtility/Include/BsEvent.h

+ 1 - 4
BoostPort.txt

@@ -1,10 +1,7 @@
 Events:
- - Locking currently doesn't work properly. I need to use stack allocation to temporarily allocate std::functions when triggering an event
  - Add support for variadic number of parameters. Initially just by using boost preprocessor like in other places.
   - Note for events and its function style arguments: "class variadic_extended_signature<R (Args...)>"
 
-Replace boost::any from Any in POCO
-Replace boost::uid from UUID in POCO
 Temporarily make filesystem use VS filesystem header
 
 --------------
@@ -16,4 +13,4 @@ Once ported to VS:
 --------------
 Other:
  - std::function allocates memory yet I'm not using a custom allocator for it. Replace std::function with custom Function method.
- - Ability to construct Module without having to do allocation outside of it
+ - Ability to construct Module using startUp without having to do allocation outside of it

+ 6 - 17
CamelotUtility/Include/BsEvent.h

@@ -52,7 +52,7 @@ namespace BansheeEngine
 			ConnectionData* connData = cm_new<ConnectionData>(func);
 
 			{
-				ScopedSpinLock lock;
+				CM_LOCK_MUTEX(mMutex);
 				mConnections.push_back(connData);
 			}
 			
@@ -61,17 +61,7 @@ namespace BansheeEngine
 
 		void operator() (P0 args)
 		{
-			SpinLock lock;
-
-			// TODO - This is TERRIBLE as the spin lock might lock for a long while while
-			// the called method is executing. But there is no reason to lock while method is executing.
-			// Fix by copying list of connections temporarily.
-
-			// Fix by:
-			//  - Using stack alloc allocate N std::function objects
-			//  - Create std::function objects using a stack allocator internally
-
-			lock.lock();
+			CM_LOCK_MUTEX(mMutex);
 
 			for(auto& connection : mConnections)
 			{
@@ -79,13 +69,11 @@ namespace BansheeEngine
 
 				func(args);
 			}
-
-			lock.unlock();
 		}
 
 		void clear()
 		{
-			ScopedSpinLock lock;
+			CM_LOCK_MUTEX(mMutex);
 
 			for(auto& connection : mConnections)
 			{
@@ -97,7 +85,7 @@ namespace BansheeEngine
 
 		bool empty()
 		{
-			ScopedSpinLock lock;
+			CM_LOCK_MUTEX(mMutex);
 
 			return mConnections.size() == 0;
 		}
@@ -106,6 +94,7 @@ namespace BansheeEngine
 		friend class EventHandle1;
 
 		typename Vector<ConnectionData*>::type mConnections;
+		CM_MUTEX(mMutex);
 
 		static void disconnectCallback(void* connection, void* event)
 		{
@@ -118,7 +107,7 @@ namespace BansheeEngine
 
 		void disconnect(ConnectionData* connData)
 		{
-			ScopedSpinLock lock;
+			CM_LOCK_MUTEX(mMutex);
 
 			for(auto& iter = mConnections.begin(); iter != mConnections.end(); ++iter)
 			{