From 42655dd168d884f60c0194e8a74606c68a3e6378 Mon Sep 17 00:00:00 2001 From: erikchen Date: Mon, 14 Mar 2016 15:54:17 -0700 Subject: base: Implement GetCurrentThreadPriority. The Chrome thread priority is saved in the thread dictionary during SetCurrentThreadPriority(). This seemed a better approach than using thread_policy_get(), since there are 4 Chrome priorities which won't cleanly map to thread flavors. BUG=554651 Review URL: https://codereview.chromium.org/1620673003 Cr-Commit-Position: refs/heads/master@{#381102} --- base/threading/platform_thread.h | 2 +- base/threading/platform_thread_mac.mm | 35 +++++++++++++++++++++++++----- base/threading/platform_thread_unittest.cc | 9 +------- 3 files changed, 32 insertions(+), 14 deletions(-) (limited to 'base') diff --git a/base/threading/platform_thread.h b/base/threading/platform_thread.h index e62eb2b..72da93b 100644 --- a/base/threading/platform_thread.h +++ b/base/threading/platform_thread.h @@ -99,7 +99,7 @@ const PlatformThreadId kInvalidThreadId(0); // Valid values for priority of Thread::Options and SimpleThread::Options, and // SetCurrentThreadPriority(), listed in increasing order of importance. -enum class ThreadPriority { +enum class ThreadPriority : int { // Suitable for threads that shouldn't disrupt high priority work. BACKGROUND, // Default priority level. diff --git a/base/threading/platform_thread_mac.mm b/base/threading/platform_thread_mac.mm index 7ab47cb..51f3621 100644 --- a/base/threading/platform_thread_mac.mm +++ b/base/threading/platform_thread_mac.mm @@ -15,6 +15,7 @@ #include "base/lazy_instance.h" #include "base/logging.h" +#include "base/mac/foundation_util.h" #include "base/mac/mach_logging.h" #include "base/threading/thread_id_name_manager.h" #include "base/tracked_objects.h" @@ -22,6 +23,10 @@ namespace base { +namespace { +NSString* const kThreadPriorityKey = @"CrThreadPriorityKey"; +} // namespace + // If Cocoa is to be used on more than one thread, it must know that the // application is multithreaded. Since it's possible to enter Cocoa code // from threads created by pthread_thread_create, Cocoa won't necessarily @@ -164,21 +169,41 @@ void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { switch (priority) { case ThreadPriority::NORMAL: + case ThreadPriority::BACKGROUND: + case ThreadPriority::DISPLAY: + // Add support for non-NORMAL thread priorities. https://crbug.com/554651 SetPriorityNormal(mach_thread_id); break; case ThreadPriority::REALTIME_AUDIO: SetPriorityRealtimeAudio(mach_thread_id); break; - default: - NOTREACHED() << "Unknown priority."; - break; } + + [[[NSThread currentThread] threadDictionary] + setObject:@(static_cast(priority)) + forKey:kThreadPriorityKey]; } // static ThreadPriority PlatformThread::GetCurrentThreadPriority() { - NOTIMPLEMENTED(); - return ThreadPriority::NORMAL; + NSNumber* priority = base::mac::ObjCCast([[[NSThread currentThread] + threadDictionary] objectForKey:kThreadPriorityKey]); + + if (!priority) + return ThreadPriority::NORMAL; + + ThreadPriority thread_priority = + static_cast(priority.intValue); + switch (thread_priority) { + case ThreadPriority::BACKGROUND: + case ThreadPriority::NORMAL: + case ThreadPriority::DISPLAY: + case ThreadPriority::REALTIME_AUDIO: + return thread_priority; + default: + NOTREACHED() << "Unknown priority."; + return ThreadPriority::NORMAL; + } } size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { diff --git a/base/threading/platform_thread_unittest.cc b/base/threading/platform_thread_unittest.cc index 1c68ded..6738775 100644 --- a/base/threading/platform_thread_unittest.cc +++ b/base/threading/platform_thread_unittest.cc @@ -239,16 +239,9 @@ class ThreadPriorityTestThread : public FunctionTestThread { } // namespace -#if defined(OS_MACOSX) -// PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. -#define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread -#else -#define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread -#endif - // Test changing a created thread's priority (which has different semantics on // some platforms). -TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { +TEST(PlatformThreadTest, ThreadPriorityCurrentThread) { const bool bumping_priority_allowed = IsBumpingPriorityAllowed(); if (bumping_priority_allowed) { // Bump the priority in order to verify that new threads are started with -- cgit v1.1