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

Unified Diff: base/threading/platform_thread_posix.cc

Issue 12741012: base: Support setting thread priorities generically. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 7 years, 8 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_mac.mm ('k') | base/threading/platform_thread_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/threading/platform_thread_posix.cc
diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc
index 0fb2a8a2258068af1a1393201b530483d0d475f7..b6afc16d106494c6d0fd52316fe6f1f7e24ddd28 100644
--- a/base/threading/platform_thread_posix.cc
+++ b/base/threading/platform_thread_posix.cc
@@ -28,12 +28,6 @@
#include <unistd.h>
#endif
-#if defined(OS_ANDROID)
-#include <sys/resource.h>
-#include "base/android/thread_utils.h"
-#include "jni/ThreadUtils_jni.h"
-#endif
-
// TODO(bbudge) Use time.h when NaCl toolchain supports _POSIX_TIMERS
#if defined(OS_NACL)
#include <sys/nacl_syscalls.h>
@@ -41,9 +35,10 @@
namespace base {
-#if defined(OS_MACOSX)
void InitThreading();
-#endif
+void InitOnThread();
+void TerminateOnThread();
+size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes);
namespace {
@@ -53,54 +48,22 @@ struct ThreadParams {
ThreadPriority priority;
};
-void SetCurrentThreadPriority(ThreadPriority priority) {
-#if defined(OS_LINUX) || defined(OS_ANDROID)
- switch (priority) {
- case kThreadPriority_Normal:
- NOTREACHED() << "Don't reset priority as not all processes can.";
- break;
- case kThreadPriority_RealtimeAudio:
-#if defined(OS_LINUX)
- const int kNiceSetting = -10;
- // Linux isn't posix compliant with setpriority(2), it will set a thread
- // priority if it is passed a tid, 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.
- if (setpriority(PRIO_PROCESS, PlatformThread::CurrentId(), kNiceSetting))
- DVLOG(1) << "Failed to set nice value of thread to " << kNiceSetting;
-#elif defined(OS_ANDROID)
- JNIEnv* env = base::android::AttachCurrentThread();
- Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
-#endif // defined(OS_LINUX)
- break;
- }
-#else // !defined(OS_LINUX) && !defined(OS_ANDROID)
- PlatformThread::SetThreadPriority(pthread_self(), priority);
-#endif
-}
-
void* ThreadFunc(void* params) {
-#if defined(OS_ANDROID)
- // Threads on linux/android may inherit their priority from the thread
- // where they were created. This sets all threads to the default.
- // TODO(epenner): Move thread priorities to base. (crbug.com/170549)
- if (setpriority(PRIO_PROCESS, PlatformThread::CurrentId(), 0))
- DVLOG(1) << "Failed to reset initial thread nice value to zero.";
-#endif
+ base::InitOnThread();
ThreadParams* thread_params = static_cast<ThreadParams*>(params);
PlatformThread::Delegate* delegate = thread_params->delegate;
if (!thread_params->joinable)
base::ThreadRestrictions::SetSingletonAllowed(false);
- // If there is a non-default priority for this thread, set it now.
- if (thread_params->priority != kThreadPriority_Normal)
- SetCurrentThreadPriority(thread_params->priority);
+ if (thread_params->priority != kThreadPriority_Normal) {
+ PlatformThread::SetThreadPriority(pthread_self(),
+ PlatformThread::CurrentId(),
+ thread_params->priority);
+ }
delete thread_params;
delegate->ThreadMain();
-#if defined(OS_ANDROID)
- base::android::DetachFromVM();
-#endif
+ base::TerminateOnThread();
return NULL;
}
@@ -108,9 +71,7 @@ bool CreateThread(size_t stack_size, bool joinable,
PlatformThread::Delegate* delegate,
PlatformThreadHandle* thread_handle,
ThreadPriority priority) {
-#if defined(OS_MACOSX)
base::InitThreading();
-#endif // OS_MACOSX
bool success = false;
pthread_attr_t attributes;
@@ -122,36 +83,9 @@ bool CreateThread(size_t stack_size, bool joinable,
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
}
-#if defined(OS_MACOSX) && !defined(OS_IOS)
- // The Mac OS X default for a pthread stack size is 512kB.
- // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses
- // DEFAULT_STACK_SIZE for this purpose.
- //
- // 512kB isn't quite generous enough for some deeply recursive threads that
- // otherwise request the default stack size by specifying 0. Here, adopt
- // glibc's behavior as on Linux, which is to use the current stack size
- // limit (ulimit -s) as the default stack size. See
- // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To
- // avoid setting the limit below the Mac OS X default or the minimum usable
- // stack size, these values are also considered. If any of these values
- // can't be determined, or if stack size is unlimited (ulimit -s unlimited),
- // stack_size is left at 0 to get the system default.
- //
- // Mac OS X normally only applies ulimit -s to the main thread stack. On
- // contemporary OS X and Linux systems alike, this value is generally 8MB
- // or in that neighborhood.
- if (stack_size == 0) {
- size_t default_stack_size;
- struct rlimit stack_rlimit;
- if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 &&
- getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 &&
- stack_rlimit.rlim_cur != RLIM_INFINITY) {
- stack_size = std::max(std::max(default_stack_size,
- static_cast<size_t>(PTHREAD_STACK_MIN)),
- static_cast<size_t>(stack_rlimit.rlim_cur));
- }
- }
-#endif // OS_MACOSX && !OS_IOS
+ // Get a better default if available.
+ if (stack_size == 0)
+ stack_size = base::GetDefaultThreadStackSize(attributes);
if (stack_size > 0)
pthread_attr_setstacksize(&attributes, stack_size);
@@ -218,42 +152,6 @@ void PlatformThread::Sleep(TimeDelta duration) {
sleep_time = remaining;
}
-#if defined(OS_LINUX)
-// static
-void PlatformThread::SetName(const char* name) {
- ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
- tracked_objects::ThreadData::InitializeThreadContext(name);
-
- // 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)";
-}
-#elif defined(OS_MACOSX)
-// Mac is implemented in platform_thread_mac.mm.
-#else
-// static
-void PlatformThread::SetName(const char* name) {
- ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
- tracked_objects::ThreadData::InitializeThreadContext(name);
-
- // (This should be relatively simple to implement for the BSDs; I
- // just don't have one handy to test the code on.)
-}
-#endif // defined(OS_LINUX)
-
// static
const char* PlatformThread::GetName() {
return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
@@ -292,18 +190,43 @@ void PlatformThread::Join(PlatformThreadHandle thread_handle) {
pthread_join(thread_handle, NULL);
}
-#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
-// Mac OS X uses lower-level mach APIs and Android uses Java APIs.
+#if !defined(OS_LINUX) && !defined(OS_MACOSX)
// static
-void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) {
- // TODO(crogers): Implement, see http://crbug.com/116172
+void PlatformThread::SetName(const char* name) {
+ ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
+ tracked_objects::ThreadData::InitializeThreadContext(name);
+
+ // (This should be relatively simple to implement for the BSDs; I
+ // just don't have one handy to test the code on.)
}
#endif
-#if defined(OS_ANDROID)
-bool RegisterThreadUtils(JNIEnv* env) {
- return RegisterNativesImpl(env);
+#if !defined(OS_MACOSX) && !defined(OS_LINUX) && !defined(OS_ANDROID)
+void PlatformThread::SetThreadPriority(PlatformThreadHandle,
+ PlatformThreadId id,
+ ThreadPriority priority) {
}
-#endif // defined(OS_ANDROID)
+#endif
+
+#if !defined(OS_MACOSX)
jar (doing other things) 2013/05/03 23:54:07 This section is still messy (lines 193-230) with a
epennerAtGoogle 2013/05/04 01:09:45 Yes, I really did this for code clarity as opposed
+void InitThreading() {
+}
+#endif
+
+#if !defined(OS_ANDROID)
+void InitOnThread() {
+}
+#endif
+
+#if !defined(OS_ANDROID)
+void TerminateOnThread() {
+}
+#endif
+
+#if !defined(OS_MACOSX) && !defined(OS_IOS)
+size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
+ return 0;
+}
+#endif
} // namespace base
« no previous file with comments | « base/threading/platform_thread_mac.mm ('k') | base/threading/platform_thread_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698