summaryrefslogtreecommitdiff
path: root/drivers/unix/thread_posix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix/thread_posix.cpp')
-rw-r--r--drivers/unix/thread_posix.cpp28
1 files changed, 12 insertions, 16 deletions
diff --git a/drivers/unix/thread_posix.cpp b/drivers/unix/thread_posix.cpp
index c227aec6d6..5c7a546b29 100644
--- a/drivers/unix/thread_posix.cpp
+++ b/drivers/unix/thread_posix.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -29,17 +29,17 @@
/*************************************************************************/
#include "thread_posix.h"
-#include "core/script_language.h"
#if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(NO_THREADS)
+#include "core/object/script_language.h"
+#include "core/os/memory.h"
+#include "core/templates/safe_refcount.h"
+
#ifdef PTHREAD_BSD_SET_NAME
#include <pthread_np.h>
#endif
-#include "core/os/memory.h"
-#include "core/safe_refcount.h"
-
static void _thread_id_key_destr_callback(void *p_value) {
memdelete(static_cast<Thread::ID *>(p_value));
}
@@ -54,17 +54,14 @@ pthread_key_t ThreadPosix::thread_id_key = _create_thread_id_key();
Thread::ID ThreadPosix::next_thread_id = 0;
Thread::ID ThreadPosix::get_id() const {
-
return id;
}
Thread *ThreadPosix::create_thread_posix() {
-
return memnew(ThreadPosix);
}
void *ThreadPosix::thread_callback(void *userdata) {
-
ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
t->id = atomic_increment(&next_thread_id);
pthread_setspecific(thread_id_key, (void *)memnew(ID(t->id)));
@@ -79,7 +76,6 @@ void *ThreadPosix::thread_callback(void *userdata) {
}
Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_user, const Settings &) {
-
ThreadPosix *tr = memnew(ThreadPosix);
tr->callback = p_callback;
tr->user = p_user;
@@ -91,19 +87,20 @@ Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_
return tr;
}
-Thread::ID ThreadPosix::get_thread_id_func_posix() {
+Thread::ID ThreadPosix::get_thread_id_func_posix() {
void *value = pthread_getspecific(thread_id_key);
- if (value)
+ if (value) {
return *static_cast<ID *>(value);
+ }
ID new_id = atomic_increment(&next_thread_id);
pthread_setspecific(thread_id_key, (void *)memnew(ID(new_id)));
return new_id;
}
-void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
+void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
ThreadPosix *tp = static_cast<ThreadPosix *>(p_thread);
ERR_FAIL_COND(!tp);
ERR_FAIL_COND(tp->pthread == 0);
@@ -113,7 +110,6 @@ void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
}
Error ThreadPosix::set_name_func_posix(const String &p_name) {
-
#ifdef PTHREAD_NO_RENAME
return ERR_UNAVAILABLE;
@@ -130,6 +126,8 @@ Error ThreadPosix::set_name_func_posix(const String &p_name) {
#ifdef PTHREAD_BSD_SET_NAME
pthread_set_name_np(running_thread, p_name.utf8().get_data());
int err = 0; // Open/FreeBSD ignore errors in this function
+#elif defined(PTHREAD_NETBSD_SET_NAME)
+ int err = pthread_setname_np(running_thread, "%s", const_cast<char *>(p_name.utf8().get_data()));
#else
int err = pthread_setname_np(running_thread, p_name.utf8().get_data());
#endif // PTHREAD_BSD_SET_NAME
@@ -142,7 +140,6 @@ Error ThreadPosix::set_name_func_posix(const String &p_name) {
};
void ThreadPosix::make_default() {
-
create_func = create_func_posix;
get_thread_id_func = get_thread_id_func_posix;
wait_to_finish_func = wait_to_finish_func_posix;
@@ -150,7 +147,6 @@ void ThreadPosix::make_default() {
}
ThreadPosix::ThreadPosix() {
-
pthread = 0;
}