From 3205631c96e14334b572412be6a6a5ff84c1050d Mon Sep 17 00:00:00 2001 From: Sergey Safarov Date: Sun, 15 Jan 2017 10:19:04 -0500 Subject: [PATCH] Added switch_thread_getname and switch_thread_setname functions --- include/apr_thread_proc.h | 14 ++++++++++++++ threadproc/beos/thread.c | 10 ++++++++++ threadproc/netware/thread.c | 8 ++++++++ threadproc/os2/thread.c | 12 ++++++++++-- threadproc/unix/thread.c | 24 ++++++++++++++++++++++++ threadproc/win32/thread.c | 10 ++++++++++ 6 files changed, 76 insertions(+), 2 deletions(-) diff --git a/include/apr_thread_proc.h b/include/apr_thread_proc.h index a09bdb0bf49..da890ff9cc4 100644 --- a/include/apr_thread_proc.h +++ b/include/apr_thread_proc.h @@ -277,6 +277,13 @@ APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new_thread, APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval); +/** + * Get name of thread + * @param thread The thread that name required to get. + * @param name The variable where is will be stored name of thread. + */ +APR_DECLARE(apr_status_t) apr_thread_getname(apr_thread_t *thread, char ** name); + /** * block until the desired thread stops executing. * @param retval The return value from the dead thread. @@ -285,6 +292,13 @@ APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd); +/** + * Set name of thread + * @param thread The thread that name will be changed. + * @param name The name of thread must be setted. If name is to long, then name stripped to max length supported by operation system. + */ +APR_DECLARE(apr_status_t) apr_thread_setname(apr_thread_t *thread, const char *name); + /** * force the current thread to yield the processor */ diff --git a/threadproc/beos/thread.c b/threadproc/beos/thread.c index 01bc7a9732a..7b4e5f4083f 100644 --- a/threadproc/beos/thread.c +++ b/threadproc/beos/thread.c @@ -240,3 +240,13 @@ APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, } APR_POOL_IMPLEMENT_ACCESSOR(thread) + +APR_DECLARE(apr_status_t) apr_thread_setname(apr_thread_t *thread, const char *name) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_thread_getname(apr_thread_t *thread, char ** name) +{ + return APR_ENOTIMPL; +} diff --git a/threadproc/netware/thread.c b/threadproc/netware/thread.c index a37b107a02e..1afc9964c72 100644 --- a/threadproc/netware/thread.c +++ b/threadproc/netware/thread.c @@ -256,4 +256,12 @@ APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, APR_POOL_IMPLEMENT_ACCESSOR(thread) +APR_DECLARE(apr_status_t) apr_thread_setname(apr_thread_t *thread, const char *name) +{ + return APR_ENOTIMPL; +} +APR_DECLARE(apr_status_t) apr_thread_getname(apr_thread_t *thread, char ** name) +{ + return APR_ENOTIMPL; +} \ No newline at end of file diff --git a/threadproc/os2/thread.c b/threadproc/os2/thread.c index 9911034ae7c..ecdae827462 100644 --- a/threadproc/os2/thread.c +++ b/threadproc/os2/thread.c @@ -74,8 +74,6 @@ static void apr_thread_begin(void *arg) apr_pool_destroy(thread->pool); } - - APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr, apr_thread_start_t func, void *data, apr_pool_t *pool) @@ -261,3 +259,13 @@ APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, return APR_SUCCESS; } + +APR_DECLARE(apr_status_t) apr_thread_setname(apr_thread_t *thread, const char *name) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_thread_getname(apr_thread_t *thread, char ** name) +{ + return APR_ENOTIMPL; +} diff --git a/threadproc/unix/thread.c b/threadproc/unix/thread.c index dcef500e9f7..5de418ee51e 100644 --- a/threadproc/unix/thread.c +++ b/threadproc/unix/thread.c @@ -22,6 +22,10 @@ #if APR_HAVE_PTHREAD_H +/* Unfortunately the kernel headers do not export the TASK_COMM_LEN + macro. So we have to define it here. Used in apr_thread_getname and apr_thread_setname functions */ +#define TASK_COMM_LEN 16 + /* Destroy the threadattr object */ static apr_status_t threadattr_cleanup(void *data) { @@ -193,6 +197,26 @@ APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, } } +APR_DECLARE(apr_status_t) apr_thread_setname(apr_thread_t *thread, const char *name) +{ + size_t name_len; + if (!name) { + return APR_BADARG; + } + + name_len = strlen (name); + if ( name_len >= TASK_COMM_LEN) { + name = name + name_len - TASK_COMM_LEN + 1; + } + return pthread_setname_np(*thread->td, name); +} + +APR_DECLARE(apr_status_t) apr_thread_getname(apr_thread_t *thread, char ** name) +{ + *name = apr_pcalloc(thread->pool, TASK_COMM_LEN); + return pthread_getname_np(*thread->td, *name, TASK_COMM_LEN); +} + APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void) { return pthread_self(); diff --git a/threadproc/win32/thread.c b/threadproc/win32/thread.c index c713e1444f3..caa7f6e75b0 100644 --- a/threadproc/win32/thread.c +++ b/threadproc/win32/thread.c @@ -284,3 +284,13 @@ APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1, } APR_POOL_IMPLEMENT_ACCESSOR(thread) + +APR_DECLARE(apr_status_t) apr_thread_setname(apr_thread_t *thread, const char *name) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_thread_getname(apr_thread_t *thread, char ** name) +{ + return APR_ENOTIMPL; +}