thread renaming

This commit is contained in:
Ariel Manzur 2016-01-31 20:22:38 -03:00
parent b406aae790
commit 0cd8c054a2
7 changed files with 28 additions and 39 deletions

View File

@ -390,6 +390,12 @@ bool _OS::is_ok_left_and_cancel_right() const {
return OS::get_singleton()->get_swap_ok_cancel(); return OS::get_singleton()->get_swap_ok_cancel();
} }
Error _OS::set_thread_name(const String& p_name) {
return Thread::set_name(p_name);
};
/* /*
enum Weekday { enum Weekday {
DAY_SUNDAY, DAY_SUNDAY,
@ -877,6 +883,8 @@ void _OS::_bind_methods() {
ObjectTypeDB::bind_method(_MD("alert","text","title"),&_OS::alert,DEFVAL("Alert!")); ObjectTypeDB::bind_method(_MD("alert","text","title"),&_OS::alert,DEFVAL("Alert!"));
ObjectTypeDB::bind_method(_MD("set_thread_name","name"),&_OS::set_thread_name);
BIND_CONSTANT( DAY_SUNDAY ); BIND_CONSTANT( DAY_SUNDAY );
BIND_CONSTANT( DAY_MONDAY ); BIND_CONSTANT( DAY_MONDAY );
@ -1895,13 +1903,7 @@ void _Thread::_start_func(void *ud) {
Variant::CallError ce; Variant::CallError ce;
const Variant* arg[1]={&t->userdata}; const Variant* arg[1]={&t->userdata};
// we don't know our thread pointer yet :( Thread::set_name(t->target_method);
if (t->name == "") {
// come up with a better name using maybe the filename on the Script?
//t->thread->set_name(t->target_method);
} else {
//t->thread->set_name(t->name);
};
t->ret=t->target_instance->call(t->target_method,arg,1,ce); t->ret=t->target_instance->call(t->target_method,arg,1,ce);
if (ce.error!=Variant::CallError::CALL_OK) { if (ce.error!=Variant::CallError::CALL_OK) {
@ -1992,24 +1994,12 @@ Variant _Thread::wait_to_finish() {
return r; return r;
} }
Error _Thread::set_name(const String &p_name) {
name = p_name;
if (thread) {
return thread->set_name(p_name);
};
return OK;
};
void _Thread::_bind_methods() { void _Thread::_bind_methods() {
ObjectTypeDB::bind_method(_MD("start:Error","instance","method","userdata","priority"),&_Thread::start,DEFVAL(Variant()),DEFVAL(PRIORITY_NORMAL)); ObjectTypeDB::bind_method(_MD("start:Error","instance","method","userdata","priority"),&_Thread::start,DEFVAL(Variant()),DEFVAL(PRIORITY_NORMAL));
ObjectTypeDB::bind_method(_MD("get_id"),&_Thread::get_id); ObjectTypeDB::bind_method(_MD("get_id"),&_Thread::get_id);
ObjectTypeDB::bind_method(_MD("is_active"),&_Thread::is_active); ObjectTypeDB::bind_method(_MD("is_active"),&_Thread::is_active);
ObjectTypeDB::bind_method(_MD("wait_to_finish:Variant"),&_Thread::wait_to_finish); ObjectTypeDB::bind_method(_MD("wait_to_finish:Variant"),&_Thread::wait_to_finish);
ObjectTypeDB::bind_method(_MD("set_name:Error", "name"),&_Thread::set_name);
BIND_CONSTANT( PRIORITY_LOW ); BIND_CONSTANT( PRIORITY_LOW );
BIND_CONSTANT( PRIORITY_NORMAL ); BIND_CONSTANT( PRIORITY_NORMAL );

View File

@ -270,6 +270,8 @@ public:
bool is_ok_left_and_cancel_right() const; bool is_ok_left_and_cancel_right() const;
Error set_thread_name(const String& p_name);
static _OS *get_singleton() { return singleton; } static _OS *get_singleton() { return singleton; }
_OS(); _OS();
@ -512,7 +514,6 @@ protected:
Object *target_instance; Object *target_instance;
StringName target_method; StringName target_method;
Thread *thread; Thread *thread;
String name;
static void _bind_methods(); static void _bind_methods();
static void _start_func(void *ud); static void _start_func(void *ud);
public: public:
@ -528,7 +529,6 @@ public:
String get_id() const; String get_id() const;
bool is_active() const; bool is_active() const;
Variant wait_to_finish(); Variant wait_to_finish();
Error set_name(const String& p_name);
_Thread(); _Thread();
~_Thread(); ~_Thread();

View File

@ -32,6 +32,7 @@
Thread* (*Thread::create_func)(ThreadCreateCallback,void *,const Settings&)=NULL; Thread* (*Thread::create_func)(ThreadCreateCallback,void *,const Settings&)=NULL;
Thread::ID (*Thread::get_thread_ID_func)()=NULL; Thread::ID (*Thread::get_thread_ID_func)()=NULL;
void (*Thread::wait_to_finish_func)(Thread*)=NULL; void (*Thread::wait_to_finish_func)(Thread*)=NULL;
Error (*Thread::set_name_func)(const String&)=NULL;
Thread::ID Thread::_main_thread_id=0; Thread::ID Thread::_main_thread_id=0;
@ -60,6 +61,9 @@ void Thread::wait_to_finish(Thread *p_thread) {
Error Thread::set_name(const String &p_name) { Error Thread::set_name(const String &p_name) {
if (set_name_func)
return set_name_func(p_name);
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
}; };

View File

@ -63,6 +63,7 @@ protected:
static Thread* (*create_func)(ThreadCreateCallback p_callback,void *,const Settings&); static Thread* (*create_func)(ThreadCreateCallback p_callback,void *,const Settings&);
static ID (*get_thread_ID_func)(); static ID (*get_thread_ID_func)();
static void (*wait_to_finish_func)(Thread*); static void (*wait_to_finish_func)(Thread*);
static Error (*set_name_func)(const String&);
friend class Main; friend class Main;
@ -73,10 +74,9 @@ protected:
public: public:
virtual Error set_name(const String& p_name);
virtual ID get_ID() const=0; virtual ID get_ID() const=0;
static Error set_name(const String &p_name);
_FORCE_INLINE_ static ID get_main_ID() { return _main_thread_id; } ///< get the ID of the main thread _FORCE_INLINE_ static ID get_main_ID() { return _main_thread_id; } ///< get the ID of the main thread
static ID get_caller_ID(); ///< get the ID of the caller function ID static ID get_caller_ID(); ///< get the ID of the caller function ID
static void wait_to_finish(Thread *p_thread); ///< waits until thread is finished, and deallocates it. static void wait_to_finish(Thread *p_thread); ///< waits until thread is finished, and deallocates it.

View File

@ -81,9 +81,9 @@ void ThreadPosix::wait_to_finish_func_posix(Thread* p_thread) {
tp->pthread=0; tp->pthread=0;
} }
Error ThreadPosix::set_name(const String& p_name) { Error ThreadPosix::set_name_func_posix(const String& p_name) {
ERR_FAIL_COND_V(pthread == 0, ERR_UNCONFIGURED); pthread_t running_thread = pthread_self();
#ifdef PTHREAD_NO_RENAME #ifdef PTHREAD_NO_RENAME
return ERR_UNAVAILABLE; return ERR_UNAVAILABLE;
@ -93,22 +93,15 @@ Error ThreadPosix::set_name(const String& p_name) {
#ifdef PTHREAD_RENAME_SELF #ifdef PTHREAD_RENAME_SELF
// check if thread is the same as caller // check if thread is the same as caller
int caller = Thread::get_caller_ID();
int self = get_ID();
if (caller != self) {
ERR_EXPLAIN("On this platform, thread can only be renamed with calls from the threads to be renamed.");
ERR_FAIL_V(ERR_UNAVAILABLE);
return ERR_UNAVAILABLE;
};
int err = pthread_setname_np(p_name.utf8().get_data()); int err = pthread_setname_np(p_name.utf8().get_data());
#else #else
#ifdef PTHREAD_BSD_SET_NAME #ifdef PTHREAD_BSD_SET_NAME
pthread_set_name_np(pthread, p_name.utf8().get_data()); pthread_set_name_np(running_thread, p_name.utf8().get_data());
int err = 0; // Open/FreeBSD ignore errors in this function int err = 0; // Open/FreeBSD ignore errors in this function
#else #else
int err = pthread_setname_np(pthread, p_name.utf8().get_data()); int err = pthread_setname_np(running_thread, p_name.utf8().get_data());
#endif // PTHREAD_BSD_SET_NAME #endif // PTHREAD_BSD_SET_NAME
#endif // PTHREAD_RENAME_SELF #endif // PTHREAD_RENAME_SELF
@ -123,7 +116,7 @@ void ThreadPosix::make_default() {
create_func=create_func_posix; create_func=create_func_posix;
get_thread_ID_func=get_thread_ID_func_posix; get_thread_ID_func=get_thread_ID_func_posix;
wait_to_finish_func=wait_to_finish_func_posix; wait_to_finish_func=wait_to_finish_func_posix;
set_name_func = set_name_func_posix;
} }
ThreadPosix::ThreadPosix() { ThreadPosix::ThreadPosix() {

View File

@ -55,13 +55,14 @@ class ThreadPosix : public Thread {
static Thread* create_func_posix(ThreadCreateCallback p_callback,void *,const Settings&); static Thread* create_func_posix(ThreadCreateCallback p_callback,void *,const Settings&);
static ID get_thread_ID_func_posix(); static ID get_thread_ID_func_posix();
static void wait_to_finish_func_posix(Thread* p_thread); static void wait_to_finish_func_posix(Thread* p_thread);
static Error set_name_func_posix(const String& p_name);
ThreadPosix(); ThreadPosix();
public: public:
virtual ID get_ID() const; virtual ID get_ID() const;
Error set_name(const String& p_name);
static void make_default(); static void make_default();

View File

@ -765,6 +765,8 @@ void AudioServerSW::free(RID p_id) {
void AudioServerSW::_thread_func(void *self) { void AudioServerSW::_thread_func(void *self) {
Thread::set_name("AudioServerSW");
AudioServerSW *as=(AudioServerSW *)self; AudioServerSW *as=(AudioServerSW *)self;
while (!as->exit_update_thread) { while (!as->exit_update_thread) {
@ -807,7 +809,6 @@ void AudioServerSW::init() {
#ifndef NO_THREADS #ifndef NO_THREADS
exit_update_thread=false; exit_update_thread=false;
thread = Thread::create(_thread_func,this); thread = Thread::create(_thread_func,this);
thread->set_name("AudioServerSW");
#endif #endif
} }