Browse Source

Merge pull request #10552 from RandomShaper/improve-posix

Improve Mac/UNIX conformance/reliability
Rémi Verschelde 8 years ago
parent
commit
9a8a0e20e5

+ 3 - 1
drivers/unix/os_unix.cpp

@@ -315,7 +315,9 @@ OS::TimeZoneInfo OS_Unix::get_time_zone_info() const {
 
 
 void OS_Unix::delay_usec(uint32_t p_usec) const {
 void OS_Unix::delay_usec(uint32_t p_usec) const {
 
 
-	usleep(p_usec);
+	struct timespec rem = { p_usec / 1000000, (p_usec % 1000000) * 1000 };
+	while (nanosleep(&rem, &rem) == EINTR) {
+	}
 }
 }
 uint64_t OS_Unix::get_ticks_usec() const {
 uint64_t OS_Unix::get_ticks_usec() const {
 
 

+ 13 - 2
drivers/unix/thread_posix.cpp

@@ -36,8 +36,18 @@
 #include <pthread_np.h>
 #include <pthread_np.h>
 #endif
 #endif
 
 
+#include "core/safe_refcount.h"
 #include "os/memory.h"
 #include "os/memory.h"
 
 
+static pthread_key_t _create_thread_id_key() {
+	pthread_key_t key;
+	pthread_key_create(&key, NULL);
+	return key;
+}
+
+pthread_key_t ThreadPosix::thread_id_key = _create_thread_id_key();
+Thread::ID ThreadPosix::next_thread_id = 0;
+
 Thread::ID ThreadPosix::get_id() const {
 Thread::ID ThreadPosix::get_id() const {
 
 
 	return id;
 	return id;
@@ -51,7 +61,8 @@ Thread *ThreadPosix::create_thread_posix() {
 void *ThreadPosix::thread_callback(void *userdata) {
 void *ThreadPosix::thread_callback(void *userdata) {
 
 
 	ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
 	ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
-	t->id = (ID)pthread_self();
+	t->id = atomic_increment(&next_thread_id);
+	pthread_setspecific(thread_id_key, (void *)t->id);
 
 
 	ScriptServer::thread_enter(); //scripts may need to attach a stack
 	ScriptServer::thread_enter(); //scripts may need to attach a stack
 
 
@@ -77,7 +88,7 @@ Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_
 }
 }
 Thread::ID ThreadPosix::get_thread_id_func_posix() {
 Thread::ID ThreadPosix::get_thread_id_func_posix() {
 
 
-	return (ID)pthread_self();
+	return (ID)pthread_getspecific(thread_id_key);
 }
 }
 void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
 void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
 
 

+ 3 - 0
drivers/unix/thread_posix.h

@@ -42,6 +42,9 @@
 
 
 class ThreadPosix : public Thread {
 class ThreadPosix : public Thread {
 
 
+	static pthread_key_t thread_id_key;
+	static ID next_thread_id;
+
 	pthread_t pthread;
 	pthread_t pthread;
 	pthread_attr_t pthread_attr;
 	pthread_attr_t pthread_attr;
 	ThreadCreateCallback callback;
 	ThreadCreateCallback callback;

+ 13 - 2
platform/android/thread_jandroid.cpp

@@ -29,9 +29,19 @@
 /*************************************************************************/
 /*************************************************************************/
 #include "thread_jandroid.h"
 #include "thread_jandroid.h"
 
 
+#include "core/safe_refcount.h"
 #include "os/memory.h"
 #include "os/memory.h"
 #include "script_language.h"
 #include "script_language.h"
 
 
+static pthread_key_t _create_thread_id_key() {
+	pthread_key_t key;
+	pthread_key_create(&key, NULL);
+	return key;
+}
+
+pthread_key_t ThreadAndroid::thread_id_key = _create_thread_id_key();
+Thread::ID ThreadAndroid::next_thread_id = 0;
+
 Thread::ID ThreadAndroid::get_id() const {
 Thread::ID ThreadAndroid::get_id() const {
 
 
 	return id;
 	return id;
@@ -47,7 +57,8 @@ void *ThreadAndroid::thread_callback(void *userdata) {
 	ThreadAndroid *t = reinterpret_cast<ThreadAndroid *>(userdata);
 	ThreadAndroid *t = reinterpret_cast<ThreadAndroid *>(userdata);
 	setup_thread();
 	setup_thread();
 	ScriptServer::thread_enter(); //scripts may need to attach a stack
 	ScriptServer::thread_enter(); //scripts may need to attach a stack
-	t->id = (ID)pthread_self();
+	t->id = atomic_increment(&next_thread_id);
+	pthread_setspecific(thread_id_key, (void *)t->id);
 	t->callback(t->user);
 	t->callback(t->user);
 	ScriptServer::thread_exit();
 	ScriptServer::thread_exit();
 	return NULL;
 	return NULL;
@@ -68,7 +79,7 @@ Thread *ThreadAndroid::create_func_jandroid(ThreadCreateCallback p_callback, voi
 
 
 Thread::ID ThreadAndroid::get_thread_id_func_jandroid() {
 Thread::ID ThreadAndroid::get_thread_id_func_jandroid() {
 
 
-	return (ID)pthread_self();
+	return (ID)pthread_getspecific(thread_id_key);
 }
 }
 
 
 void ThreadAndroid::wait_to_finish_func_jandroid(Thread *p_thread) {
 void ThreadAndroid::wait_to_finish_func_jandroid(Thread *p_thread) {

+ 3 - 0
platform/android/thread_jandroid.h

@@ -41,6 +41,9 @@
 
 
 class ThreadAndroid : public Thread {
 class ThreadAndroid : public Thread {
 
 
+	static pthread_key_t thread_id_key;
+	static ID next_thread_id;
+
 	pthread_t pthread;
 	pthread_t pthread;
 	pthread_attr_t pthread_attr;
 	pthread_attr_t pthread_attr;
 	ThreadCreateCallback callback;
 	ThreadCreateCallback callback;