Mercurial > libjeffpc
changeset 835:3d25e6c09e9f
thread: allow naming threads
This will make debugging a bit easier.
We truncate the names to 8 characters as a compromise between providing a
useful label and taking up too much screen/log/memory space.
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Wed, 03 Mar 2021 19:09:17 -0500 |
parents | 9819d9d0e08a |
children | ceb71a7a96f6 |
files | cmake/config.cmake include/jeffpc/config.h.in include/jeffpc/thread.h mapfile-vers thread.c |
diffstat | 5 files changed, 52 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/cmake/config.cmake Wed Mar 03 11:37:11 2021 -0500 +++ b/cmake/config.cmake Wed Mar 03 19:09:17 2021 -0500 @@ -73,6 +73,7 @@ check_function_exists(addrtosymstr JEFFPC_HAVE_ADDRTOSYMSTR) check_function_exists(pthread_cond_reltimedwait_np JEFFPC_HAVE_PTHREAD_COND_RELTIMEDWAIT_NP) +check_function_exists(pthread_setname_np JEFFPC_HAVE_PTHREAD_SETNAME_NP) check_function_exists(reallocarray JEFFPC_HAVE_REALLOCARRAY) check_function_exists(recallocarray JEFFPC_HAVE_RECALLOCARRAY) check_include_files(alloca.h JEFFPC_HAVE_ALLOCA_H)
--- a/include/jeffpc/config.h.in Wed Mar 03 11:37:11 2021 -0500 +++ b/include/jeffpc/config.h.in Wed Mar 03 19:09:17 2021 -0500 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2016-2021 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,6 +35,7 @@ #cmakedefine JEFFPC_HAVE_ASSFAIL #cmakedefine JEFFPC_HAVE_ADDRTOSYMSTR #cmakedefine JEFFPC_HAVE_PTHREAD_COND_RELTIMEDWAIT_NP +#cmakedefine JEFFPC_HAVE_PTHREAD_SETNAME_NP #cmakedefine JEFFPC_HAVE_REALLOCARRAY #cmakedefine JEFFPC_HAVE_RECALLOCARRAY #cmakedefine JEFFPC_HAVE_ALLOCA_H
--- a/include/jeffpc/thread.h Wed Mar 03 11:37:11 2021 -0500 +++ b/include/jeffpc/thread.h Wed Mar 03 19:09:17 2021 -0500 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2016-2021 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,8 +25,19 @@ #include <pthread.h> -extern int xthr_create(pthread_t *restrict thread, void *(*start)(void*), - void *restrict arg); +/* maximum length of thread names, anything longer will get truncated */ +#define XTHR_NAME_MAX_LEN 8 + +extern int xthr_create_named(pthread_t *restrict thread, const char *name, + void *(*start)(void *), void *restrict arg); +extern int xthr_set_name(const char *name); +extern const char *xthr_get_name(void); + +static inline int xthr_create(pthread_t *restrict thread, + void *(*start)(void *), void *restrict arg) +{ + return xthr_create_named(thread, "", start, arg); +} static inline int xthr_join(pthread_t thread, void **status) {
--- a/mapfile-vers Wed Mar 03 11:37:11 2021 -0500 +++ b/mapfile-vers Wed Mar 03 19:09:17 2021 -0500 @@ -296,7 +296,9 @@ taskq_wait; # thread - xthr_create; + xthr_create_named; + xthr_get_name; + xthr_set_name; # tree tree_destroy_nodes;
--- a/thread.c Wed Mar 03 11:37:11 2021 -0500 +++ b/thread.c Wed Mar 03 19:09:17 2021 -0500 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2016-2021 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,12 +24,37 @@ #include <jeffpc/synch.h> #include <jeffpc/error.h> #include <jeffpc/mem.h> +#include <jeffpc/cstr.h> struct xthr_info { + char name[XTHR_NAME_MAX_LEN + 1]; void *(*f)(void *); void *arg; }; +static __thread char thread_name[XTHR_NAME_MAX_LEN + 1]; + +int xthr_set_name(const char *name) +{ + int ret; + + strcpy_safe(thread_name, name ? name : "", sizeof(thread_name)); + +#ifdef JEFFPC_HAVE_PTHREAD_SETNAME_NP + ret = -pthread_setname_np(xthr_self(), thread_name); +#else + /* no known way to set OS thread name */ + ret = 0; +#endif + + return ret; +} + +const char *xthr_get_name(void) +{ + return thread_name; +} + static void *xthr_setup(void *_info) { struct xthr_info info = *((struct xthr_info *) _info); @@ -38,6 +63,9 @@ /* free early since the function may run for a very long time */ free(_info); + /* stash & set the thread name, but ignore errors */ + xthr_set_name(info.name); + ret = info.f(info.arg); lockdep_no_locks(); @@ -45,8 +73,8 @@ return ret; } -int xthr_create(pthread_t *restrict thread, void *(*start)(void*), - void *restrict arg) +int xthr_create_named(pthread_t *restrict thread, const char *name, + void *(*start)(void *), void *restrict arg) { struct xthr_info *info; pthread_t tmp; @@ -59,6 +87,7 @@ if (!info) return -ENOMEM; + strcpy_safe(info->name, name ? name : "", sizeof(info->name)); info->f = start; info->arg = arg;