OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 60 } |
61 | 61 |
62 void ProcessThreadImpl::Start() { | 62 void ProcessThreadImpl::Start() { |
63 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 63 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
64 RTC_DCHECK(!thread_.get()); | 64 RTC_DCHECK(!thread_.get()); |
65 if (thread_.get()) | 65 if (thread_.get()) |
66 return; | 66 return; |
67 | 67 |
68 RTC_DCHECK(!stop_); | 68 RTC_DCHECK(!stop_); |
69 | 69 |
70 { | 70 for (ModuleCallback& m : modules_) |
71 // TODO(tommi): Since DeRegisterModule is currently being called from | 71 m.module->ProcessThreadAttached(this); |
72 // different threads in some cases (ChannelOwner), we need to lock access to | |
73 // the modules_ collection even on the controller thread. | |
74 // Once we've cleaned up those places, we can remove this lock. | |
75 rtc::CritScope lock(&lock_); | |
76 for (ModuleCallback& m : modules_) | |
77 m.module->ProcessThreadAttached(this); | |
78 } | |
79 | 72 |
80 thread_.reset( | 73 thread_.reset( |
81 new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_)); | 74 new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_)); |
82 thread_->Start(); | 75 thread_->Start(); |
83 } | 76 } |
84 | 77 |
85 void ProcessThreadImpl::Stop() { | 78 void ProcessThreadImpl::Stop() { |
86 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 79 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
87 if(!thread_.get()) | 80 if(!thread_.get()) |
88 return; | 81 return; |
89 | 82 |
90 { | 83 { |
91 rtc::CritScope lock(&lock_); | 84 rtc::CritScope lock(&lock_); |
92 stop_ = true; | 85 stop_ = true; |
93 } | 86 } |
94 | 87 |
95 wake_up_->Set(); | 88 wake_up_->Set(); |
96 | 89 |
97 thread_->Stop(); | 90 thread_->Stop(); |
98 stop_ = false; | 91 stop_ = false; |
99 | 92 |
100 // TODO(tommi): Since DeRegisterModule is currently being called from | |
101 // different threads in some cases (ChannelOwner), we need to lock access to | |
102 // the modules_ collection even on the controller thread. | |
103 // Since DeRegisterModule also checks thread_, we also need to hold the | |
104 // lock for the .reset() operation. | |
105 // Once we've cleaned up those places, we can remove this lock. | |
106 rtc::CritScope lock(&lock_); | |
107 thread_.reset(); | 93 thread_.reset(); |
108 for (ModuleCallback& m : modules_) | 94 for (ModuleCallback& m : modules_) |
109 m.module->ProcessThreadAttached(nullptr); | 95 m.module->ProcessThreadAttached(nullptr); |
110 } | 96 } |
111 | 97 |
112 void ProcessThreadImpl::WakeUp(Module* module) { | 98 void ProcessThreadImpl::WakeUp(Module* module) { |
113 // Allowed to be called on any thread. | 99 // Allowed to be called on any thread. |
114 { | 100 { |
115 rtc::CritScope lock(&lock_); | 101 rtc::CritScope lock(&lock_); |
116 for (ModuleCallback& m : modules_) { | 102 for (ModuleCallback& m : modules_) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 modules_.push_back(ModuleCallback(module, from)); | 141 modules_.push_back(ModuleCallback(module, from)); |
156 } | 142 } |
157 | 143 |
158 // Wake the thread calling ProcessThreadImpl::Process() to update the | 144 // Wake the thread calling ProcessThreadImpl::Process() to update the |
159 // waiting time. The waiting time for the just registered module may be | 145 // waiting time. The waiting time for the just registered module may be |
160 // shorter than all other registered modules. | 146 // shorter than all other registered modules. |
161 wake_up_->Set(); | 147 wake_up_->Set(); |
162 } | 148 } |
163 | 149 |
164 void ProcessThreadImpl::DeRegisterModule(Module* module) { | 150 void ProcessThreadImpl::DeRegisterModule(Module* module) { |
165 // Allowed to be called on any thread. | 151 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
166 // TODO(tommi): Disallow this ^^^ | |
167 RTC_DCHECK(module); | 152 RTC_DCHECK(module); |
168 | 153 |
169 { | 154 { |
170 rtc::CritScope lock(&lock_); | 155 rtc::CritScope lock(&lock_); |
171 modules_.remove_if([&module](const ModuleCallback& m) { | 156 modules_.remove_if([&module](const ModuleCallback& m) { |
172 return m.module == module; | 157 return m.module == module; |
173 }); | 158 }); |
| 159 } |
174 | 160 |
175 // TODO(tommi): we currently need to hold the lock while calling out to | 161 // Notify the module that it's been detached. |
176 // ProcessThreadAttached. This is to make sure that the thread hasn't been | 162 module->ProcessThreadAttached(nullptr); |
177 // destroyed while we attach the module. Once we can make sure | |
178 // DeRegisterModule isn't being called on arbitrary threads, we can move the | |
179 // |if (thread_.get())| check and ProcessThreadAttached() call outside the | |
180 // lock scope. | |
181 | |
182 // Notify the module that it's been detached. | |
183 if (thread_.get()) | |
184 module->ProcessThreadAttached(nullptr); | |
185 } | |
186 } | 163 } |
187 | 164 |
188 // static | 165 // static |
189 bool ProcessThreadImpl::Run(void* obj) { | 166 bool ProcessThreadImpl::Run(void* obj) { |
190 return static_cast<ProcessThreadImpl*>(obj)->Process(); | 167 return static_cast<ProcessThreadImpl*>(obj)->Process(); |
191 } | 168 } |
192 | 169 |
193 bool ProcessThreadImpl::Process() { | 170 bool ProcessThreadImpl::Process() { |
194 TRACE_EVENT1("webrtc", "ProcessThreadImpl", "name", thread_name_); | 171 TRACE_EVENT1("webrtc", "ProcessThreadImpl", "name", thread_name_); |
195 int64_t now = rtc::TimeMillis(); | 172 int64_t now = rtc::TimeMillis(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 213 } |
237 } | 214 } |
238 | 215 |
239 int64_t time_to_wait = next_checkpoint - rtc::TimeMillis(); | 216 int64_t time_to_wait = next_checkpoint - rtc::TimeMillis(); |
240 if (time_to_wait > 0) | 217 if (time_to_wait > 0) |
241 wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); | 218 wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); |
242 | 219 |
243 return true; | 220 return true; |
244 } | 221 } |
245 } // namespace webrtc | 222 } // namespace webrtc |
OLD | NEW |