Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1724)

Unified Diff: base/threading/platform_thread_linux.cc

Issue 12741012: base: Support setting thread priorities generically. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove static initializers. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/threading/platform_thread_android.cc ('k') | base/threading/platform_thread_mac.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/threading/platform_thread_linux.cc
diff --git a/base/threading/platform_thread_linux.cc b/base/threading/platform_thread_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..42e12c71a554920d94c23c503ff02c80bf8aaad4
--- /dev/null
+++ b/base/threading/platform_thread_linux.cc
@@ -0,0 +1,103 @@
+// 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_LINUX)
+#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) {
+ static const int threadPriorityAudio = -10;
+ static const int threadPriorityBackground = 10;
+ static const int threadPriorityDefault = 0;
+ static const int threadPriorityDisplay = -6;
+ switch (priority) {
+ case kThreadPriority_RealtimeAudio:
+ return threadPriorityAudio;
+ case kThreadPriority_Background:
+ return threadPriorityBackground;
+ case kThreadPriority_Normal:
+ return threadPriorityDefault;
+ case kThreadPriority_Display:
+ return threadPriorityDisplay;
+ default:
+ NOTREACHED() << "Unknown priority.";
+ return 0;
+ }
+}
+} // namespace
+
+// static
+void PlatformThread::SetName(const char* name) {
+ ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
+ tracked_objects::ThreadData::InitializeThreadContext(name);
+
+#ifndef 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
+}
+
+// static
+void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
+ ThreadPriority priority) {
+#ifndef OS_NACL
+ // setpriority(2) will set a thread's priority if it is passed a tid as
+ // the 'process identifier', not affecting the rest of the threads in the
+ // process. Setting this priority will only succeed if the user has been
+ // granted permission to adjust nice values on the system.
+ DCHECK_NE(handle.id_, kInvalidThreadId);
+ int kNiceSetting = ThreadNiceValue(priority);
+ if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting))
+ LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting;
+#endif
+}
+
+void InitThreading() {
+}
+
+void InitOnThread() {
+}
+
+void TerminateOnThread() {
+}
+
+size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
+ return 0;
+}
+
+} // namespace base
« no previous file with comments | « base/threading/platform_thread_android.cc ('k') | base/threading/platform_thread_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698