forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_thread_linux.cc
119 lines (101 loc) · 3.79 KB
/
platform_thread_linux.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/platform_thread.h"
#include <errno.h>
#include <sched.h>
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/safe_strerror_posix.h"
#include "base/threading/thread_id_name_manager.h"
#include "base/threading/thread_restrictions.h"
#include "base/tracked_objects.h"
#if !defined(OS_NACL)
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>
#endif
namespace base {
namespace {
int ThreadNiceValue(ThreadPriority priority) {
switch (priority) {
case kThreadPriority_RealtimeAudio:
return -10;
case kThreadPriority_Background:
return 10;
case kThreadPriority_Normal:
return 0;
case kThreadPriority_Display:
return -6;
default:
NOTREACHED() << "Unknown priority.";
return 0;
}
}
} // namespace
// static
void PlatformThread::SetName(const char* name) {
ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
tracked_objects::ThreadData::InitializeThreadContext(name);
#if !defined(OS_NACL)
// On linux we can get the thread names to show up in the debugger by setting
// the process name for the LWP. We don't want to do this for the main
// thread because that would rename the process, causing tools like killall
// to stop working.
if (PlatformThread::CurrentId() == getpid())
return;
// http://0pointer.de/blog/projects/name-your-threads.html
// Set the name for the LWP (which gets truncated to 15 characters).
// Note that glibc also has a 'pthread_setname_np' api, but it may not be
// available everywhere and it's only benefit over using prctl directly is
// that it can set the name of threads other than the current thread.
int err = prctl(PR_SET_NAME, name);
// We expect EPERM failures in sandboxed processes, just ignore those.
if (err < 0 && errno != EPERM)
DPLOG(ERROR) << "prctl(PR_SET_NAME)";
#endif // !defined(OS_NACL)
}
// static
void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
ThreadPriority priority) {
#if !defined(OS_NACL)
if (priority == kThreadPriority_RealtimeAudio) {
const struct sched_param kRealTimePrio = {8};
if (pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0) {
// Got real time priority, no need to set nice level.
return;
}
}
// setpriority(2) should change the whole thread group's (i.e. process)
// priority. however, on linux it will only change the target thread's
// priority. see the bugs section in
// http://man7.org/linux/man-pages/man2/getpriority.2.html.
// we prefer using 0 rather than the current thread id since they are
// equivalent but it makes sandboxing easier (https://crbug.com/399473).
DCHECK_NE(handle.id_, kInvalidThreadId);
const int kNiceSetting = ThreadNiceValue(priority);
const PlatformThreadId current_id = PlatformThread::CurrentId();
if (setpriority(PRIO_PROCESS,
handle.id_ == current_id ? 0 : handle.id_,
kNiceSetting)) {
DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to "
<< kNiceSetting;
}
#endif // !defined(OS_NACL)
}
void InitThreading() {}
void InitOnThread() {}
void TerminateOnThread() {}
size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
#if !defined(THREAD_SANITIZER)
return 0;
#else
// ThreadSanitizer bloats the stack heavily. Evidence has been that the
// default stack size isn't enough for some browser tests.
return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).
#endif
}
} // namespace base