@@ -16,24 +16,60 @@ written by
16
16
#ifndef INC_SRT_THREADNAME_H
17
17
#define INC_SRT_THREADNAME_H
18
18
19
-
#if defined(__APPLE__) || defined(__linux__)
20
-
#if defined(__linux__)
21
-
#include <sys/prctl.h>
19
+
// NOTE:
20
+
// HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H
21
+
// HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H
22
+
// HAVE_PTHREAD_GETNAME_NP
23
+
// HAVE_PTHREAD_GETNAME_NP
24
+
// Are detected and set in ../CMakeLists.txt.
25
+
// OS Availability of pthread_getname_np(..) and pthread_setname_np(..)::
26
+
// MacOS(10.6)
27
+
// iOS(3.2)
28
+
// AIX(7.1)
29
+
// FreeBSD(version?), OpenBSD(Version?)
30
+
// Linux-GLIBC(GLIBC-2.12).
31
+
// Linux-MUSL(MUSL-1.1.20 Partial Implementation. See below).
32
+
// MINGW-W64(4.0.6)
33
+
34
+
#if defined(HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H) \
35
+
|| defined(HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H)
36
+
#include <pthread_np.h>
37
+
#if defined(HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H) \
38
+
&& !defined(HAVE_PTHREAD_GETNAME_NP)
39
+
#define HAVE_PTHREAD_GETNAME_NP 1
40
+
#endif
41
+
#if defined(HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H) \
42
+
&& !defined(HAVE_PTHREAD_SETNAME_NP)
43
+
#define HAVE_PTHREAD_SETNAME_NP 1
44
+
#endif
22
45
#endif
23
46
24
-
#include <pthread.h>
47
+
#if (defined(HAVE_PTHREAD_GETNAME_NP) && defined(HAVE_PTHREAD_GETNAME_NP)) \
48
+
|| defined(__linux__)
49
+
// NOTE:
50
+
// Linux pthread_getname_np() and pthread_setname_np() became available
51
+
// in GLIBC-2.12 and later.
52
+
// Some Linux runtimes do not have pthread_getname_np(), but have
53
+
// pthread_setname_np(), for instance MUSL at least as of v1.1.20.
54
+
// So using the prctl() for Linux is more portable.
55
+
#if defined(__linux__)
56
+
#include <sys/prctl.h>
57
+
#endif
58
+
#include <pthread.h>
25
59
#endif
26
60
27
61
#include <cstdio>
28
62
#include <cstring>
29
63
#include <string>
30
64
65
+
#include "common.h"
31
66
#include "sync.h"
32
67
33
68
class ThreadName
34
69
{
35
70
36
-
#if defined(__APPLE__) || defined(__linux__)
71
+
#if (defined(HAVE_PTHREAD_GETNAME_NP) && defined(HAVE_PTHREAD_GETNAME_NP)) \
72
+
|| defined(__linux__)
37
73
38
74
class ThreadNameImpl
39
75
{
@@ -47,8 +83,7 @@ class ThreadName
47
83
// since Linux 2.6.11. The buffer should allow space for up to 16
48
84
// bytes; the returned string will be null-terminated.
49
85
return prctl(PR_GET_NAME, (unsigned long)namebuf, 0, 0) != -1;
50
-
#elif defined(__APPLE__)
51
-
// since macos(10.6), ios(3.2)
86
+
#elif defined(HAVE_PTHREAD_GETNAME_NP)
52
87
return pthread_getname_np(pthread_self(), namebuf, BUFSIZE) == 0;
53
88
#else
54
89
#error "unsupported platform"
@@ -57,14 +92,18 @@ class ThreadName
57
92
58
93
static bool set(const char* name)
59
94
{
95
+
SRT_ASSERT(name != NULL);
60
96
#if defined(__linux__)
61
97
// The name can be up to 16 bytes long, including the terminating
62
98
// null byte. (If the length of the string, including the terminating
63
99
// null byte, exceeds 16 bytes, the string is silently truncated.)
64
100
return prctl(PR_SET_NAME, (unsigned long)name, 0, 0) != -1;
65
-
#elif defined(__APPLE__)
66
-
// since macos(10.6), ios(3.2)
101
+
#elif defined(HAVE_PTHREAD_SETNAME_NP)
102
+
#if defined(__APPLE__)
67
103
return pthread_setname_np(name) == 0;
104
+
#else
105
+
return pthread_setname_np(pthread_self(), name) == 0;
106
+
#endif
68
107
#else
69
108
#error "unsupported platform"
70
109
#endif
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4