blob: 4167392363039e8a4225c8431ddd8ba9ba3f3c08 [file] [log] [blame]
Tommibebc6902015-05-18 09:51:42 +02001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/base/platform_thread.h"
12
Tommiea14f0a2015-05-18 13:51:06 +020013#include <string.h>
14
Tommibebc6902015-05-18 09:51:42 +020015#include "webrtc/base/checks.h"
16
17#if defined(WEBRTC_LINUX)
Tommiea14f0a2015-05-18 13:51:06 +020018#include <sys/prctl.h>
Tommibebc6902015-05-18 09:51:42 +020019#include <sys/syscall.h>
20#endif
21
22namespace rtc {
23
24PlatformThreadId CurrentThreadId() {
25 PlatformThreadId ret;
26#if defined(WEBRTC_WIN)
27 ret = GetCurrentThreadId();
28#elif defined(WEBRTC_POSIX)
29#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
30 ret = pthread_mach_thread_np(pthread_self());
31#elif defined(WEBRTC_LINUX)
32 ret = syscall(__NR_gettid);
33#elif defined(WEBRTC_ANDROID)
34 ret = gettid();
35#else
36 // Default implementation for nacl and solaris.
37 ret = reinterpret_cast<pid_t>(pthread_self());
38#endif
39#endif // defined(WEBRTC_POSIX)
henrikg91d6ede2015-09-17 00:24:34 -070040 RTC_DCHECK(ret);
Tommibebc6902015-05-18 09:51:42 +020041 return ret;
42}
43
44PlatformThreadRef CurrentThreadRef() {
45#if defined(WEBRTC_WIN)
46 return GetCurrentThreadId();
47#elif defined(WEBRTC_POSIX)
48 return pthread_self();
49#endif
50}
51
52bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) {
53#if defined(WEBRTC_WIN)
54 return a == b;
55#elif defined(WEBRTC_POSIX)
56 return pthread_equal(a, b);
57#endif
58}
59
Tommiea14f0a2015-05-18 13:51:06 +020060void SetCurrentThreadName(const char* name) {
henrikg91d6ede2015-09-17 00:24:34 -070061 RTC_DCHECK(strlen(name) < 64);
Tommiea14f0a2015-05-18 13:51:06 +020062#if defined(WEBRTC_WIN)
63 struct {
64 DWORD dwType;
65 LPCSTR szName;
66 DWORD dwThreadID;
67 DWORD dwFlags;
68 } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0};
69
70 __try {
71 ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD),
72 reinterpret_cast<ULONG_PTR*>(&threadname_info));
73 } __except (EXCEPTION_EXECUTE_HANDLER) {
74 }
75#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
76 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name));
77#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
78 pthread_setname_np(name);
79#endif
80}
81
Tommibebc6902015-05-18 09:51:42 +020082} // namespace rtc