summaryrefslogtreecommitdiffstats
path: root/base/threading
diff options
context:
space:
mode:
authordgreid@chromium.org <dgreid@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-05 18:58:50 +0000
committerdgreid@chromium.org <dgreid@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-05 18:58:50 +0000
commit5c62e75fd40f4c475ef324ed1a7cb308102ee304 (patch)
treefc1a4a7927df095e3d6d9b6393f64049fd529f7c /base/threading
parent83055ea7ae7cadaf89b28bdfc96c61e7ec6b41a0 (diff)
downloadchromium_src-5c62e75fd40f4c475ef324ed1a7cb308102ee304.zip
chromium_src-5c62e75fd40f4c475ef324ed1a7cb308102ee304.tar.gz
chromium_src-5c62e75fd40f4c475ef324ed1a7cb308102ee304.tar.bz2
Add ability to create a platform thread with a priority.
Add a function to create a thread with a given priority. This allows for a thread on Linux to set its priority without the need for passing around its tid. SetThreadPriority remains unimplemented on Linux for now. Use the new create function from AudioDeviceThread::Create(). BUG=116172 TEST=Manual, build and run chrome, check thread priorities with ps like this: ps -C chrome -Lo pid,tid,pri_api,rtprio,cmd Review URL: http://codereview.chromium.org/9950127 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130982 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/threading')
-rw-r--r--base/threading/platform_thread.h11
-rw-r--r--base/threading/platform_thread_posix.cc35
-rw-r--r--base/threading/platform_thread_win.cc10
3 files changed, 53 insertions, 3 deletions
diff --git a/base/threading/platform_thread.h b/base/threading/platform_thread.h
index 3aa3879..3843ce5 100644
--- a/base/threading/platform_thread.h
+++ b/base/threading/platform_thread.h
@@ -89,6 +89,15 @@ class BASE_EXPORT PlatformThread {
static bool Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle);
+ // CreateWithPriority() does the same thing as Create() except the priority of
+ // the thread is set based on |priority|. Can be used in place of Create()
+ // followed by SetThreadPriority(). SetThreadPriority() has not been
+ // implemented on the Linux platform yet, this is the only way to get a high
+ // priority thread on Linux.
+ static bool CreateWithPriority(size_t stack_size, Delegate* delegate,
+ PlatformThreadHandle* thread_handle,
+ ThreadPriority priority);
+
// CreateNonJoinable() does the same thing as Create() except the thread
// cannot be Join()'d. Therefore, it also does not output a
// PlatformThreadHandle.
@@ -99,6 +108,8 @@ class BASE_EXPORT PlatformThread {
// |thread_handle|.
static void Join(PlatformThreadHandle thread_handle);
+ // Sets the priority of the thread specified in |handle| to |priority|.
+ // This does not work on Linux, use CreateWithPriority() instead.
static void SetThreadPriority(PlatformThreadHandle handle,
ThreadPriority priority);
diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc
index 24bf3d2..388f8ba 100644
--- a/base/threading/platform_thread_posix.cc
+++ b/base/threading/platform_thread_posix.cc
@@ -22,7 +22,9 @@
#if defined(OS_LINUX)
#include <sys/prctl.h>
+#include <sys/resource.h>
#include <sys/syscall.h>
+#include <sys/time.h>
#include <unistd.h>
#endif
@@ -69,7 +71,8 @@ void* ThreadFunc(void* params) {
bool CreateThread(size_t stack_size, bool joinable,
PlatformThread::Delegate* delegate,
- PlatformThreadHandle* thread_handle) {
+ PlatformThreadHandle* thread_handle,
+ ThreadPriority priority) {
#if defined(OS_MACOSX)
base::InitThreading();
#endif // OS_MACOSX
@@ -123,6 +126,24 @@ bool CreateThread(size_t stack_size, bool joinable,
params->joinable = joinable;
success = !pthread_create(thread_handle, &attributes, ThreadFunc, params);
+ if (priority != kThreadPriority_Normal) {
+#if defined(OS_LINUX)
+ if (priority == kThreadPriority_RealtimeAudio) {
+ // 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.
+ const int kNiceSetting = -10;
+ if (setpriority(PRIO_PROCESS, PlatformThread::CurrentId(), kNiceSetting))
+ DVLOG(1) << "Failed to set nice value of thread to " << kNiceSetting;
+ } else {
+ NOTREACHED() << "Unknown thread priority.";
+ }
+#else
+ PlatformThread::SetThreadPriority(*thread_handle, priority);
+#endif
+ }
+
pthread_attr_destroy(&attributes);
if (!success)
delete params;
@@ -226,7 +247,15 @@ const char* PlatformThread::GetName() {
bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle) {
return CreateThread(stack_size, true /* joinable thread */,
- delegate, thread_handle);
+ delegate, thread_handle, kThreadPriority_Normal);
+}
+
+// static
+bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate,
+ PlatformThreadHandle* thread_handle,
+ ThreadPriority priority) {
+ return CreateThread(stack_size, true, // joinable thread
+ delegate, thread_handle, priority);
}
// static
@@ -234,7 +263,7 @@ bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
PlatformThreadHandle unused;
bool result = CreateThread(stack_size, false /* non-joinable thread */,
- delegate, &unused);
+ delegate, &unused, kThreadPriority_Normal);
return result;
}
diff --git a/base/threading/platform_thread_win.cc b/base/threading/platform_thread_win.cc
index 4ba5a8a..a757965 100644
--- a/base/threading/platform_thread_win.cc
+++ b/base/threading/platform_thread_win.cc
@@ -152,6 +152,16 @@ bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
}
// static
+bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate,
+ PlatformThreadHandle* thread_handle,
+ ThreadPriority priority) {
+ bool result = Create(stack_size, delegate, thread_handle);
+ if (result)
+ SetThreadPriority(*thread_handle, priority);
+ return result;
+}
+
+// static
bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
return CreateThreadInternal(stack_size, delegate, NULL);
}