Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #include <mach/mach.h> | 8 #include <mach/mach.h> |
| 9 #include <mach/mach_time.h> | 9 #include <mach/mach_time.h> |
| 10 #include <mach/thread_policy.h> | 10 #include <mach/thread_policy.h> |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 #include <sys/resource.h> | 12 #include <sys/resource.h> |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 | 15 |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/mac/foundation_util.h" | |
| 18 #include "base/mac/mach_logging.h" | 19 #include "base/mac/mach_logging.h" |
| 19 #include "base/threading/thread_id_name_manager.h" | 20 #include "base/threading/thread_id_name_manager.h" |
| 20 #include "base/tracked_objects.h" | 21 #include "base/tracked_objects.h" |
| 21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 | 25 |
| 26 namespace { | |
| 27 NSString* const kThreadPriorityKey = @"CrThreadPriorityKey"; | |
| 28 } // namespace | |
| 29 | |
| 25 // If Cocoa is to be used on more than one thread, it must know that the | 30 // If Cocoa is to be used on more than one thread, it must know that the |
| 26 // application is multithreaded. Since it's possible to enter Cocoa code | 31 // application is multithreaded. Since it's possible to enter Cocoa code |
| 27 // from threads created by pthread_thread_create, Cocoa won't necessarily | 32 // from threads created by pthread_thread_create, Cocoa won't necessarily |
| 28 // be aware that the application is multithreaded. Spawning an NSThread is | 33 // be aware that the application is multithreaded. Spawning an NSThread is |
| 29 // enough to get Cocoa to set up for multithreaded operation, so this is done | 34 // enough to get Cocoa to set up for multithreaded operation, so this is done |
| 30 // if necessary before pthread_thread_create spawns any threads. | 35 // if necessary before pthread_thread_create spawns any threads. |
| 31 // | 36 // |
| 32 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html | 37 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html |
| 33 void InitThreading() { | 38 void InitThreading() { |
| 34 static BOOL multithreaded = [NSThread isMultiThreaded]; | 39 static BOOL multithreaded = [NSThread isMultiThreaded]; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 } // anonymous namespace | 162 } // anonymous namespace |
| 158 | 163 |
| 159 // static | 164 // static |
| 160 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { | 165 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { |
| 161 // Convert from pthread_t to mach thread identifier. | 166 // Convert from pthread_t to mach thread identifier. |
| 162 mach_port_t mach_thread_id = | 167 mach_port_t mach_thread_id = |
| 163 pthread_mach_thread_np(PlatformThread::CurrentHandle().platform_handle()); | 168 pthread_mach_thread_np(PlatformThread::CurrentHandle().platform_handle()); |
| 164 | 169 |
| 165 switch (priority) { | 170 switch (priority) { |
| 166 case ThreadPriority::NORMAL: | 171 case ThreadPriority::NORMAL: |
| 172 case ThreadPriority::BACKGROUND: | |
| 173 case ThreadPriority::DISPLAY: | |
| 174 // Add support for non-NORMAL thread priorities. https://crbug.com/554651 | |
| 167 SetPriorityNormal(mach_thread_id); | 175 SetPriorityNormal(mach_thread_id); |
| 168 break; | 176 break; |
| 169 case ThreadPriority::REALTIME_AUDIO: | 177 case ThreadPriority::REALTIME_AUDIO: |
| 170 SetPriorityRealtimeAudio(mach_thread_id); | 178 SetPriorityRealtimeAudio(mach_thread_id); |
| 171 break; | 179 break; |
| 172 default: | |
| 173 NOTREACHED() << "Unknown priority."; | |
| 174 break; | |
| 175 } | 180 } |
| 181 | |
| 182 [[[NSThread currentThread] threadDictionary] | |
| 183 setObject:@(static_cast<int>(priority)) | |
| 184 forKey:kThreadPriorityKey]; | |
| 176 } | 185 } |
| 177 | 186 |
| 178 // static | 187 // static |
| 179 ThreadPriority PlatformThread::GetCurrentThreadPriority() { | 188 ThreadPriority PlatformThread::GetCurrentThreadPriority() { |
| 180 NOTIMPLEMENTED(); | 189 NSNumber* priority = base::mac::ObjCCast<NSNumber>([[[NSThread currentThread] |
| 181 return ThreadPriority::NORMAL; | 190 threadDictionary] objectForKey:kThreadPriorityKey]); |
| 191 | |
| 192 if (!priority) | |
| 193 return ThreadPriority::NORMAL; | |
| 194 | |
| 195 ThreadPriority thread_priority = | |
| 196 static_cast<ThreadPriority>(priority.intValue); | |
| 197 switch (thread_priority) { | |
| 198 case ThreadPriority::BACKGROUND: | |
| 199 case ThreadPriority::NORMAL: | |
| 200 case ThreadPriority::DISPLAY: | |
| 201 case ThreadPriority::REALTIME_AUDIO: | |
| 202 return thread_priority; | |
| 203 default: | |
| 204 NOTREACHED() << "Unknown priority."; | |
| 205 return ThreadPriority::NORMAL; | |
|
gab
2016/03/15 02:37:07
nit: Prefer putting these two lines outside the sw
| |
| 206 } | |
| 182 } | 207 } |
| 183 | 208 |
| 184 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { | 209 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { |
| 185 #if defined(OS_IOS) | 210 #if defined(OS_IOS) |
| 186 return 0; | 211 return 0; |
| 187 #else | 212 #else |
| 188 // The Mac OS X default for a pthread stack size is 512kB. | 213 // The Mac OS X default for a pthread stack size is 512kB. |
| 189 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses | 214 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses |
| 190 // DEFAULT_STACK_SIZE for this purpose. | 215 // DEFAULT_STACK_SIZE for this purpose. |
| 191 // | 216 // |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 213 static_cast<size_t>(stack_rlimit.rlim_cur)); | 238 static_cast<size_t>(stack_rlimit.rlim_cur)); |
| 214 } | 239 } |
| 215 return default_stack_size; | 240 return default_stack_size; |
| 216 #endif | 241 #endif |
| 217 } | 242 } |
| 218 | 243 |
| 219 void TerminateOnThread() { | 244 void TerminateOnThread() { |
| 220 } | 245 } |
| 221 | 246 |
| 222 } // namespace base | 247 } // namespace base |
| OLD | NEW |