On Tue, Jun 28, 2022 at 1:13 AM <i...@apache.org> wrote: > > Author: ivan > Date: Mon Jun 27 23:13:52 2022 > New Revision: 1902297 > > URL: http://svn.apache.org/viewvc?rev=1902297&view=rev > Log: > On 'thread-name' branch: Add apr_thread_name_get() and apr_thread_name_set() > API to get/set thread name. > [] > --- apr/apr/branches/thread-name/threadproc/unix/thread.c (original) > +++ apr/apr/branches/thread-name/threadproc/unix/thread.c Mon Jun 27 23:13:52 > 2022 [] > @@ -277,6 +282,48 @@ APR_DECLARE(apr_thread_t *) apr_thread_c > #endif > } > > +APR_DECLARE(apr_status_t) apr_thread_name_set(const char *name, > + apr_thread_t *thread, > + apr_pool_t *pool) > +{ > + 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);
We probably need to check for HAVE_PTHREAD_SETNAME_NP since it's _np (non-portable). Some systems seem to implement "prctl(PR_SET_NAME, ...)" too, but that's for pthread_self() only.. > +} > + > +APR_DECLARE(apr_status_t) apr_thread_name_get(char **name, > + apr_thread_t *thread, > + apr_pool_t *pool) > +{ > + 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); Likewise for HAVE_PTHREAD_GETNAME_NP/prctl(PR_GET_NAME). > +}