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"
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"
18 #include <sys/prctl.h>
19 #include <sys/types.h>
29 const struct sched_param kRealTimePrio
= {8};
33 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap
[4] = {
34 {kThreadPriority_RealtimeAudio
, -10},
35 {kThreadPriority_Background
, 10},
36 {kThreadPriority_Normal
, 0},
37 {kThreadPriority_Display
, -6},
40 bool HandleSetThreadPriorityForPlatform(PlatformThreadHandle handle
,
41 ThreadPriority priority
) {
43 return priority
== kThreadPriority_RealtimeAudio
&&
44 pthread_setschedparam(pthread_self(), SCHED_RR
, &kRealTimePrio
) == 0;
50 } // namespace internal
53 void PlatformThread::SetName(const char* name
) {
54 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name
);
55 tracked_objects::ThreadData::InitializeThreadContext(name
);
58 // On linux we can get the thread names to show up in the debugger by setting
59 // the process name for the LWP. We don't want to do this for the main
60 // thread because that would rename the process, causing tools like killall
62 if (PlatformThread::CurrentId() == getpid())
65 // http://0pointer.de/blog/projects/name-your-threads.html
66 // Set the name for the LWP (which gets truncated to 15 characters).
67 // Note that glibc also has a 'pthread_setname_np' api, but it may not be
68 // available everywhere and it's only benefit over using prctl directly is
69 // that it can set the name of threads other than the current thread.
70 int err
= prctl(PR_SET_NAME
, name
);
71 // We expect EPERM failures in sandboxed processes, just ignore those.
72 if (err
< 0 && errno
!= EPERM
)
73 DPLOG(ERROR
) << "prctl(PR_SET_NAME)";
74 #endif // !defined(OS_NACL)
77 void InitThreading() {}
79 void InitOnThread() {}
81 void TerminateOnThread() {}
83 size_t GetDefaultThreadStackSize(const pthread_attr_t
& attributes
) {
84 #if !defined(THREAD_SANITIZER)
87 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
88 // default stack size isn't enough for some browser tests.
89 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).