Daniele Bartolini 11 lat temu
rodzic
commit
86978d6419
1 zmienionych plików z 12 dodań i 8 usunięć
  1. 12 8
      engine/core/thread/semaphore.h

+ 12 - 8
engine/core/thread/semaphore.h

@@ -34,7 +34,7 @@ struct Semaphore
 		CE_UNUSED(result);
 		CE_UNUSED(result);
 #elif CROWN_PLATFORM_WINDOWS
 #elif CROWN_PLATFORM_WINDOWS
 		_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
 		_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
-		CE_ASSERT(_handle != NULL, "Unable to create semaphore!");
+		CE_ASSERT(_handle != NULL, "CreateSemaphore: GetLastError = %d", GetLastError());
 		CE_UNUSED(_handle);
 		CE_UNUSED(_handle);
 #endif
 #endif
 	}
 	}
@@ -46,15 +46,18 @@ struct Semaphore
 		CE_ASSERT(result == 0, "pthread_cond_destroy: errno = %d", result);
 		CE_ASSERT(result == 0, "pthread_cond_destroy: errno = %d", result);
 		CE_UNUSED(result);
 		CE_UNUSED(result);
 #elif CROWN_PLATFORM_WINDOWS
 #elif CROWN_PLATFORM_WINDOWS
-		CloseHandle(_handle);
+		BOOL err = CloseHandle(_handle);
+		CE_ASSERT(err != 0, "CloseHandle: GetLastError = %d", GetLastError());
+		CE_UNUSED(err);
 #endif
 #endif
 	}
 	}
 
 
 	void post(uint32_t count = 1)
 	void post(uint32_t count = 1)
 	{
 	{
 #if CROWN_PLATFORM_POSIX
 #if CROWN_PLATFORM_POSIX
-		_mutex.lock();
-		for (uint32_t i = 0; i < count; i++)
+		ScopedMutex sm(_mutex);
+
+		for (uint32_t i = 0; i < count; ++i)
 		{
 		{
 			int result = pthread_cond_signal(&m_cond);
 			int result = pthread_cond_signal(&m_cond);
 			CE_ASSERT(result == 0, "pthread_cond_signal: errno = %d", result);
 			CE_ASSERT(result == 0, "pthread_cond_signal: errno = %d", result);
@@ -62,16 +65,18 @@ struct Semaphore
 		}
 		}
 
 
 		_count += count;
 		_count += count;
-		_mutex.unlock();
 #elif CROWN_PLATFORM_WINDOWS
 #elif CROWN_PLATFORM_WINDOWS
-		ReleaseSemaphore(_handle, count, NULL);
+		BOOL err = ReleaseSemaphore(_handle, count, NULL);
+		CE_ASSERT(err != 0, "ReleaseSemaphore: GetLastError = %d", GetLastError());
+		CE_UNUSED(err);
 #endif
 #endif
 	}
 	}
 
 
 	void wait()
 	void wait()
 	{
 	{
 #if CROWN_PLATFORM_POSIX
 #if CROWN_PLATFORM_POSIX
-		_mutex.lock();
+		ScopedMutex sm(_mutex);
+
 		while (_count <= 0)
 		while (_count <= 0)
 		{
 		{
 			int result = pthread_cond_wait(&m_cond, &(_mutex._mutex));
 			int result = pthread_cond_wait(&m_cond, &(_mutex._mutex));
@@ -80,7 +85,6 @@ struct Semaphore
 		}
 		}
 
 
 		_count--;
 		_count--;
-		_mutex.unlock();
 #elif CROWN_PLATFORM_WINDOWS
 #elif CROWN_PLATFORM_WINDOWS
 		DWORD result = WaitForSingleObject(_handle, INFINITE);
 		DWORD result = WaitForSingleObject(_handle, INFINITE);
 		CE_ASSERT(result == WAIT_OBJECT_0, "WaitForSingleObject: GetLastError = %d", GetLastError());
 		CE_ASSERT(result == WAIT_OBJECT_0, "WaitForSingleObject: GetLastError = %d", GetLastError());