Remove SimpleCacheSetSize test.
[chromium-blink-merge.git] / base / threading / platform_thread_freebsd.cc
blobf4fded0ebb2e11dfc41aeebf93005aa0f3f5985d
1 // Copyright 2014 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/thread_id_name_manager.h"
13 #include "base/tracked_objects.h"
15 #if !defined(OS_NACL)
16 #include <pthread.h>
17 #include <sys/prctl.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif
22 namespace base {
24 namespace internal {
26 namespace {
27 #if !defined(OS_NACL)
28 const struct sched_param kRealTimePrio = {8};
29 #endif
30 } // namespace
32 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
33 {ThreadPriority::BACKGROUND, 10},
34 {ThreadPriority::NORMAL, 0},
35 {ThreadPriority::DISPLAY, -6},
36 {ThreadPriority::REALTIME_AUDIO, -10},
39 bool SetThreadPriorityForPlatform(PlatformThreadHandle handle,
40 ThreadPriority priority) {
41 #if !defined(OS_NACL)
42 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
43 // of |handle|. http://crbug.com/468793.
44 return priority == ThreadPriority::REALTIME_AUDIO &&
45 pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0;
46 #else
47 return false;
48 #endif
51 bool GetThreadPriorityForPlatform(PlatformThreadHandle handle,
52 ThreadPriority* priority) {
53 #if !defined(OS_NACL)
54 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
55 // of |handle|. http://crbug.com/468793.
56 int maybe_sched_rr = 0;
57 struct sched_param maybe_realtime_prio = {0};
58 if (pthread_getschedparam(pthread_self(), &maybe_sched_rr,
59 &maybe_realtime_prio) == 0 &&
60 maybe_sched_rr == SCHED_RR &&
61 maybe_realtime_prio.sched_priority == kRealTimePrio.sched_priority) {
62 *priority = ThreadPriority::REALTIME_AUDIO;
63 return true;
65 #endif
66 return false;
69 } // namespace internal
71 // static
72 void PlatformThread::SetName(const std::string& name) {
73 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
74 tracked_objects::ThreadData::InitializeThreadContext(name);
76 #if !defined(OS_NACL)
77 // On FreeBSD we can get the thread names to show up in the debugger by
78 // setting the process name for the LWP. We don't want to do this for the
79 // main thread because that would rename the process, causing tools like
80 // killall to stop working.
81 if (PlatformThread::CurrentId() == getpid())
82 return;
83 setproctitle("%s", name.c_str());
84 #endif // !defined(OS_NACL)
87 void InitThreading() {}
89 void InitOnThread() {}
91 void TerminateOnThread() {}
93 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
94 #if !defined(THREAD_SANITIZER)
95 return 0;
96 #else
97 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
98 // default stack size isn't enough for some browser tests.
99 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).
100 #endif
103 } // namespace base