Skip to content

Commit

Permalink
al: rename pthread_set_name() to pthread_self_name_set() and remove f…
Browse files Browse the repository at this point in the history
…irst arg (pthread_t).
  • Loading branch information
rozhuk-im committed Apr 28, 2024
1 parent 96cdf87 commit bde84f2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
17 changes: 6 additions & 11 deletions include/al/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,21 @@ pthread_create_eagain(pthread_t *thread, const pthread_attr_t *attr,
}

static inline void
pthread_set_name(pthread_t thread, const char *name) {
pthread_self_name_set(const char *name) {

if (NULL == thread) {
thread = pthread_self();
}
if (NULL == name) {
name = "";
}
#ifdef HAVE_PTHREAD_SETNAME_NP
#ifdef __APPLE__ /* Ugly apple can not set names for other threads. */
if (pthread_self() == thread) {
pthread_setname_np(name);
}
#ifdef __APPLE__ /* Ugly apple can not set name for other threads. */
pthread_setname_np(name);
#elif defined(__NetBSD__)
pthread_setname_np(thread, "%s", name)
pthread_setname_np(pthread_self(), "%s", name)
#else
pthread_setname_np(thread, name);
pthread_setname_np(pthread_self(), name);
#endif
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
pthread_set_name_np(thread, name);
pthread_set_name_np(pthread_self(), name);
#endif
}

Expand Down
15 changes: 8 additions & 7 deletions src/threadpool/threadpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,6 @@ tp_destroy(tp_p tp) {
int
tp_threads_create(tp_p tp, int skip_first) {
tpt_p tpt;
char thr_name[64];

if (NULL == tp)
return (EINVAL);
Expand All @@ -1131,8 +1130,6 @@ tp_threads_create(tp_p tp, int skip_first) {
tpt->state = TP_THREAD_STATE_STARTING;
if (0 == pthread_create_eagain(&tpt->pt_id, NULL,
tp_thread_proc, tpt)) {
snprintf(thr_name, sizeof(thr_name), "%s: %zu", tp->name, i);
pthread_set_name(tpt->pt_id, thr_name);
} else {
tpt->state = TP_THREAD_STATE_STOP;
}
Expand Down Expand Up @@ -1174,6 +1171,7 @@ static void *
tp_thread_proc(void *data) {
tpt_p tpt = data;
sigset_t sig_set;
char thr_name[(TP_NAME_SIZE + 16)];

if (NULL == tpt) {
SYSLOGD_ERR(LOG_DEBUG, EINVAL, "Invalid data.");
Expand All @@ -1182,9 +1180,12 @@ tp_thread_proc(void *data) {

tpt->tp->threads_cnt ++;
tpt->state = TP_THREAD_STATE_RUNNING;
pthread_setspecific(tp_tls_key_tpt, (const void*)tpt);
syslog(LOG_INFO, "%s: thread %zu started...",

snprintf(thr_name, sizeof(thr_name), "%s: %zu",
tpt->tp->name, tpt->thread_num);
pthread_self_name_set(thr_name);
pthread_setspecific(tp_tls_key_tpt, (const void*)tpt);
syslog(LOG_INFO, "%s thread started...", thr_name);

sigemptyset(&sig_set);
sigaddset(&sig_set, SIGPIPE);
Expand Down Expand Up @@ -1214,9 +1215,9 @@ tp_thread_proc(void *data) {

tpt_loop(tpt);

syslog(LOG_INFO, "%s thread exited...", thr_name);
pthread_setspecific(tp_tls_key_tpt, NULL);
syslog(LOG_INFO, "%s: thread %zu exited...",
tpt->tp->name, tpt->thread_num);
pthread_self_name_set(NULL);
mem_bzero(&tpt->pt_id, sizeof(pthread_t));
tpt->state = TP_THREAD_STATE_STOP; /* Reset state on exit. */
tpt->tp->threads_cnt --;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ std_syslog_redirector_proc(void *data) {
syslog(LOG_DEBUG, "STD syslog redirector starting...");

/* Set thread name for better debugging. */
pthread_set_name(NULL, "STD syslog redirector");
pthread_self_name_set("STD syslog redirector");
/* Block PIPE signal. */
sigemptyset(&sig_set);
sigaddset(&sig_set, SIGPIPE);
Expand Down

0 comments on commit bde84f2

Please sign in to comment.