diff options
| author | Konstantin Belousov <kib@FreeBSD.org> | 2018-08-17 18:34:07 +0000 |
|---|---|---|
| committer | Konstantin Belousov <kib@FreeBSD.org> | 2018-08-17 18:34:07 +0000 |
| commit | 4627d47bc83903d3bf89828e32f3a4c3322a31d2 (patch) | |
| tree | b86f0fb8f2543643954985070712dac20c322af4 /lib/libthr/thread | |
| parent | d9cf291382345563f5e5e97a0f8fe054998f5a6a (diff) | |
Notes
Diffstat (limited to 'lib/libthr/thread')
| -rw-r--r-- | lib/libthr/thread/thr_exit.c | 3 | ||||
| -rw-r--r-- | lib/libthr/thread/thr_info.c | 64 | ||||
| -rw-r--r-- | lib/libthr/thread/thr_private.h | 2 |
3 files changed, 58 insertions, 11 deletions
diff --git a/lib/libthr/thread/thr_exit.c b/lib/libthr/thread/thr_exit.c index 44f00a5c7204a..5c5a6bd766a31 100644 --- a/lib/libthr/thread/thr_exit.c +++ b/lib/libthr/thread/thr_exit.c @@ -280,6 +280,9 @@ exit_thread(void) { struct pthread *curthread = _get_curthread(); + free(curthread->name); + curthread->name = NULL; + /* Check if there is thread specific data: */ if (curthread->specific != NULL) { /* Run the thread-specific data destructors: */ diff --git a/lib/libthr/thread/thr_info.c b/lib/libthr/thread/thr_info.c index d932bb0c7692d..948ed3e00aab2 100644 --- a/lib/libthr/thread/thr_info.c +++ b/lib/libthr/thread/thr_info.c @@ -2,8 +2,12 @@ * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> + * Copyright (c) 2018 The FreeBSD Foundation * All rights reserved. * + * Portions of this software were developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -43,27 +47,65 @@ __FBSDID("$FreeBSD$"); __weak_reference(_pthread_set_name_np, pthread_set_name_np); +static void +thr_set_name_np(struct pthread *thread, const char *name) +{ + + free(thread->name); + thread->name = strdup(name); +} + /* Set the thread name for debug. */ void _pthread_set_name_np(pthread_t thread, const char *name) { - struct pthread *curthread = _get_curthread(); - int ret = 0; + struct pthread *curthread; + curthread = _get_curthread(); if (curthread == thread) { - if (thr_set_name(thread->tid, name)) - ret = errno; + THR_THREAD_LOCK(curthread, thread); + thr_set_name(thread->tid, name); + thr_set_name_np(thread, name); + THR_THREAD_UNLOCK(curthread, thread); } else { - if ((ret=_thr_find_thread(curthread, thread, 0)) == 0) { + if (_thr_find_thread(curthread, thread, 0) == 0) { if (thread->state != PS_DEAD) { - if (thr_set_name(thread->tid, name)) - ret = errno; + thr_set_name(thread->tid, name); + thr_set_name_np(thread, name); } THR_THREAD_UNLOCK(curthread, thread); } } -#if 0 - /* XXX should return error code. */ - return (ret); -#endif +} + +static void +thr_get_name_np(struct pthread *thread, char *buf, size_t len) +{ + + if (thread->name != NULL) + strlcpy(buf, thread->name, len); + else if (len > 0) + buf[0] = '\0'; +} + +__weak_reference(_pthread_get_name_np, pthread_get_name_np); + +void +_pthread_get_name_np(pthread_t thread, char *buf, size_t len) +{ + struct pthread *curthread; + + curthread = _get_curthread(); + if (curthread == thread) { + THR_THREAD_LOCK(curthread, thread); + thr_get_name_np(thread, buf, len); + THR_THREAD_UNLOCK(curthread, thread); + } else { + if (_thr_find_thread(curthread, thread, 0) == 0) { + if (thread->state != PS_DEAD) + thr_get_name_np(thread, buf, len); + THR_THREAD_UNLOCK(curthread, thread); + } else if (len > 0) + buf[0] = '\0'; + } } diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index b6ba174accc93..eac8880b5d739 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -572,6 +572,8 @@ struct pthread { /* Sleep queue */ struct sleepqueue *sleepqueue; + /* pthread_set/get_name_np */ + char *name; }; #define THR_SHOULD_GC(thrd) \ |
