Bugfix: Unlock thread mutex while waiting for events.

This commit is contained in:
Gunnar Beutner 2013-02-16 07:27:45 +01:00
parent d0481ea578
commit 172938b19d
5 changed files with 22 additions and 7 deletions

View File

@ -124,7 +124,7 @@ bool Application::ProcessEvents(void)
if (m_ShuttingDown)
return false;
GetEQ().ProcessEvents(boost::posix_time::milliseconds(sleep * 1000));
GetEQ().ProcessEvents(m_Mutex, boost::posix_time::milliseconds(sleep * 1000));
DynamicObject::FlushTx();

View File

@ -46,24 +46,33 @@ void EventQueue::Stop(void)
* Waits for events using the specified timeout value and processes
* them.
*
* @param mtx The mutex that should be unlocked while waiting. Caller
* must have this mutex locked.
* @param timeout The wait timeout.
* @returns false if the queue has been stopped, true otherwise.
*/
bool EventQueue::ProcessEvents(millisec timeout)
bool EventQueue::ProcessEvents(boost::mutex& mtx, millisec timeout)
{
vector<Callback> events;
mtx.unlock();
{
boost::mutex::scoped_lock lock(m_Mutex);
while (m_Events.empty() && !m_Stopped) {
if (!m_EventAvailable.timed_wait(lock, timeout))
if (!m_EventAvailable.timed_wait(lock, timeout)) {
mtx.lock();
return !m_Stopped;
}
}
events.swap(m_Events);
}
mtx.lock();
BOOST_FOREACH(const Callback& ev, events) {
double st = Utility::GetTime();
@ -100,4 +109,3 @@ void EventQueue::Post(const EventQueue::Callback& callback)
m_EventAvailable.notify_all();
}
}

View File

@ -35,7 +35,7 @@ public:
EventQueue(void);
bool ProcessEvents(millisec timeout = boost::posix_time::milliseconds(30000));
bool ProcessEvents(boost::mutex& mtx, millisec timeout = boost::posix_time::milliseconds(30000));
void Post(const Callback& callback);
void Stop(void);
@ -43,6 +43,8 @@ public:
boost::thread::id GetOwner(void) const;
void SetOwner(boost::thread::id owner);
boost::mutex& GetMutex(void);
private:
boost::thread::id m_Owner;

View File

@ -55,8 +55,12 @@ void ScriptInterpreter::ThreadWorkerProc(void)
{
m_EQ.SetOwner(boost::this_thread::get_id());
while (m_EQ.ProcessEvents())
; /* empty loop */
{
boost::mutex::scoped_lock lock(m_Mutex);
while (m_EQ.ProcessEvents(m_Mutex))
; /* empty loop */
}
}
void ScriptInterpreter::ScriptFunctionThunk(const ScriptTask::Ptr& task,

View File

@ -49,6 +49,7 @@ protected:
void UnsubscribeFunction(const String& name);
private:
boost::mutex m_Mutex;
EventQueue m_EQ;
set<String> m_SubscribedFunctions;
boost::thread m_Thread;