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 |
| 17 /* |
| 18 * This file contains platform-specific implementations for several |
| 19 * methods in rtc::Thread. |
| 20 */ |
| 21 |
| 22 namespace { |
| 23 void InitCocoaMultiThreading() { |
| 24 if ([NSThread isMultiThreaded] == NO) { |
| 25 // The sole purpose of this autorelease pool is to avoid a console |
| 26 // message on Leopard that tells us we're autoreleasing the thread |
| 27 // with no autorelease pool in place. |
| 28 @autoreleasepool { |
| 29 [NSThread detachNewThreadSelector:@selector(class) |
| 30 toTarget:[NSObject class] |
| 31 withObject:nil]; |
| 32 } |
| 33 } |
| 34 |
| 35 RTC_DCHECK([NSThread isMultiThreaded]); |
| 36 } |
| 37 } |
| 38 |
| 39 namespace rtc { |
| 40 |
| 41 ThreadManager::ThreadManager() { |
| 42 pthread_key_create(&key_, nullptr); |
| 43 #ifndef NO_MAIN_THREAD_WRAPPING |
| 44 WrapCurrentThread(); |
| 45 #endif |
| 46 // This is necessary to alert the cocoa runtime of the fact that |
| 47 // we are running in a multithreaded environment. |
| 48 InitCocoaMultiThreading(); |
| 49 } |
| 50 |
| 51 ThreadManager::~ThreadManager() { |
| 52 @autoreleasepool { |
| 53 UnwrapCurrentThread(); |
| 54 pthread_key_delete(key_); |
| 55 } |
| 56 } |
| 57 |
| 58 // static |
| 59 void* Thread::PreRun(void* pv) { |
| 60 ThreadInit* init = static_cast<ThreadInit*>(pv); |
| 61 ThreadManager::Instance()->SetCurrentThread(init->thread); |
| 62 rtc::SetCurrentThreadName(init->thread->name_.c_str()); |
| 63 @autoreleasepool { |
| 64 if (init->runnable) { |
| 65 init->runnable->Run(init->thread); |
| 66 } else { |
| 67 init->thread->Run(); |
| 68 } |
| 69 } |
| 70 delete init; |
| 71 return nullptr; |
| 72 } |
| 73 |
| 74 bool Thread::ProcessMessages(int cmsLoop) { |
| 75 int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
| 76 int cmsNext = cmsLoop; |
| 77 |
| 78 while (true) { |
| 79 @autoreleasepool { |
| 80 Message msg; |
| 81 if (!Get(&msg, cmsNext)) |
| 82 return !IsQuitting(); |
| 83 Dispatch(&msg); |
| 84 |
| 85 if (cmsLoop != kForever) { |
| 86 cmsNext = static_cast<int>(TimeUntil(msEnd)); |
| 87 if (cmsNext < 0) |
| 88 return true; |
| 89 } |
| 90 } |
| 91 } |
| 92 } |
| 93 } // namespace rtc |
OLD | NEW |