 Chromium Code Reviews
 Chromium Code Reviews Issue 2784483002:
  Add Darwin thread.h implementation.  (Closed)
    
  
    Issue 2784483002:
  Add Darwin thread.h implementation.  (Closed) 
  | OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/base/thread.h" | |
| 12 | |
| 13 #import <Foundation/Foundation.h> | |
| 14 | |
| 15 #include "webrtc/base/platform_thread.h" | |
| 16 | |
| 
magjed_webrtc
2017/03/28 14:41:15
Maybe add a comment for this file that it contains
 
kthelgason
2017/03/28 14:50:21
Done.
 | |
| 17 namespace { | |
| 18 void InitCocoaMultiThreading() { | |
| 19 if ([NSThread isMultiThreaded] == NO) { | |
| 20 // The sole purpose of this autorelease pool is to avoid a console | |
| 21 // message on Leopard that tells us we're autoreleasing the thread | |
| 22 // with no autorelease pool in place. | |
| 23 @autoreleasepool { | |
| 24 [NSThread detachNewThreadSelector:@selector(class) | |
| 25 toTarget:[NSObject class] | |
| 26 withObject:nil]; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 RTC_DCHECK([NSThread isMultiThreaded]); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 namespace rtc { | |
| 35 | |
| 36 ThreadManager::ThreadManager() { | |
| 37 pthread_key_create(&key_, nullptr); | |
| 38 #ifndef NO_MAIN_THREAD_WRAPPING | |
| 39 WrapCurrentThread(); | |
| 40 #endif | |
| 41 // This is necessary to alert the cocoa runtime of the fact that | |
| 42 // we are running in a multithreaded environment. | |
| 43 InitCocoaMultiThreading(); | |
| 44 } | |
| 45 | |
| 46 ThreadManager::~ThreadManager() { | |
| 47 @autoreleasepool { | |
| 48 UnwrapCurrentThread(); | |
| 49 pthread_key_delete(key_); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 void* Thread::PreRun(void* pv) { | |
| 55 ThreadInit* init = static_cast<ThreadInit*>(pv); | |
| 56 ThreadManager::Instance()->SetCurrentThread(init->thread); | |
| 57 rtc::SetCurrentThreadName(init->thread->name_.c_str()); | |
| 58 @autoreleasepool { | |
| 59 if (init->runnable) { | |
| 60 init->runnable->Run(init->thread); | |
| 61 } else { | |
| 62 init->thread->Run(); | |
| 63 } | |
| 64 } | |
| 65 delete init; | |
| 66 return nullptr; | |
| 67 } | |
| 68 | |
| 69 bool Thread::ProcessMessages(int cmsLoop) { | |
| 70 int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); | |
| 71 int cmsNext = cmsLoop; | |
| 72 | |
| 73 while (true) { | |
| 74 @autoreleasepool { | |
| 75 Message msg; | |
| 76 if (!Get(&msg, cmsNext)) | |
| 77 return !IsQuitting(); | |
| 78 Dispatch(&msg); | |
| 79 | |
| 80 if (cmsLoop != kForever) { | |
| 81 cmsNext = static_cast<int>(TimeUntil(msEnd)); | |
| 82 if (cmsNext < 0) | |
| 83 return true; | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 } // namespace rtc | |
| OLD | NEW |