Move dynamic-type to libcommon
[lttng-ust.git] / src / common / compat / pthread.h
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2016 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
6 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
7 */
8
9#ifndef _UST_COMMON_COMPAT_PTHREAD_H
10#define _UST_COMMON_COMPAT_PTHREAD_H
11
12#include <pthread.h>
13#include <errno.h>
14
15#include <lttng/ust-abi.h>
16
17#ifdef __FreeBSD__
18#include <pthread_np.h>
19#endif
20
21#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
22static inline
23int 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_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
30 return ERANGE;
31 }
32
33 return pthread_setname_np(pthread_self(), name);
34}
35#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
36static inline
37int 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
43static inline
44int lttng_pthread_setname_np(const char *name)
45{
46 /* Replicate pthread_setname_np's behavior */
47 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
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
59static inline
60int 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#error "Please add pthread set name support for your OS."
70#endif
71
72
73#if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID)
74static inline
75int lttng_pthread_getname_np(char *name, size_t len)
76{
77 return pthread_getname_np(pthread_self(), name, len);
78}
79#elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID)
80static inline
81int lttng_pthread_getname_np(char *name, size_t len)
82{
83 return pthread_getname_np(name, len);
84}
85#elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID)
86
87static inline
88int lttng_pthread_getname_np(char *name, size_t len)
89{
90 pthread_get_name_np(pthread_self(), name, len);
91 return 0;
92}
93#elif defined(__linux__)
94
95/* Fallback on prtctl on Linux */
96#include <sys/prctl.h>
97
98static inline
99int lttng_pthread_getname_np(char *name, size_t len)
100{
101 return prctl(PR_GET_NAME, name, 0, 0, 0);
102}
103
104#else
105#error "Please add pthread get name support for your OS."
106#endif
107
108#endif /* _UST_COMMON_COMPAT_PTHREAD_H */
This page took 0.035611 seconds and 4 git commands to generate.