Debian Codegraph @

srtcore/threadname.h

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 written by
13 Haivision Systems Inc.
14 *****************************************************************************/
15
16 #ifndef INC_SRT_THREADNAME_H
17 #define INC_SRT_THREADNAME_H
18
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
45 #endif
46
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>
59 #endif
60
61 #include <cstdio>
62 #include <cstring>
63 #include <string>
64
65 #include "common.h"
66 #include "sync.h"
67
68 namespace srt {
69
70 class ThreadName
71 {
72
73 #if (defined(HAVE_PTHREAD_GETNAME_NP) && defined(HAVE_PTHREAD_GETNAME_NP)) \
74 || defined(__linux__)
75
76 class ThreadNameImpl
77 {
78 public:
79 static const size_t BUFSIZE = 64;
80 static const bool DUMMY_IMPL = false;
81
82 static bool get(char* namebuf)
83 {
84 #if defined(__linux__)
85 // since Linux 2.6.11. The buffer should allow space for up to 16
86 // bytes; the returned string will be null-terminated.
87 return prctl(PR_GET_NAME, (unsigned long)namebuf, 0, 0) != -1;
88 #elif defined(HAVE_PTHREAD_GETNAME_NP)
89 return pthread_getname_np(pthread_self(), namebuf, BUFSIZE) == 0;
90 #else
91 #error "unsupported platform"
92 #endif
93 }
94
95 static bool set(const char* name)
96 {
97 SRT_ASSERT(name != NULL);
98 #if defined(__linux__)
99 // The name can be up to 16 bytes long, including the terminating
100 // null byte. (If the length of the string, including the terminating
101 // null byte, exceeds 16 bytes, the string is silently truncated.)
102 return prctl(PR_SET_NAME, (unsigned long)name, 0, 0) != -1;
103 #elif defined(HAVE_PTHREAD_SETNAME_NP)
104 #if defined(__APPLE__)
105 return pthread_setname_np(name) == 0;
106 #elif defined(__NetBSD__)
107 return pthread_setname_np(pthread_self(), "%s", name) == 0;
108 #else
109 return pthread_setname_np(pthread_self(), name) == 0;
110 #endif
111 #else
112 #error "unsupported platform"
113 #endif
114 }
115
116 explicit ThreadNameImpl(const std::string& name)
117 : reset(false)
118 {
119 tid = pthread_self();
120
121 if (!get(old_name))
122 return;
123
124 reset = set(name.c_str());
125 if (reset)
126 return;
127
128 // Try with a shorter name. 15 is the upper limit supported by Linux,
129 // other platforms should support a larger value. So 15 should works
130 // on all platforms.
131 const size_t max_len = 15;
132 if (name.size() > max_len)
133 reset = set(name.substr(0, max_len).c_str());
134 }
135
136 ~ThreadNameImpl()
137 {
138 if (!reset)
139 return;
140
141 // ensure it's called on the right thread
142 if (tid == pthread_self())
143 set(old_name);
144 }
145
146 private:
147 ThreadNameImpl(ThreadNameImpl& other);
148 ThreadNameImpl& operator=(const ThreadNameImpl& other);
149
150 private:
151 bool reset;
152 pthread_t tid;
153 char old_name[BUFSIZE];
154 };
155
156 #else
157
158 class ThreadNameImpl
159 {
160 public:
161 static const bool DUMMY_IMPL = true;
162 static const size_t BUFSIZE = 64;
163
164 static bool get(char* output)
165 {
166 // The default implementation will simply try to get the thread ID
167 std::ostringstream bs;
168 bs << "T" << sync::this_thread::get_id();
169 size_t s = bs.str().copy(output, BUFSIZE - 1);
170 output[s] = '\0';
171 return true;
172 }
173
174 static bool set(const char*) { return false; }
175
176 ThreadNameImpl(const std::string&) {}
177
178 ~ThreadNameImpl() // just to make it "non-trivially-destructible" for compatibility with normal version
179 {
180 }
181 };
182
183 #endif // platform dependent impl
184
185 // Why delegate to impl:
186 // 1. to make sure implementation on different platforms have the same interface.
187 // 2. it's simple to add some wrappers like get(const std::string &).
188 ThreadNameImpl impl;
189
190 public:
191 static const bool DUMMY_IMPL = ThreadNameImpl::DUMMY_IMPL;
192 static const size_t BUFSIZE = ThreadNameImpl::BUFSIZE;
193
194 /// @brief Print thread ID to the provided buffer.
195 /// The size of the destination buffer is assumed to be at least ThreadName::BUFSIZE.
196 /// @param [out] output destination buffer to get thread name
197 /// @return true on success, false on failure
198 static bool get(char* output) {
199 return ThreadNameImpl::get(output);
200 }
201
202 static bool get(std::string& name)
203 {
204 char buf[BUFSIZE];
205 bool ret = get(buf);
206 if (ret)
207 name = buf;
208 return ret;
209 }
210
211 static bool set(const std::string& name) { return ThreadNameImpl::set(name.c_str()); }
212
213 explicit ThreadName(const std::string& name)
214 : impl(name)
215 {
216 }
217
218 private:
219 ThreadName(const ThreadName&);
220 ThreadName(const char*);
221 ThreadName& operator=(const ThreadName& other);
222 };
223
224 } // namespace srt
225
226 #endif