Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/debug/trace_event.h" | |
|
joth
2011/07/26 09:53:17
alpha order
| |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 | 17 |
| 17 // If Cocoa is to be used on more than one thread, it must know that the | 18 // If Cocoa is to be used on more than one thread, it must know that the |
| 18 // application is multithreaded. Since it's possible to enter Cocoa code | 19 // application is multithreaded. Since it's possible to enter Cocoa code |
| 19 // from threads created by pthread_thread_create, Cocoa won't necessarily | 20 // from threads created by pthread_thread_create, Cocoa won't necessarily |
| 20 // be aware that the application is multithreaded. Spawning an NSThread is | 21 // be aware that the application is multithreaded. Spawning an NSThread is |
| 21 // enough to get Cocoa to set up for multithreaded operation, so this is done | 22 // enough to get Cocoa to set up for multithreaded operation, so this is done |
| 22 // if necessary before pthread_thread_create spawns any threads. | 23 // if necessary before pthread_thread_create spawns any threads. |
| 23 // | 24 // |
| 24 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html | 25 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html |
| 25 void InitThreading() { | 26 void InitThreading() { |
| 26 static BOOL multithreaded = [NSThread isMultiThreaded]; | 27 static BOOL multithreaded = [NSThread isMultiThreaded]; |
| 27 if (!multithreaded) { | 28 if (!multithreaded) { |
| 28 // +[NSObject class] is idempotent. | 29 // +[NSObject class] is idempotent. |
| 29 [NSThread detachNewThreadSelector:@selector(class) | 30 [NSThread detachNewThreadSelector:@selector(class) |
| 30 toTarget:[NSObject class] | 31 toTarget:[NSObject class] |
| 31 withObject:nil]; | 32 withObject:nil]; |
| 32 multithreaded = YES; | 33 multithreaded = YES; |
| 33 | 34 |
| 34 DCHECK([NSThread isMultiThreaded]); | 35 DCHECK([NSThread isMultiThreaded]); |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 | 38 |
| 38 // static | 39 // static |
| 39 void PlatformThread::SetName(const char* name) { | 40 void PlatformThread::SetName(const char* name) { |
| 41 base::debug::TraceLog::GetInstance()->SetCurrentThreadName(name); | |
| 42 | |
| 40 // pthread_setname_np is only available in 10.6 or later, so test | 43 // pthread_setname_np is only available in 10.6 or later, so test |
| 41 // for it at runtime. | 44 // for it at runtime. |
| 42 int (*dynamic_pthread_setname_np)(const char*); | 45 int (*dynamic_pthread_setname_np)(const char*); |
| 43 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 46 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 44 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 47 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 45 if (!dynamic_pthread_setname_np) | 48 if (!dynamic_pthread_setname_np) |
| 46 return; | 49 return; |
| 47 | 50 |
| 48 // Mac OS X does not expose the length limit of the name, so | 51 // Mac OS X does not expose the length limit of the name, so |
| 49 // hardcode it. | 52 // hardcode it. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 case kThreadPriority_Normal: | 150 case kThreadPriority_Normal: |
| 148 SetPriorityNormal(mach_thread_id); | 151 SetPriorityNormal(mach_thread_id); |
| 149 break; | 152 break; |
| 150 case kThreadPriority_RealtimeAudio: | 153 case kThreadPriority_RealtimeAudio: |
| 151 SetPriorityRealtimeAudio(mach_thread_id); | 154 SetPriorityRealtimeAudio(mach_thread_id); |
| 152 break; | 155 break; |
| 153 } | 156 } |
| 154 } | 157 } |
| 155 | 158 |
| 156 } // namespace base | 159 } // namespace base |
| OLD | NEW |