Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: webrtc/base/thread.cc

Issue 2833993003: Move autowrap from ThreadManager constructor to Thread::Current. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/base/thread_darwin.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/thread.h" 11 #include "webrtc/base/thread.h"
12 12
13 #if defined(WEBRTC_WIN) 13 #if defined(WEBRTC_WIN)
14 #include <comdef.h> 14 #include <comdef.h>
15 #elif defined(WEBRTC_POSIX) 15 #elif defined(WEBRTC_POSIX)
16 #include <time.h> 16 #include <time.h>
17 #endif 17 #endif
18 18
19 #include "webrtc/base/checks.h" 19 #include "webrtc/base/checks.h"
20 #include "webrtc/base/logging.h" 20 #include "webrtc/base/logging.h"
21 #include "webrtc/base/nullsocketserver.h" 21 #include "webrtc/base/nullsocketserver.h"
22 #include "webrtc/base/platform_thread.h" 22 #include "webrtc/base/platform_thread.h"
23 #include "webrtc/base/stringutils.h" 23 #include "webrtc/base/stringutils.h"
24 #include "webrtc/base/timeutils.h" 24 #include "webrtc/base/timeutils.h"
25 #include "webrtc/base/trace_event.h" 25 #include "webrtc/base/trace_event.h"
26 26
27 namespace rtc { 27 namespace rtc {
28 28
29 ThreadManager* ThreadManager::Instance() { 29 ThreadManager* ThreadManager::Instance() {
30 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ()); 30 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ());
tommi 2017/04/21 09:38:58 I hadn't seen this macro before. It doesn't say "L
nisse-webrtc 2017/04/21 11:06:07 I think I'd prefer to delete this macro, I don't t
31 return &thread_manager; 31 return &thread_manager;
32 } 32 }
33 33
34 ThreadManager::~ThreadManager() {
35 // By above RTC_DEFINE_STATIC_LOCAL.
36 RTC_NOTREACHED() << "ThreadManager should never be destructed.";
Taylor Brandstetter 2017/04/21 09:54:28 But it has a public constructor/destructor, so a n
nisse-webrtc 2017/04/21 11:06:07 I'd prefer to not add the bookkeeping needed to un
Taylor Brandstetter 2017/04/21 11:32:54 Let's try making the constructor/destructor privat
37 }
38
34 // static 39 // static
35 Thread* Thread::Current() { 40 Thread* Thread::Current() {
41 #ifdef NO_MAIN_THREAD_WRAPPING
36 return ThreadManager::Instance()->CurrentThread(); 42 return ThreadManager::Instance()->CurrentThread();
43 #else
44 return ThreadManager::Instance()->WrapCurrentThread();
45 #endif
37 } 46 }
38 47
39 #if defined(WEBRTC_POSIX) 48 #if defined(WEBRTC_POSIX)
40 #if !defined(WEBRTC_MAC) 49 #if !defined(WEBRTC_MAC)
41 ThreadManager::ThreadManager() { 50 ThreadManager::ThreadManager() {
42 pthread_key_create(&key_, nullptr); 51 pthread_key_create(&key_, nullptr);
43 #ifndef NO_MAIN_THREAD_WRAPPING
44 WrapCurrentThread();
45 #endif
46 }
47
48 ThreadManager::~ThreadManager() {
49 UnwrapCurrentThread();
50 pthread_key_delete(key_);
51 } 52 }
52 #endif 53 #endif
53 54
54 Thread *ThreadManager::CurrentThread() { 55 Thread *ThreadManager::CurrentThread() {
55 return static_cast<Thread *>(pthread_getspecific(key_)); 56 return static_cast<Thread *>(pthread_getspecific(key_));
56 } 57 }
57 58
58 void ThreadManager::SetCurrentThread(Thread *thread) { 59 void ThreadManager::SetCurrentThread(Thread *thread) {
59 pthread_setspecific(key_, thread); 60 pthread_setspecific(key_, thread);
60 } 61 }
61 #endif 62 #endif
62 63
63 #if defined(WEBRTC_WIN) 64 #if defined(WEBRTC_WIN)
64 ThreadManager::ThreadManager() { 65 ThreadManager::ThreadManager() {
65 key_ = TlsAlloc(); 66 key_ = TlsAlloc();
66 #ifndef NO_MAIN_THREAD_WRAPPING
67 WrapCurrentThread();
68 #endif
69 }
70
71 ThreadManager::~ThreadManager() {
72 UnwrapCurrentThread();
73 TlsFree(key_);
74 } 67 }
75 68
76 Thread *ThreadManager::CurrentThread() { 69 Thread *ThreadManager::CurrentThread() {
77 return static_cast<Thread *>(TlsGetValue(key_)); 70 return static_cast<Thread *>(TlsGetValue(key_));
78 } 71 }
79 72
80 void ThreadManager::SetCurrentThread(Thread *thread) { 73 void ThreadManager::SetCurrentThread(Thread *thread) {
81 TlsSetValue(key_, thread); 74 TlsSetValue(key_, thread);
82 } 75 }
83 #endif 76 #endif
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 if (SUCCEEDED(hr)) { 522 if (SUCCEEDED(hr)) {
530 Thread::Run(); 523 Thread::Run();
531 CoUninitialize(); 524 CoUninitialize();
532 } else { 525 } else {
533 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; 526 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr;
534 } 527 }
535 } 528 }
536 #endif 529 #endif
537 530
538 } // namespace rtc 531 } // namespace rtc
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/thread_darwin.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698