summaryrefslogtreecommitdiff
path: root/threadproc
diff options
context:
space:
mode:
authorIvan Zhakov <ivan@apache.org>2023-01-21 16:13:27 +0000
committerIvan Zhakov <ivan@apache.org>2023-01-21 16:13:27 +0000
commit9b4df62cd1117d6b4348c8a1f7871ece024eb413 (patch)
tree6b009aab810e5a22ec6ff0c167af99b0dd5e733e /threadproc
parent545069e7dc77a47168c0667bd7080db008d402de (diff)
parent166a06a11e80a5479a806ebb5a213d1c3e547fc7 (diff)
downloadapr-9b4df62cd1117d6b4348c8a1f7871ece024eb413.tar.gz
Merge thread-name branch (PR 60587) [1]:
* Introduce apr_thread_name_set() and apr_thread_name_get(). [1] https://bz.apache.org/bugzilla/show_bug.cgi?id=60587 [2] https://lists.apache.org/thread/z24logzc6v8tc0p2q3375cc10qo9y5yw git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1906889 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'threadproc')
-rw-r--r--threadproc/beos/thread.c14
-rw-r--r--threadproc/netware/thread.c12
-rw-r--r--threadproc/os2/thread.c14
-rw-r--r--threadproc/unix/thread.c55
-rw-r--r--threadproc/win32/thread.c72
5 files changed, 167 insertions, 0 deletions
diff --git a/threadproc/beos/thread.c b/threadproc/beos/thread.c
index 4f9d0408e..bf297330f 100644
--- a/threadproc/beos/thread.c
+++ b/threadproc/beos/thread.c
@@ -345,3 +345,17 @@ 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_name_set(const char* name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+
+APR_DECLARE(apr_status_t) apr_thread_name_get(apr_thread_t *thread,
+ char **name,
+ apr_pool_t pool)
+{
+ return APR_ENOTIMPL;
+}
diff --git a/threadproc/netware/thread.c b/threadproc/netware/thread.c
index 8d3bfd2b0..822d2e24f 100644
--- a/threadproc/netware/thread.c
+++ b/threadproc/netware/thread.c
@@ -356,4 +356,16 @@ 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_name_set(const char *name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+APR_DECLARE(apr_status_t) apr_thread_name_get(char ** name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+} \ No newline at end of file
diff --git a/threadproc/os2/thread.c b/threadproc/os2/thread.c
index 32acb3704..1ea7954dc 100644
--- a/threadproc/os2/thread.c
+++ b/threadproc/os2/thread.c
@@ -261,6 +261,20 @@ APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
return APR_SUCCESS;
}
+APR_DECLARE(apr_status_t) apr_thread_name_set(const char *name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+
+APR_DECLARE(apr_status_t) apr_thread_name_get(char ** name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ return APR_ENOTIMPL;
+}
+
void apr_thread_yield()
diff --git a/threadproc/unix/thread.c b/threadproc/unix/thread.c
index d90d05f46..70a7c5d30 100644
--- a/threadproc/unix/thread.c
+++ b/threadproc/unix/thread.c
@@ -22,6 +22,11 @@
#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_name_get and
+ apr_thread_name_set functions */
+#define TASK_COMM_LEN 16
+
/* Destroy the threadattr object */
static apr_status_t threadattr_cleanup(void *data)
{
@@ -279,6 +284,56 @@ APR_DECLARE(apr_thread_t *) apr_thread_current(void)
#endif
}
+APR_DECLARE(apr_status_t) apr_thread_name_set(const char *name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+#if HAVE_PTHREAD_SETNAME_NP
+ pthread_t td;
+
+ size_t name_len;
+ if (!name) {
+ return APR_BADARG;
+ }
+
+ if (thread) {
+ td = *thread->td;
+ }
+ else {
+ td = pthread_self();
+ }
+
+ name_len = strlen(name);
+ if (name_len >= TASK_COMM_LEN) {
+ name = name + name_len - TASK_COMM_LEN + 1;
+ }
+
+ return pthread_setname_np(td, name);
+#else
+ return APR_ENOTIMPL;
+#endif
+}
+
+APR_DECLARE(apr_status_t) apr_thread_name_get(char **name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+#if HAVE_PTHREAD_SETNAME_NP
+ pthread_t td;
+ if (thread) {
+ td = *thread->td;
+ }
+ else {
+ td = pthread_self();
+ }
+
+ *name = apr_pcalloc(pool, TASK_COMM_LEN);
+ return pthread_getname_np(td, *name, TASK_COMM_LEN);
+#else
+ return APR_ENOTIMPL;
+#endif
+}
+
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 9d1a0f5d3..502c317a8 100644
--- a/threadproc/win32/thread.c
+++ b/threadproc/win32/thread.c
@@ -24,6 +24,7 @@
#include <process.h>
#endif
#include "apr_arch_misc.h"
+#include "apr_arch_utf8.h"
/* Chosen for us by apr_initialize */
DWORD tls_apr_thread = 0;
@@ -303,6 +304,77 @@ APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
return apr_pool_userdata_set(data, key, cleanup, thread->pool);
}
+APR_DECLARE(apr_status_t) apr_thread_name_set(const char *name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ apr_wchar_t *wname;
+ apr_size_t wname_len;
+ apr_size_t name_len;
+ apr_status_t rv;
+ HANDLE thread_handle;
+
+ if (!APR_HAVE_LATE_DLL_FUNC(SetThreadDescription)) {
+ return APR_ENOTIMPL;
+ }
+
+ if (thread) {
+ thread_handle = thread->td;
+ }
+ else {
+ thread_handle = GetCurrentThread();
+ }
+
+ name_len = strlen(name) + 1;
+ wname_len = name_len;
+ wname = apr_palloc(pool, wname_len * sizeof(apr_wchar_t));
+ rv = apr_conv_utf8_to_ucs2(name, &name_len, wname, &wname_len);
+ if (rv) {
+ return rv;
+ }
+
+ if (!apr_winapi_SetThreadDescription(thread_handle, wname)) {
+ return apr_get_os_error();
+ }
+
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_thread_name_get(char **name,
+ apr_thread_t *thread,
+ apr_pool_t *pool)
+{
+ apr_wchar_t *wname;
+ apr_size_t wname_len;
+ apr_size_t name_len;
+ apr_status_t rv;
+ HANDLE thread_handle;
+
+ if (!APR_HAVE_LATE_DLL_FUNC(GetThreadDescription)) {
+ return APR_ENOTIMPL;
+ }
+
+ if (thread) {
+ thread_handle = thread->td;
+ }
+ else {
+ thread_handle = GetCurrentThread();
+ }
+
+ if (!apr_winapi_GetThreadDescription(thread_handle, &wname)) {
+ return apr_get_os_error();
+ }
+
+ wname_len = wcslen(wname) + 1;
+
+ name_len = wname_len * 3;
+ *name = apr_palloc(pool, name_len);
+
+ rv = apr_conv_ucs2_to_utf8(wname, &wname_len, *name, &name_len);
+ LocalFree(wname);
+
+ return rv;
+}
APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
{