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 | { |
| 68 | _thread->SetNotAlive(); |
| 69 | |
| 70 | ThreadWrapper* thread = _thread; |
| 71 | _thread = NULL; |
| 72 | |
| 73 | _timeEvent.Set(); |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 74 | _critSectModules->Leave(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
| 76 | if(thread->Stop()) |
| 77 | { |
| 78 | delete thread; |
| 79 | } else { |
| 80 | return -1; |
| 81 | } |
| 82 | } else { |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 83 | _critSectModules->Leave(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 88 | int32_t ProcessThreadImpl::RegisterModule(Module* module) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | CriticalSectionScoped lock(_critSectModules); |
| 91 | |
| 92 | // Only allow module to be registered once. |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 93 | for (ModuleList::iterator iter = _modules.begin(); |
| 94 | iter != _modules.end(); ++iter) { |
| 95 | if(module == *iter) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | { |
| 97 | return -1; |
| 98 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 101 | _modules.push_front(module); |
asapersson@webrtc.org | 8b2ec15 | 2014-04-11 07:59:43 +0000 | [diff] [blame] | 102 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | // Wake the thread calling ProcessThreadImpl::Process() to update the |
| 104 | // waiting time. The waiting time for the just registered module may be |
| 105 | // shorter than all other registered modules. |
| 106 | _timeEvent.Set(); |
| 107 | return 0; |
| 108 | } |
| 109 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 110 | int32_t ProcessThreadImpl::DeRegisterModule(const Module* module) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | CriticalSectionScoped lock(_critSectModules); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 113 | for (ModuleList::iterator iter = _modules.begin(); |
| 114 | iter != _modules.end(); ++iter) { |
| 115 | if(module == *iter) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | { |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 117 | _modules.erase(iter); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 118 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | bool ProcessThreadImpl::Run(void* obj) |
| 125 | { |
| 126 | return static_cast<ProcessThreadImpl*>(obj)->Process(); |
| 127 | } |
| 128 | |
| 129 | bool ProcessThreadImpl::Process() |
| 130 | { |
| 131 | // Wait for the module that should be called next, but don't block thread |
| 132 | // longer than 100 ms. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 133 | int32_t minTimeToNext = 100; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | { |
| 135 | CriticalSectionScoped lock(_critSectModules); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 136 | for (ModuleList::iterator iter = _modules.begin(); |
| 137 | iter != _modules.end(); ++iter) { |
| 138 | int32_t timeToNext = (*iter)->TimeUntilNextProcess(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | if(minTimeToNext > timeToNext) |
| 140 | { |
| 141 | minTimeToNext = timeToNext; |
| 142 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | if(minTimeToNext > 0) |
| 147 | { |
| 148 | if(kEventError == _timeEvent.Wait(minTimeToNext)) |
| 149 | { |
| 150 | return true; |
| 151 | } |
henrika@webrtc.org | 4ff956f | 2013-04-02 11:59:11 +0000 | [diff] [blame] | 152 | CriticalSectionScoped lock(_critSectModules); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | if(!_thread) |
| 154 | { |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | { |
| 159 | CriticalSectionScoped lock(_critSectModules); |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 160 | for (ModuleList::iterator iter = _modules.begin(); |
| 161 | iter != _modules.end(); ++iter) { |
| 162 | int32_t timeToNext = (*iter)->TimeUntilNextProcess(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | if(timeToNext < 1) |
| 164 | { |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 165 | (*iter)->Process(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 171 | } // namespace webrtc |