| 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 | 
| 11 #include "webrtc/modules/utility/source/process_thread_impl.h" | 11 #include "webrtc/modules/utility/source/process_thread_impl.h" | 
| 12 | 12 | 
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" | 
|  | 14 #include "webrtc/base/timeutils.h" | 
| 14 #include "webrtc/modules/include/module.h" | 15 #include "webrtc/modules/include/module.h" | 
| 15 #include "webrtc/system_wrappers/include/logging.h" | 16 #include "webrtc/system_wrappers/include/logging.h" | 
| 16 #include "webrtc/system_wrappers/include/tick_util.h" |  | 
| 17 | 17 | 
| 18 namespace webrtc { | 18 namespace webrtc { | 
| 19 namespace { | 19 namespace { | 
| 20 | 20 | 
| 21 // We use this constant internally to signal that a module has requested | 21 // We use this constant internally to signal that a module has requested | 
| 22 // a callback right away.  When this is set, no call to TimeUntilNextProcess | 22 // a callback right away.  When this is set, no call to TimeUntilNextProcess | 
| 23 // should be made, but Process() should be called directly. | 23 // should be made, but Process() should be called directly. | 
| 24 const int64_t kCallProcessImmediately = -1; | 24 const int64_t kCallProcessImmediately = -1; | 
| 25 | 25 | 
| 26 int64_t GetNextCallbackTime(Module* module, int64_t time_now) { | 26 int64_t GetNextCallbackTime(Module* module, int64_t time_now) { | 
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 181       module->ProcessThreadAttached(nullptr); | 181       module->ProcessThreadAttached(nullptr); | 
| 182   } | 182   } | 
| 183 } | 183 } | 
| 184 | 184 | 
| 185 // static | 185 // static | 
| 186 bool ProcessThreadImpl::Run(void* obj) { | 186 bool ProcessThreadImpl::Run(void* obj) { | 
| 187   return static_cast<ProcessThreadImpl*>(obj)->Process(); | 187   return static_cast<ProcessThreadImpl*>(obj)->Process(); | 
| 188 } | 188 } | 
| 189 | 189 | 
| 190 bool ProcessThreadImpl::Process() { | 190 bool ProcessThreadImpl::Process() { | 
| 191   int64_t now = TickTime::MillisecondTimestamp(); | 191   int64_t now = rtc::TimeMillis(); | 
| 192   int64_t next_checkpoint = now + (1000 * 60); | 192   int64_t next_checkpoint = now + (1000 * 60); | 
| 193 | 193 | 
| 194   { | 194   { | 
| 195     rtc::CritScope lock(&lock_); | 195     rtc::CritScope lock(&lock_); | 
| 196     if (stop_) | 196     if (stop_) | 
| 197       return false; | 197       return false; | 
| 198     for (ModuleCallback& m : modules_) { | 198     for (ModuleCallback& m : modules_) { | 
| 199       // TODO(tommi): Would be good to measure the time TimeUntilNextProcess | 199       // TODO(tommi): Would be good to measure the time TimeUntilNextProcess | 
| 200       // takes and dcheck if it takes too long (e.g. >=10ms).  Ideally this | 200       // takes and dcheck if it takes too long (e.g. >=10ms).  Ideally this | 
| 201       // operation should not require taking a lock, so querying all modules | 201       // operation should not require taking a lock, so querying all modules | 
| 202       // should run in a matter of nanoseconds. | 202       // should run in a matter of nanoseconds. | 
| 203       if (m.next_callback == 0) | 203       if (m.next_callback == 0) | 
| 204         m.next_callback = GetNextCallbackTime(m.module, now); | 204         m.next_callback = GetNextCallbackTime(m.module, now); | 
| 205 | 205 | 
| 206       if (m.next_callback <= now || | 206       if (m.next_callback <= now || | 
| 207           m.next_callback == kCallProcessImmediately) { | 207           m.next_callback == kCallProcessImmediately) { | 
| 208         m.module->Process(); | 208         m.module->Process(); | 
| 209         // Use a new 'now' reference to calculate when the next callback | 209         // Use a new 'now' reference to calculate when the next callback | 
| 210         // should occur.  We'll continue to use 'now' above for the baseline | 210         // should occur.  We'll continue to use 'now' above for the baseline | 
| 211         // of calculating how long we should wait, to reduce variance. | 211         // of calculating how long we should wait, to reduce variance. | 
| 212         int64_t new_now = TickTime::MillisecondTimestamp(); | 212         int64_t new_now = rtc::TimeMillis(); | 
| 213         m.next_callback = GetNextCallbackTime(m.module, new_now); | 213         m.next_callback = GetNextCallbackTime(m.module, new_now); | 
| 214       } | 214       } | 
| 215 | 215 | 
| 216       if (m.next_callback < next_checkpoint) | 216       if (m.next_callback < next_checkpoint) | 
| 217         next_checkpoint = m.next_callback; | 217         next_checkpoint = m.next_callback; | 
| 218     } | 218     } | 
| 219 | 219 | 
| 220     while (!queue_.empty()) { | 220     while (!queue_.empty()) { | 
| 221       ProcessTask* task = queue_.front(); | 221       ProcessTask* task = queue_.front(); | 
| 222       queue_.pop(); | 222       queue_.pop(); | 
| 223       lock_.Leave(); | 223       lock_.Leave(); | 
| 224       task->Run(); | 224       task->Run(); | 
| 225       delete task; | 225       delete task; | 
| 226       lock_.Enter(); | 226       lock_.Enter(); | 
| 227     } | 227     } | 
| 228   } | 228   } | 
| 229 | 229 | 
| 230   int64_t time_to_wait = next_checkpoint - TickTime::MillisecondTimestamp(); | 230   int64_t time_to_wait = next_checkpoint - rtc::TimeMillis(); | 
| 231   if (time_to_wait > 0) | 231   if (time_to_wait > 0) | 
| 232     wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); | 232     wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); | 
| 233 | 233 | 
| 234   return true; | 234   return true; | 
| 235 } | 235 } | 
| 236 }  // namespace webrtc | 236 }  // namespace webrtc | 
| OLD | NEW | 
|---|