Move dynamic-type to libcommon
[lttng-ust.git] / src / common / compat / pthread.h
CommitLineData
b728d87e 1/*
c0c0989a
MJ
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
e92f3e28 4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
01f0e40c 5 * Copyright (C) 2016 Raphaël Beamonte <raphael.beamonte@gmail.com>
0db3d6ee 6 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
b728d87e
MD
7 */
8
27b98e6c
MJ
9#ifndef _UST_COMMON_COMPAT_PTHREAD_H
10#define _UST_COMMON_COMPAT_PTHREAD_H
c0c0989a 11
d9c56a93 12#include <pthread.h>
0db3d6ee 13#include <errno.h>
27b98e6c
MJ
14
15#include <lttng/ust-abi.h>
d9c56a93 16
653f7d34
MJ
17#ifdef __FreeBSD__
18#include <pthread_np.h>
19#endif
20
d9c56a93 21#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
08114193 22static inline
d9c56a93 23int lttng_pthread_setname_np(const char *name)
08114193 24{
bebc0c82
MJ
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
0db3d6ee 33 return pthread_setname_np(pthread_self(), name);
08114193 34}
d9c56a93 35#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
e4486ebc 36static inline
d9c56a93 37int lttng_pthread_setname_np(const char *name)
e4486ebc 38{
0db3d6ee 39 return pthread_setname_np(name);
e4486ebc 40}
0db3d6ee
MJ
41#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
42
01f0e40c 43static inline
d9c56a93 44int lttng_pthread_setname_np(const char *name)
01f0e40c 45{
f2db7517
MJ
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
0db3d6ee
MJ
51 pthread_set_name_np(pthread_self(), name);
52 return 0;
01f0e40c 53}
0db3d6ee
MJ
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{
f2db7517
MJ
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 }
0db3d6ee 66 return prctl(PR_SET_NAME, name, 0, 0, 0);
d9c56a93 67}
653f7d34
MJ
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>
01f0e40c 97
d9c56a93 98static inline
0db3d6ee 99int lttng_pthread_getname_np(char *name, size_t len)
d9c56a93 100{
0db3d6ee 101 return prctl(PR_GET_NAME, name, 0, 0, 0);
d9c56a93 102}
01f0e40c 103
0db3d6ee 104#else
653f7d34 105#error "Please add pthread get name support for your OS."
0db3d6ee
MJ
106#endif
107
27b98e6c 108#endif /* _UST_COMMON_COMPAT_PTHREAD_H */
This page took 0.039606 seconds and 4 git commands to generate.