]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthr/thread/thr_info.c
Add pthread_getname_np() and pthread_setname_np() aliases
[FreeBSD/FreeBSD.git] / lib / libthr / thread / thr_info.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Konstantin Belousov
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "namespace.h"
40 #include <stdlib.h>
41 #include <string.h>
42 #include <pthread.h>
43 #include <pthread_np.h>
44 #include "un-namespace.h"
45
46 #include "thr_private.h"
47
48 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
49 __weak_reference(_pthread_set_name_np, pthread_setname_np);
50
51 static void
52 thr_set_name_np(struct pthread *thread, const char *name)
53 {
54
55         free(thread->name);
56         thread->name = name != NULL ? strdup(name) : NULL;
57 }
58
59 /* Set the thread name for debug. */
60 void
61 _pthread_set_name_np(pthread_t thread, const char *name)
62 {
63         struct pthread *curthread;
64
65         curthread = _get_curthread();
66         if (curthread == thread) {
67                 THR_THREAD_LOCK(curthread, thread);
68                 thr_set_name(thread->tid, name);
69                 thr_set_name_np(thread, name);
70                 THR_THREAD_UNLOCK(curthread, thread);
71         } else {
72                 if (_thr_find_thread(curthread, thread, 0) == 0) {
73                         if (thread->state != PS_DEAD) {
74                                 thr_set_name(thread->tid, name);
75                                 thr_set_name_np(thread, name);
76                         }
77                         THR_THREAD_UNLOCK(curthread, thread);
78                 }
79         }
80 }
81
82 static void
83 thr_get_name_np(struct pthread *thread, char *buf, size_t len)
84 {
85
86         if (thread->name != NULL)
87                 strlcpy(buf, thread->name, len);
88         else if (len > 0)
89                 buf[0] = '\0';
90 }
91
92 __weak_reference(_pthread_get_name_np, pthread_get_name_np);
93 __weak_reference(_pthread_get_name_np, pthread_getname_np);
94
95 void
96 _pthread_get_name_np(pthread_t thread, char *buf, size_t len)
97 {
98         struct pthread *curthread;
99
100         curthread = _get_curthread();
101         if (curthread == thread) {
102                 THR_THREAD_LOCK(curthread, thread);
103                 thr_get_name_np(thread, buf, len);
104                 THR_THREAD_UNLOCK(curthread, thread);
105         } else {
106                 if (_thr_find_thread(curthread, thread, 0) == 0) {
107                         if (thread->state != PS_DEAD)
108                                 thr_get_name_np(thread, buf, len);
109                         THR_THREAD_UNLOCK(curthread, thread);
110                 } else if (len > 0)
111                         buf[0] = '\0';
112         }
113 }