Sfoglia il codice sorgente

core: add Thread::exit_code()

Daniele Bartolini 7 anni fa
parent
commit
1ac7440889
3 ha cambiato i file con 32 aggiunte e 7 eliminazioni
  1. 12 7
      src/core/thread/thread.cpp
  2. 4 0
      src/core/thread/thread.h
  3. 16 0
      src/core/unit_tests.cpp

+ 12 - 7
src/core/thread/thread.cpp

@@ -27,19 +27,16 @@ struct Private
 #if CROWN_PLATFORM_POSIX
 static void* thread_proc(void* arg)
 {
-	static s32 result = -1;
 	Thread* thread = (Thread*)arg;
 	thread->_sem.post();
-	result = thread->_function(thread->_user_data);
-	return (void*)&result;
+	return (void*)(uintptr_t)thread->_function(thread->_user_data);
 }
 #elif CROWN_PLATFORM_WINDOWS
 static DWORD WINAPI thread_proc(void* arg)
 {
 	Thread* thread = (Thread*)arg;
 	thread->_sem.post();
-	s32 result = thread->_function(thread->_user_data);
-	return result;
+	return thread->_function(thread->_user_data);
 }
 #endif
 
@@ -47,6 +44,7 @@ Thread::Thread()
 	: _function(NULL)
 	, _user_data(NULL)
 	, _is_running(false)
+	, _exit_code(0)
 {
 	Private* priv = (Private*)_data;
 	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(Private));
@@ -106,13 +104,15 @@ void Thread::stop()
 	CE_ASSERT(_is_running, "Thread is not running");
 
 #if CROWN_PLATFORM_POSIX
-	int err = pthread_join(priv->handle, NULL);
+	void* retval;
+	int err = pthread_join(priv->handle, &retval);
 	CE_ASSERT(err == 0, "pthread_join: errno = %d", err);
 	CE_UNUSED(err);
+	_exit_code = (s32)(uintptr_t)retval;
 	priv->handle = 0;
 #elif CROWN_PLATFORM_WINDOWS
 	WaitForSingleObject(priv->handle, INFINITE);
-	// GetExitCodeThread(_handle, &m_exit_code);
+	GetExitCodeThread(_handle, (DWORD*)&_exit_code);
 	CloseHandle(priv->handle);
 	priv->handle = INVALID_HANDLE_VALUE;
 #endif
@@ -125,4 +125,9 @@ bool Thread::is_running()
 	return _is_running;
 }
 
+s32 Thread::exit_code()
+{
+	return _exit_code;
+}
+
 } // namespace crown

+ 4 - 0
src/core/thread/thread.h

@@ -24,6 +24,7 @@ struct Thread
 	void* _user_data;
 	Semaphore _sem;
 	bool _is_running;
+	s32 _exit_code;
 	CE_ALIGN_DECL(16, u8 _data[32]);
 
 	///
@@ -46,6 +47,9 @@ struct Thread
 
 	///
 	bool is_running();
+
+	///
+	s32 exit_code();
 };
 
 } // namespace crown

+ 16 - 0
src/core/unit_tests.cpp

@@ -31,6 +31,7 @@
 #include "core/strings/dynamic_string.h"
 #include "core/strings/string.h"
 #include "core/strings/string_id.h"
+#include "core/thread/thread.h"
 
 #define ENSURE(condition)                                \
 	do                                                   \
@@ -1252,6 +1253,20 @@ static void test_command_line()
 	ENSURE(orange != NULL && strcmp(orange, "orange") == 0);
 }
 
+static void test_thread()
+{
+	Thread thread;
+	ENSURE(!thread.is_running());
+
+	thread.start([](void*) { return 0; }, NULL);
+	thread.stop();
+	ENSURE(thread.exit_code() == 0);
+
+	thread.start([](void*) { return -1; }, NULL);
+	thread.stop();
+	ENSURE(thread.exit_code() == -1);
+}
+
 int main_unit_tests()
 {
 	test_memory();
@@ -1275,6 +1290,7 @@ int main_unit_tests()
 	test_sjson();
 	test_path();
 	test_command_line();
+	test_thread();
 
 	return EXIT_SUCCESS;
 }