2007-02-14 00:36:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 by Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
2007-02-18 11:26:57 +00:00
|
|
|
// This is ugly, but I can't help it.
|
2007-02-18 16:04:00 +00:00
|
|
|
inline void ADD_MSEC_TO_XTIME(boost::xtime &xt, unsigned msec)
|
|
|
|
|
{
|
|
|
|
|
xt.sec += msec / 1000;
|
|
|
|
|
xt.nsec += (msec % 1000) * 1000000;
|
|
|
|
|
if (xt.nsec > NANOSECONDS_PER_SECOND)
|
|
|
|
|
{
|
|
|
|
|
xt.sec++;
|
|
|
|
|
xt.nsec -= NANOSECONDS_PER_SECOND;
|
2007-02-18 11:26:57 +00:00
|
|
|
}
|
2007-02-18 16:04:00 +00:00
|
|
|
}
|
2007-02-18 11:26:57 +00:00
|
|
|
|
|
|
|
|
|
2007-02-14 00:36:10 +00:00
|
|
|
// Helper class for thread creation.
|
|
|
|
|
class ThreadStarter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ThreadStarter(Thread &thread) : m_thread(thread) {}
|
|
|
|
|
void operator()()
|
|
|
|
|
{
|
|
|
|
|
m_thread.MainWrapper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Thread &m_thread;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Thread::Thread()
|
|
|
|
|
: m_isTerminatedMutexLock(m_isTerminatedMutex), m_userReqTerminateLock(m_shouldTerminateMutex)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread::~Thread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::Run()
|
|
|
|
|
{
|
|
|
|
|
// Create the boost thread object.
|
|
|
|
|
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
|
|
|
|
|
|
|
|
|
if (!m_threadObj.get())
|
|
|
|
|
m_threadObj.reset(new boost::thread(ThreadStarter(*this)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::SignalTermination()
|
|
|
|
|
{
|
|
|
|
|
// Unlock the shouldTerminateMutex.
|
|
|
|
|
m_userReqTerminateLock.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::Join(unsigned msecTimeout)
|
|
|
|
|
{
|
2007-02-17 23:44:45 +00:00
|
|
|
if (!IsRunning())
|
|
|
|
|
return true;
|
|
|
|
|
|
2007-02-14 00:36:10 +00:00
|
|
|
// Calculate time after timeout
|
|
|
|
|
boost::xtime t;
|
|
|
|
|
boost::xtime_get(&t, boost::TIME_UTC);
|
2007-02-18 11:26:57 +00:00
|
|
|
ADD_MSEC_TO_XTIME(t, msecTimeout);
|
2007-02-14 00:36:10 +00:00
|
|
|
|
|
|
|
|
// Wait for the termination of the application code.
|
|
|
|
|
boost::timed_mutex::scoped_timed_lock lock(m_isTerminatedMutex, t);
|
|
|
|
|
bool tmpIsTerminated = lock.locked();
|
|
|
|
|
|
|
|
|
|
if (tmpIsTerminated)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_threadObjMutex);
|
|
|
|
|
// Wait for "real" termination of the thread.
|
|
|
|
|
if (m_threadObj.get())
|
|
|
|
|
{
|
|
|
|
|
m_threadObj->join();
|
|
|
|
|
m_threadObj.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tmpIsTerminated;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-18 11:26:57 +00:00
|
|
|
void
|
|
|
|
|
Thread::Msleep(unsigned msecs)
|
|
|
|
|
{
|
|
|
|
|
boost::xtime t;
|
|
|
|
|
boost::xtime_get(&t, boost::TIME_UTC);
|
|
|
|
|
ADD_MSEC_TO_XTIME(t, msecs);
|
|
|
|
|
|
|
|
|
|
boost::thread::sleep(t);
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-14 00:36:10 +00:00
|
|
|
void
|
|
|
|
|
Thread::MainWrapper()
|
|
|
|
|
{
|
|
|
|
|
this->Main();
|
|
|
|
|
// Thread has been terminated.
|
|
|
|
|
// Unlock the isTerminated mutex.
|
|
|
|
|
m_isTerminatedMutexLock.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::ShouldTerminate() const
|
|
|
|
|
{
|
|
|
|
|
boost::timed_mutex::scoped_try_lock lock(m_shouldTerminateMutex);
|
|
|
|
|
return lock.locked();
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-16 00:03:30 +00:00
|
|
|
bool
|
|
|
|
|
Thread::IsRunning() const
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
|
|
|
|
return (m_threadObj.get() != NULL);
|
|
|
|
|
}
|
|
|
|
|
|