Daniele Bartolini 11 年之前
父節點
當前提交
b378f8a72a
共有 4 個文件被更改,包括 57 次插入57 次删除
  1. 9 9
      engine/core/thread/atomic_int.h
  2. 15 15
      engine/core/thread/mutex.h
  3. 18 18
      engine/core/thread/semaphore.h
  4. 15 15
      engine/core/thread/thread.h

+ 9 - 9
engine/core/thread/atomic_int.h

@@ -45,31 +45,31 @@ struct AtomicInt
 
 	int load() const
 	{
-		#if CROWN_PLATFORM_POSIX && CROWN_COMPILER_GCC
+#if CROWN_PLATFORM_POSIX && CROWN_COMPILER_GCC
 		__sync_fetch_and_add(&m_val, 0);
 		return m_val;
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		InterlockedExchangeAdd(&m_val, (int32_t)0);
 		return m_val;
-		#endif
+#endif
 	}
 
 	void store(int val)
 	{
-		#if CROWN_PLATFORM_POSIX && CROWN_COMPILER_GCC
+#if CROWN_PLATFORM_POSIX && CROWN_COMPILER_GCC
 		__sync_lock_test_and_set(&m_val, val);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		InterlockedExchange(&m_val, val);
-		#endif
+#endif
 	}
 
 private:
 
-	#if CROWN_PLATFORM_POSIX && CROWN_COMPILER_GCC
+#if CROWN_PLATFORM_POSIX && CROWN_COMPILER_GCC
 	mutable int m_val;
-	#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 	mutable LONG m_val;
-	#endif
+#endif
 };
 
 } // namespace crown

+ 15 - 15
engine/core/thread/mutex.h

@@ -43,7 +43,7 @@ struct Mutex
 {
 	Mutex()
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_mutexattr_init(&m_attr);
 		CE_ASSERT(result == 0, "pthread_mutexattr_init: errno = %d", result);
 		result = pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_ERRORCHECK);
@@ -51,55 +51,55 @@ struct Mutex
 		result = pthread_mutex_init(&m_mutex, &m_attr);
 		CE_ASSERT(result == 0, "pthread_mutex_init: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		InitializeCriticalSection(&m_cs);
-		#endif
+#endif
 	}
 
 	~Mutex()
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_mutex_destroy(&m_mutex);
 		CE_ASSERT(result == 0, "pthread_mutex_destroy: errno = %d", result);
 		result = pthread_mutexattr_destroy(&m_attr);
 		CE_ASSERT(result == 0, "pthread_mutexattr_destroy: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		DeleteCriticalSection(&m_cs);
-		#endif
+#endif
 
 	}
 
 	void lock()
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_mutex_lock(&m_mutex);
 		CE_ASSERT(result == 0, "pthread_mutex_lock: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		EnterCriticalSection(&m_cs);
-		#endif
+#endif
 	}
 
 	void unlock()
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_mutex_unlock(&m_mutex);
 		CE_ASSERT(result == 0, "pthread_mutex_unlock: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		LeaveCriticalSection(&m_cs);
-		#endif
+#endif
 	}
 
 public:
 
-	#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 	pthread_mutex_t m_mutex;
 	pthread_mutexattr_t m_attr;
-	#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 	CRITICAL_SECTION m_cs;
-	#endif
+#endif
 
 private:
 

+ 18 - 18
engine/core/thread/semaphore.h

