niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 6bde7a8 | 2012-02-20 08:39:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/interface/module.h" |
| 12 | #include "webrtc/modules/utility/source/process_thread_impl.h" |
asapersson@webrtc.org | 8b2ec15 | 2014-04-11 07:59:43 +0000 | [diff] [blame] | 13 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | ProcessThread::~ProcessThread() |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | ProcessThread* ProcessThread::CreateProcessThread() |
| 21 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | return new ProcessThreadImpl(); |
| 23 | } |
| 24 | |
| 25 | void ProcessThread::DestroyProcessThread(ProcessThread* module) |
| 26 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | delete module; |
| 28 | } |
| 29 | |
| 30 | ProcessThreadImpl::ProcessThreadImpl() |
| 31 | : _timeEvent(*EventWrapper::Create()), |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 32 | _critSectModules(CriticalSectionWrapper::CreateCriticalSection()), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | _thread(NULL) |
| 34 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | ProcessThreadImpl::~ProcessThreadImpl() |
| 38 | { |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 39 | delete _critSectModules; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | delete &_timeEvent; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 43 | int32_t ProcessThreadImpl::Start() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | { |
| 45 | CriticalSectionScoped lock(_critSectModules); |
| 46 | if(_thread) |
| 47 | { |
| 48 | return -1; |
| 49 | } |
| 50 | _thread = ThreadWrapper::CreateThread(Run, this, kNormalPriority, |
| 51 | "ProcessThread"); |
| 52 | unsigned int id; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 53 | int32_t retVal = _thread->Start(id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | if(retVal >= 0) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | delete _thread; |
| 59 | _thread = NULL; |
| 60 | return -1; |
| 61 | } |
| 62 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 63 | int32_t ProcessThreadImpl::Stop() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | { |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 65 | _critSectModules->Enter(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | if(_thread) |
| 67 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | ThreadWrapper* thread = _thread; |
| 69 | _thread = NULL; |
| 70 | |
| 71 | _timeEvent.Set(); |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 72 | _critSectModules->Leave(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | |
| 74 | if(thread->Stop()) |
| 75 | { |
| 76 | delete thread; |
| 77 | } else { |
| 78 | return -1; |
| 79 | } |
| 80 | } else { |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 81 | _critSectModules->Leave(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 86 | int32_t ProcessThreadImpl::RegisterModule(Module* module) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | CriticalSectionScoped lock(_critSectModules); |
| 89 | |
| 90 | // Only allow module to be registered once. |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 91 | for (ModuleList::iterator iter = _modules.begin(); |
| 92 | iter != _modules.end(); ++iter) { |
| 93 | if(module == *iter) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | { |
| 95 | return -1; |
| 96 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 99 | _modules.push_front(module); |
asapersson@webrtc.org | 8b2ec15 | 2014-04-11 07:59:43 +0000 | [diff] [blame] | 100 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | // Wake the thread calling ProcessThreadImpl::Process() to update the |
| 102 | // waiting time. The waiting time for the just registered module may be |
| 103 | // shorter than all other registered modules. |
| 104 | _timeEvent.Set(); |
| 105 | return 0; |
| 106 | } |
| 107 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 108 | int32_t ProcessThreadImpl::DeRegisterModule(const Module* module) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | CriticalSectionScoped lock(_critSectModules); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 111 | for (ModuleList::iterator iter = _modules.begin(); |
| 112 | iter != _modules.end(); ++iter) { |
| 113 | if(module == *iter) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | { |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 115 | _modules.erase(iter); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 116 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | bool ProcessThreadImpl::Run(void* obj) |
| 123 | { |
| 124 | return static_cast<ProcessThreadImpl*>(obj)->Process(); |
| 125 | } |
| 126 | |
| 127 | bool ProcessThreadImpl::Process() |
| 128 | { |
| 129 | // Wait for the module that should be called next, but don't block thread |
| 130 | // longer than 100 ms. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 131 | int64_t minTimeToNext = 100; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | { |
| 133 | CriticalSectionScoped lock(_critSectModules); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 134 | for (ModuleList::iterator iter = _modules.begin(); |
| 135 | iter != _modules.end(); ++iter) { |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 136 | int64_t timeToNext = (*iter)->TimeUntilNextProcess(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | if(minTimeToNext > timeToNext) |
| 138 | { |
| 139 | minTimeToNext = timeToNext; |
| 140 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | if(minTimeToNext > 0) |
| 145 | { |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 146 | if(kEventError == |
| 147 | _timeEvent.Wait(static_cast<unsigned long>(minTimeToNext))) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | { |
| 149 | return true; |
| 150 | } |
henrika@webrtc.org | 4ff956f | 2013-04-02 11:59:11 +0000 | [diff] [blame] | 151 | CriticalSectionScoped lock(_critSectModules); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | if(!_thread) |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | { |
| 158 | CriticalSectionScoped lock(_critSectModules); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 159 | for (ModuleList::iterator iter = _modules.begin(); |
| 160 | iter != _modules.end(); ++iter) { |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 161 | int64_t timeToNext = (*iter)->TimeUntilNextProcess(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | if(timeToNext < 1) |
| 163 | { |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 164 | (*iter)->Process(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | return true; |
| 169 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 170 | } // namespace webrtc |