Allow supervised users to create bookmark apps.
[chromium-blink-merge.git] / base / threading / platform_thread_linux.cc
blob9f7437418a7299ec6ab92d80b1be6735f6b9b9fa
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 #endif
31 } // namespace
33 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
34 {ThreadPriority::BACKGROUND, 10},
35 {ThreadPriority::NORMAL, 0},
36 {ThreadPriority::DISPLAY, -6},
37 {ThreadPriority::REALTIME_AUDIO, -10},
40 bool SetThreadPriorityForPlatform(PlatformThreadHandle handle,
41 ThreadPriority priority) {
42 #if !defined(OS_NACL)
43 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
44 // of |handle|. http://crbug.com/468793.
45 return priority == ThreadPriority::REALTIME_AUDIO &&
46 pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0;
47 #else
48 return false;
49 #endif
52 bool GetThreadPriorityForPlatform(PlatformThreadHandle handle,
53 ThreadPriority* priority) {
54 #if !defined(OS_NACL)
55 int maybe_sched_rr = 0;
56 struct sched_param maybe_realtime_prio = {0};
57 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
58 // of |handle|. http://crbug.com/468793.
59 if (pthread_getschedparam(pthread_self(), &maybe_sched_rr,
60 &maybe_realtime_prio) == 0 &&
61 maybe_sched_rr == SCHED_RR &&
62 maybe_realtime_prio.sched_priority == kRealTimePrio.sched_priority) {
63 *priority = ThreadPriority::REALTIME_AUDIO;
64 return true;
66 #endif
67 return false;
70 } // namespace internal
72 // static
73 void PlatformThread::SetName(const std::string& name) {
74 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
75 tracked_objects::ThreadData::InitializeThreadContext(name);
77 #if !defined(OS_NACL)
78 // On linux we can get the thread names to show up in the debugger by setting
79 // the process name for the LWP. We don't want to do this for the main
80 // thread because that would rename the process, causing tools like killall
81 // to stop working.
82 if (PlatformThread::CurrentId() == getpid())
83 return;
85 // http://0pointer.de/blog/projects/name-your-threads.html
86 // Set the name for the LWP (which gets truncated to 15 characters).
87 // Note that glibc also has a 'pthread_setname_np' api, but it may not be
88 // available everywhere and it's only benefit over using prctl directly is
89 // that it can set the name of threads other than the current thread.
90 int err = prctl(PR_SET_NAME, name.c_str());
91 // We expect EPERM failures in sandboxed processes, just ignore those.
92 if (err < 0 && errno != EPERM)
93 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
94 #endif // !defined(OS_NACL)
97 void InitThreading() {}
99 void InitOnThread() {}
101 void TerminateOnThread() {}
103 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
104 #if !defined(THREAD_SANITIZER)
105 return 0;
106 #else
107 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
108 // default stack size isn't enough for some browser tests.
109 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).
110 #endif
113 } // namespace base