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.h

Issue 2833993003: Move autowrap from ThreadManager constructor to Thread::Current. (Closed)
Patch Set: Limit autowrap to main thread only. New method ThreadManager::IsMainThread. Created 3 years, 7 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.cc » ('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 #ifndef WEBRTC_BASE_THREAD_H_ 11 #ifndef WEBRTC_BASE_THREAD_H_
12 #define WEBRTC_BASE_THREAD_H_ 12 #define WEBRTC_BASE_THREAD_H_
13 13
14 #include <algorithm> 14 #include <algorithm>
15 #include <list> 15 #include <list>
16 #include <memory> 16 #include <memory>
17 #include <string> 17 #include <string>
18 #include <vector> 18 #include <vector>
19 19
20 #if defined(WEBRTC_POSIX) 20 #if defined(WEBRTC_POSIX)
21 #include <pthread.h> 21 #include <pthread.h>
22 #endif 22 #endif
23 #include "webrtc/base/constructormagic.h" 23 #include "webrtc/base/constructormagic.h"
24 #include "webrtc/base/event.h" 24 #include "webrtc/base/event.h"
25 #include "webrtc/base/messagequeue.h" 25 #include "webrtc/base/messagequeue.h"
26 #include "webrtc/base/platform_thread_types.h"
26 27
27 #if defined(WEBRTC_WIN) 28 #if defined(WEBRTC_WIN)
28 #include "webrtc/base/win32.h" 29 #include "webrtc/base/win32.h"
29 #endif 30 #endif
30 31
31 namespace rtc { 32 namespace rtc {
32 33
33 class Thread; 34 class Thread;
34 35
35 class ThreadManager { 36 class ThreadManager {
36 public: 37 public:
37 static const int kForever = -1; 38 static const int kForever = -1;
38 39
39 ThreadManager(); 40 // Singleton, constructor and destructor are private.
40 ~ThreadManager();
41
42 static ThreadManager* Instance(); 41 static ThreadManager* Instance();
43 42
44 Thread* CurrentThread(); 43 Thread* CurrentThread();
45 void SetCurrentThread(Thread* thread); 44 void SetCurrentThread(Thread* thread);
46 45
47 // Returns a thread object with its thread_ ivar set 46 // Returns a thread object with its thread_ ivar set
48 // to whatever the OS uses to represent the thread. 47 // to whatever the OS uses to represent the thread.
49 // If there already *is* a Thread object corresponding to this thread, 48 // If there already *is* a Thread object corresponding to this thread,
50 // this method will return that. Otherwise it creates a new Thread 49 // this method will return that. Otherwise it creates a new Thread
51 // object whose wrapped() method will return true, and whose 50 // object whose wrapped() method will return true, and whose
52 // handle will, on Win32, be opened with only synchronization privileges - 51 // handle will, on Win32, be opened with only synchronization privileges -
53 // if you need more privilegs, rather than changing this method, please 52 // if you need more privilegs, rather than changing this method, please
54 // write additional code to adjust the privileges, or call a different 53 // write additional code to adjust the privileges, or call a different
55 // factory method of your own devising, because this one gets used in 54 // factory method of your own devising, because this one gets used in
56 // unexpected contexts (like inside browser plugins) and it would be a 55 // unexpected contexts (like inside browser plugins) and it would be a
57 // shame to break it. It is also conceivable on Win32 that we won't even 56 // shame to break it. It is also conceivable on Win32 that we won't even
58 // be able to get synchronization privileges, in which case the result 57 // be able to get synchronization privileges, in which case the result
59 // will have a null handle. 58 // will have a null handle.
60 Thread *WrapCurrentThread(); 59 Thread *WrapCurrentThread();
61 void UnwrapCurrentThread(); 60 void UnwrapCurrentThread();
62 61
62 bool IsMainThread();
63
63 private: 64 private:
65 ThreadManager();
66 ~ThreadManager();
67
64 #if defined(WEBRTC_POSIX) 68 #if defined(WEBRTC_POSIX)
65 pthread_key_t key_; 69 pthread_key_t key_;
66 #endif 70 #endif
67 71
68 #if defined(WEBRTC_WIN) 72 #if defined(WEBRTC_WIN)
69 DWORD key_; 73 DWORD key_;
70 #endif 74 #endif
71 75
76 // The thread to potentially autowrap.
77 PlatformThreadRef main_thread_ref_;
78
72 RTC_DISALLOW_COPY_AND_ASSIGN(ThreadManager); 79 RTC_DISALLOW_COPY_AND_ASSIGN(ThreadManager);
73 }; 80 };
74 81
75 struct _SendMessage { 82 struct _SendMessage {
76 _SendMessage() {} 83 _SendMessage() {}
77 Thread *thread; 84 Thread *thread;
78 Message msg; 85 Message msg;
79 bool *ready; 86 bool *ready;
80 }; 87 };
81 88
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // Note that this is a single threaded class. 123 // Note that this is a single threaded class.
117 class ScopedDisallowBlockingCalls { 124 class ScopedDisallowBlockingCalls {
118 public: 125 public:
119 ScopedDisallowBlockingCalls(); 126 ScopedDisallowBlockingCalls();
120 ~ScopedDisallowBlockingCalls(); 127 ~ScopedDisallowBlockingCalls();
121 private: 128 private:
122 Thread* const thread_; 129 Thread* const thread_;
123 const bool previous_state_; 130 const bool previous_state_;
124 }; 131 };
125 132
126 bool IsCurrent() const { 133 bool IsCurrent() const;
127 return Current() == this;
128 }
129 134
130 // Sleeps the calling thread for the specified number of milliseconds, during 135 // Sleeps the calling thread for the specified number of milliseconds, during
131 // which time no processing is performed. Returns false if sleeping was 136 // which time no processing is performed. Returns false if sleeping was
132 // interrupted by a signal (POSIX only). 137 // interrupted by a signal (POSIX only).
133 static bool SleepMs(int millis); 138 static bool SleepMs(int millis);
134 139
135 // Sets the thread's name, for debugging. Must be called before Start(). 140 // Sets the thread's name, for debugging. Must be called before Start().
136 // If |obj| is non-null, its value is appended to |name|. 141 // If |obj| is non-null, its value is appended to |name|.
137 const std::string& name() const { return name_; } 142 const std::string& name() const { return name_; }
138 bool SetName(const std::string& name, const void* obj); 143 bool SetName(const std::string& name, const void* obj);
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 324
320 private: 325 private:
321 SocketServer* old_ss_; 326 SocketServer* old_ss_;
322 327
323 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketServerScope); 328 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketServerScope);
324 }; 329 };
325 330
326 } // namespace rtc 331 } // namespace rtc
327 332
328 #endif // WEBRTC_BASE_THREAD_H_ 333 #endif // WEBRTC_BASE_THREAD_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698