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 <dlfcn.h> | 8 #include <dlfcn.h> |
| 9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
| 10 #include <mach/mach_time.h> | 10 #include <mach/mach_time.h> |
| 11 #include <mach/thread_policy.h> | 11 #include <mach/thread_policy.h> |
| 12 | 12 |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/threading/thread_id_name_manager.h" | |
| 15 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 16 #include "base/tracked_objects.h" | 17 #include "base/tracked_objects.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 LazyInstance<ThreadLocalPointer<char> >::Leaky | 23 LazyInstance<ThreadLocalPointer<char> >::Leaky |
| 23 current_thread_name = LAZY_INSTANCE_INITIALIZER; | 24 current_thread_name = LAZY_INSTANCE_INITIALIZER; |
|
jonathan.backer
2012/12/06 16:29:08
Why the duplicated state? Now we have TLS for the
dsinclair
2012/12/06 17:55:05
Done.
Removed the thread locals from each of the
| |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 // If Cocoa is to be used on more than one thread, it must know that the | 28 // If Cocoa is to be used on more than one thread, it must know that the |
| 28 // application is multithreaded. Since it's possible to enter Cocoa code | 29 // application is multithreaded. Since it's possible to enter Cocoa code |
| 29 // from threads created by pthread_thread_create, Cocoa won't necessarily | 30 // from threads created by pthread_thread_create, Cocoa won't necessarily |
| 30 // be aware that the application is multithreaded. Spawning an NSThread is | 31 // be aware that the application is multithreaded. Spawning an NSThread is |
| 31 // enough to get Cocoa to set up for multithreaded operation, so this is done | 32 // enough to get Cocoa to set up for multithreaded operation, so this is done |
| 32 // if necessary before pthread_thread_create spawns any threads. | 33 // if necessary before pthread_thread_create spawns any threads. |
| 33 // | 34 // |
| 34 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html | 35 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html |
| 35 void InitThreading() { | 36 void InitThreading() { |
| 36 static BOOL multithreaded = [NSThread isMultiThreaded]; | 37 static BOOL multithreaded = [NSThread isMultiThreaded]; |
| 37 if (!multithreaded) { | 38 if (!multithreaded) { |
| 38 // +[NSObject class] is idempotent. | 39 // +[NSObject class] is idempotent. |
| 39 [NSThread detachNewThreadSelector:@selector(class) | 40 [NSThread detachNewThreadSelector:@selector(class) |
| 40 toTarget:[NSObject class] | 41 toTarget:[NSObject class] |
| 41 withObject:nil]; | 42 withObject:nil]; |
| 42 multithreaded = YES; | 43 multithreaded = YES; |
| 43 | 44 |
| 44 DCHECK([NSThread isMultiThreaded]); | 45 DCHECK([NSThread isMultiThreaded]); |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 | 48 |
| 48 // static | 49 // static |
| 49 void PlatformThread::SetName(const char* name) { | 50 void PlatformThread::SetName(const char* name) { |
| 50 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | 51 current_thread_name.Pointer()->Set(const_cast<char*>(name)); |
| 51 tracked_objects::ThreadData::InitializeThreadContext(name); | 52 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 52 | 53 |
| 54 ThreadIdNameManager::GetInstance()->SetNameForId(CurrentId(), name); | |
| 55 | |
| 53 // pthread_setname_np is only available in 10.6 or later, so test | 56 // pthread_setname_np is only available in 10.6 or later, so test |
| 54 // for it at runtime. | 57 // for it at runtime. |
| 55 int (*dynamic_pthread_setname_np)(const char*); | 58 int (*dynamic_pthread_setname_np)(const char*); |
| 56 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 59 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 57 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 60 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 58 if (!dynamic_pthread_setname_np) | 61 if (!dynamic_pthread_setname_np) |
| 59 return; | 62 return; |
| 60 | 63 |
| 61 // Mac OS X does not expose the length limit of the name, so | 64 // Mac OS X does not expose the length limit of the name, so |
| 62 // hardcode it. | 65 // hardcode it. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 case kThreadPriority_Normal: | 184 case kThreadPriority_Normal: |
| 182 SetPriorityNormal(mach_thread_id); | 185 SetPriorityNormal(mach_thread_id); |
| 183 break; | 186 break; |
| 184 case kThreadPriority_RealtimeAudio: | 187 case kThreadPriority_RealtimeAudio: |
| 185 SetPriorityRealtimeAudio(mach_thread_id); | 188 SetPriorityRealtimeAudio(mach_thread_id); |
| 186 break; | 189 break; |
| 187 } | 190 } |
| 188 } | 191 } |
| 189 | 192 |
| 190 } // namespace base | 193 } // namespace base |
| OLD | NEW |