Commit | Line | Data |
---|---|---|
cb8d0d24 MJ |
1 | /* |
2 | * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #ifndef _COMPAT_PTHREAD_H | |
9 | #define _COMPAT_PTHREAD_H | |
10 | ||
11 | #include <pthread.h> | |
edf4b93e | 12 | #include <common/compat/errno.h> |
014d7d3b MJ |
13 | #include <string.h> |
14 | ||
3f3e7eb7 MJ |
15 | #ifdef __FreeBSD__ |
16 | #include <pthread_np.h> | |
17 | #endif | |
18 | ||
014d7d3b | 19 | #define LTTNG_PTHREAD_NAMELEN 16 |
cb8d0d24 MJ |
20 | |
21 | #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) | |
22 | static inline | |
23 | int lttng_pthread_setname_np(const char *name) | |
24 | { | |
014d7d3b MJ |
25 | /* |
26 | * Some implementations don't error out, replicate this behavior for | |
27 | * consistency. | |
28 | */ | |
29 | if (strnlen(name, LTTNG_PTHREAD_NAMELEN) >= LTTNG_PTHREAD_NAMELEN) { | |
30 | return ERANGE; | |
31 | } | |
32 | ||
cb8d0d24 MJ |
33 | return pthread_setname_np(pthread_self(), name); |
34 | } | |
35 | #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) | |
36 | static inline | |
37 | int lttng_pthread_setname_np(const char *name) | |
38 | { | |
39 | return pthread_setname_np(name); | |
40 | } | |
014d7d3b MJ |
41 | #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID) |
42 | ||
014d7d3b MJ |
43 | static inline |
44 | int lttng_pthread_setname_np(const char *name) | |
45 | { | |
46 | /* Replicate pthread_setname_np's behavior. */ | |
47 | if (strnlen(name, LTTNG_PTHREAD_NAMELEN) >= LTTNG_PTHREAD_NAMELEN) { | |
48 | return ERANGE; | |
49 | } | |
50 | ||
51 | pthread_set_name_np(pthread_self(), name); | |
52 | return 0; | |
53 | } | |
014d7d3b MJ |
54 | #elif defined(__linux__) |
55 | ||
56 | /* Fallback on prtctl on Linux */ | |
57 | #include <sys/prctl.h> | |
58 | ||
59 | static inline | |
60 | int lttng_pthread_setname_np(const char *name) | |
61 | { | |
62 | /* Replicate pthread_setname_np's behavior. */ | |
63 | if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { | |
64 | return ERANGE; | |
65 | } | |
66 | return prctl(PR_SET_NAME, name, 0, 0, 0); | |
67 | } | |
cb8d0d24 MJ |
68 | #else |
69 | /* | |
70 | * For platforms without thread name support, do nothing. | |
71 | */ | |
72 | static inline | |
73 | int lttng_pthread_setname_np(const char *name) | |
74 | { | |
75 | return -ENOSYS; | |
76 | } | |
3f3e7eb7 MJ |
77 | #endif |
78 | ||
014d7d3b | 79 | |
3f3e7eb7 MJ |
80 | #if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID) |
81 | static inline | |
82 | int lttng_pthread_getname_np(char *name, size_t len) | |
83 | { | |
84 | return pthread_getname_np(pthread_self(), name, len); | |
85 | } | |
86 | #elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID) | |
87 | static inline | |
88 | int lttng_pthread_getname_np(char *name, size_t len) | |
89 | { | |
90 | return pthread_getname_np(name, len); | |
91 | } | |
92 | #elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID) | |
93 | static inline | |
94 | int lttng_pthread_getname_np(char *name, size_t len) | |
95 | { | |
96 | pthread_get_name_np(pthread_self(), name, len); | |
97 | return 0; | |
98 | } | |
99 | #elif defined(__linux__) | |
100 | ||
101 | /* Fallback on prtctl on Linux */ | |
102 | #include <sys/prctl.h> | |
103 | ||
104 | static inline | |
105 | int lttng_pthread_getname_np(char *name, size_t len) | |
106 | { | |
107 | return prctl(PR_GET_NAME, name, 0, 0, 0); | |
108 | } | |
109 | #else | |
110 | /* | |
111 | * For platforms without thread name support, do nothing. | |
112 | */ | |
014d7d3b MJ |
113 | static inline |
114 | int lttng_pthread_getname_np(char *name, size_t len) | |
115 | { | |
116 | return -ENOSYS; | |
117 | } | |
cb8d0d24 MJ |
118 | #endif |
119 | ||
120 | #endif /* _COMPAT_PTHREAD_H */ |