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