@@ -43,37 +43,37 @@ namespace crown
 struct Semaphore
 {
 	Semaphore()
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		: m_count(0)
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		: m_handle(INVALID_HANDLE_VALUE)
-		#endif
+#endif
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_cond_init(&m_cond, NULL);
 		CE_ASSERT(result == 0, "pthread_cond_init: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		m_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
 		CE_ASSERT(m_handle != NULL, "Unable to create semaphore!");
 		CE_UNUSED(m_handle);
-		#endif
+#endif
 	}
 
 	~Semaphore()
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_cond_destroy(&m_cond);
 		CE_ASSERT(result == 0, "pthread_cond_destroy: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		CloseHandle(m_handle);
-		#endif
+#endif
 	}
 
 	void post(uint32_t count = 1)
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		m_mutex.lock();
 		for (uint32_t i = 0; i < count; i++)
 		{
@@ -84,14 +84,14 @@ struct Semaphore
 
 		m_count += count;
 		m_mutex.unlock();
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		ReleaseSemaphore(m_handle, count, NULL);
-		#endif
+#endif
 	}
 
 	void wait()
 	{
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		m_mutex.lock();
 		while (m_count <= 0)
 		{
@@ -102,23 +102,23 @@ struct Semaphore
 
 		m_count--;
 		m_mutex.unlock();
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		DWORD milliseconds = (0 > msecs) ? INFINITE : msecs;
 		DWORD result = WaitForSingleObject(m_handle, milliseconds);
 		CE_ASSERT(result == WAIT_OBJECT_0, "Semaphore can not signal!");
 		CE_UNUSED(result);
-		#endif
+#endif
 	}
 
 private:
 
-	#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 	Mutex m_mutex;
 	pthread_cond_t m_cond;
 	int32_t m_count;
-	#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 	HANDLE m_handle;
-	#endif
+#endif
 
 private:
 

+ 15 - 15
engine/core/thread/thread.h

@@ -47,11 +47,11 @@ typedef int32_t (*ThreadFunction)(void*);
 struct Thread
 {
 	Thread()
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		: m_handle(0)
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		: m_handle(INVALID_HANDLE_VALUE)
-		#endif
+#endif
 		, m_function(NULL)
 		, m_data(NULL)
 		, m_stack_size(0)
@@ -73,7 +73,7 @@ struct Thread
 		m_data = data;
 		m_stack_size = stack_size;
 
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		pthread_attr_t attr;
 		int result = pthread_attr_init(&attr);
 		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
@@ -92,10 +92,10 @@ struct Thread
 		result = pthread_attr_destroy(&attr);
 		CE_ASSERT(result == 0, "pthread_attr_destroy: errno = %d", result);
 		CE_UNUSED(result);
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		m_handle = CreateThread(NULL, stack_size, Thread::thread_proc, this, 0, NULL);
 		CE_ASSERT(m_handle != NULL, "Failed to create the thread '%s'", m_name);
-		#endif
+#endif
 
 		m_is_running = true;
 		m_sem.wait();
@@ -105,17 +105,17 @@ struct Thread
 	{
 		CE_ASSERT(m_is_running, "Thread is not running");
 
-		#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 		int result = pthread_join(m_handle, NULL);
 		CE_ASSERT(result == 0, "pthread_join: errno = %d", result);
 		CE_UNUSED(result);
 		m_handle = 0;
-		#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 		WaitForSingleObject(m_handle, INFINITE);
 		GetExitCodeThread(m_handle, &m_exit_code);
 		CloseHandle(m_handle);
 		m_handle = INVALID_HANDLE_VALUE;
-		#endif
+#endif
 
 		m_is_running = false;
 	}
@@ -133,29 +133,29 @@ private:
 		return m_function(m_data);
 	}
 
-	#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 	static void* thread_proc(void* arg)
 	{
 		static int32_t result = -1;
 		result = ((Thread*)arg)->run();
 		return (void*)&result;
 	}
-	#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 	static DWORD WINAPI OsThread::thread_proc(void* arg)
 	{
 		OsThread* thread = (OsThread*)arg;
 		int32_t result = thread->run();
 		return result;
 	}
-	#endif
+#endif
 
 private:
 
-	#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_POSIX
 	pthread_t m_handle;
-	#elif CROWN_PLATFORM_WINDOWS
+#elif CROWN_PLATFORM_WINDOWS
 	HANDLE m_handle;
-	#endif
+#endif
 
 	ThreadFunction m_function;
 	void* m_data;