Move utils_expand_path and utils_expand_path_keep_symlink to libpath.la
[lttng-tools.git] / src / common / compat / pthread.h
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>
12 #include <common/compat/errno.h>
13 #include <string.h>
14
15 #ifdef __FreeBSD__
16 #include <pthread_np.h>
17 #endif
18
19 #define LTTNG_PTHREAD_NAMELEN 16
20
21 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
22 static inline
23 int lttng_pthread_setname_np(const char *name)
24 {
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
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 }
41 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
42
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 }
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 }
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 }
77 #endif
78
79
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 */
113 static inline
114 int lttng_pthread_getname_np(char *name, size_t len)
115 {
116 return -ENOSYS;
117 }
118 #endif
119
120 #endif /* _COMPAT_PTHREAD_H */
This page took 0.032852 seconds and 4 git commands to generate.