2 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef _COMPAT_PTHREAD_H
9 #define _COMPAT_PTHREAD_H
12 #include <common/compat/errno.h>
16 #include <pthread_np.h>
19 #define LTTNG_PTHREAD_NAMELEN 16
21 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
23 int lttng_pthread_setname_np(const char *name
)
26 * Some implementations don't error out, replicate this behavior for
29 if (strnlen(name
, LTTNG_PTHREAD_NAMELEN
) >= LTTNG_PTHREAD_NAMELEN
) {
33 return pthread_setname_np(pthread_self(), name
);
35 #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
37 int lttng_pthread_setname_np(const char *name
)
39 return pthread_setname_np(name
);
41 #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
44 int lttng_pthread_setname_np(const char *name
)
46 /* Replicate pthread_setname_np's behavior. */
47 if (strnlen(name
, LTTNG_PTHREAD_NAMELEN
) >= LTTNG_PTHREAD_NAMELEN
) {
51 pthread_set_name_np(pthread_self(), name
);
54 #elif defined(__linux__)
56 /* Fallback on prtctl on Linux */
57 #include <sys/prctl.h>
60 int lttng_pthread_setname_np(const char *name
)
62 /* Replicate pthread_setname_np's behavior. */
63 if (strnlen(name
, LTTNG_UST_ABI_PROCNAME_LEN
) >= LTTNG_UST_ABI_PROCNAME_LEN
) {
66 return prctl(PR_SET_NAME
, name
, 0, 0, 0);
70 * For platforms without thread name support, do nothing.
73 int lttng_pthread_setname_np(const char *name
)
80 #if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID)
82 int lttng_pthread_getname_np(char *name
, size_t len
)
84 return pthread_getname_np(pthread_self(), name
, len
);
86 #elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID)
88 int lttng_pthread_getname_np(char *name
, size_t len
)
90 return pthread_getname_np(name
, len
);
92 #elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID)
94 int lttng_pthread_getname_np(char *name
, size_t len
)
96 pthread_get_name_np(pthread_self(), name
, len
);
99 #elif defined(__linux__)
101 /* Fallback on prtctl on Linux */
102 #include <sys/prctl.h>
105 int lttng_pthread_getname_np(char *name
, size_t len
)
107 return prctl(PR_GET_NAME
, name
, 0, 0, 0);
111 * For platforms without thread name support, do nothing.
114 int lttng_pthread_getname_np(char *name
, size_t len
)
120 #endif /* _COMPAT_PTHREAD_H */