Don't preload rarely seen large images
[chromium-blink-merge.git] / base / threading / platform_thread_linux.cc
blobe0f620e198a4dbe2a5bd6a6da1859589c67119e1
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/threading/platform_thread.h"
7 #include <errno.h>
8 #include <sched.h>
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/threading/platform_thread_internal_posix.h"
13 #include "base/threading/thread_id_name_manager.h"
14 #include "base/tracked_objects.h"
16 #if !defined(OS_NACL)
17 #include <pthread.h>
18 #include <sys/prctl.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #endif
23 namespace base {
25 namespace internal {
27 namespace {
28 #if !defined(OS_NACL)
29 const struct sched_param kRealTimePrio = {8};
30 const struct sched_param kResetPrio = {0};
31 #endif
32 } // namespace
34 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
35 {ThreadPriority::BACKGROUND, 10},
36 {ThreadPriority::NORMAL, 0},
37 {ThreadPriority::DISPLAY, -6},
38 {ThreadPriority::REALTIME_AUDIO, -10},
41 bool SetThreadPriorityForPlatform(PlatformThreadHandle handle,
42 ThreadPriority priority) {
43 #if !defined(OS_NACL)
44 ThreadPriority current_priority;
45 if (priority != ThreadPriority::REALTIME_AUDIO &&
46 GetThreadPriorityForPlatform(handle, &current_priority) &&
47 current_priority == ThreadPriority::REALTIME_AUDIO) {
48 // If the pthread's round-robin scheduler is already enabled, and the new
49 // priority will use setpriority() instead, the pthread scheduler should be
50 // reset to use SCHED_OTHER so that setpriority() just works.
51 pthread_setschedparam(pthread_self(), SCHED_OTHER, &kResetPrio);
52 return false;
54 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
55 // of |handle|. http://crbug.com/468793.
56 return priority == ThreadPriority::REALTIME_AUDIO &&
57 pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0;
58 #else
59 return false;
60 #endif
63 bool GetThreadPriorityForPlatform(PlatformThreadHandle handle,
64 ThreadPriority* priority) {
65 #if !defined(OS_NACL)
66 int maybe_sched_rr = 0;
67 struct sched_param maybe_realtime_prio = {0};
68 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
69 // of |handle|. http://crbug.com/468793.
70 if (pthread_getschedparam(pthread_self(), &maybe_sched_rr,
71 &maybe_realtime_prio) == 0 &&
72 maybe_sched_rr == SCHED_RR &&
73 maybe_realtime_prio.sched_priority == kRealTimePrio.sched_priority) {
74 *priority = ThreadPriority::REALTIME_AUDIO;
75 return true;
77 #endif
78 return false;
81 } // namespace internal
83 // static
84 void PlatformThread::SetName(const std::string& name) {
85 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
86 tracked_objects::ThreadData::InitializeThreadContext(name);
88 #if !defined(OS_NACL)
89 // On linux we can get the thread names to show up in the debugger by setting
90 // the process name for the LWP. We don't want to do this for the main
91 // thread because that would rename the process, causing tools like killall
92 // to stop working.
93 if (PlatformThread::CurrentId() == getpid())
94 return;
96 // http://0pointer.de/blog/projects/name-your-threads.html
97 // Set the name for the LWP (which gets truncated to 15 characters).
98 // Note that glibc also has a 'pthread_setname_np' api, but it may not be
99 // available everywhere and it's only benefit over using prctl directly is
100 // that it can set the name of threads other than the current thread.
101 int err = prctl(PR_SET_NAME, name.c_str());
102 // We expect EPERM failures in sandboxed processes, just ignore those.
103 if (err < 0 && errno != EPERM)
104 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
105 #endif // !defined(OS_NACL)
108 void InitThreading() {}
110 void InitOnThread() {}
112 void TerminateOnThread() {}
114 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
115 #if !defined(THREAD_SANITIZER)
116 return 0;
117 #else
118 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
119 // default stack size isn't enough for some browser tests.
120 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).
121 #endif
124 } // namespace base