2011-07-02 14:52:16 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* PokerTH - The open source texas holdem engine *
|
|
|
|
|
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
|
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*****************************************************************************/
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
#include "thread.h"
|
2008-04-03 19:04:17 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper class for thread creation.
|
|
|
|
|
class ThreadStarter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ThreadStarter(Thread &thread) : m_thread(thread) {}
|
2011-02-11 20:02:08 +00:00
|
|
|
void operator()() {
|
2007-11-16 15:24:25 +00:00
|
|
|
m_thread.MainWrapper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Thread &m_thread;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Thread::Thread()
|
2012-03-03 14:16:45 +00:00
|
|
|
: m_isTerminatedSemaphore(0), m_shouldTerminateSemaphore(0)
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread::~Thread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::Run()
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
|
|
|
|
|
|
|
|
|
// Create the boost thread object.
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!m_threadObj.get()) {
|
2007-11-16 15:24:25 +00:00
|
|
|
m_threadObj.reset(new boost::thread(ThreadStarter(*this)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::SignalTermination()
|
|
|
|
|
{
|
2012-03-03 14:16:45 +00:00
|
|
|
m_shouldTerminateSemaphore.post();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::Join(unsigned msecTimeout)
|
|
|
|
|
{
|
|
|
|
|
if (!IsRunning())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
bool tmpIsTerminated;
|
2011-02-11 20:02:08 +00:00
|
|
|
if (msecTimeout == THREAD_WAIT_INFINITE) {
|
2007-11-16 15:24:25 +00:00
|
|
|
// Wait infinitely.
|
2012-03-03 14:16:45 +00:00
|
|
|
m_isTerminatedSemaphore.wait();
|
2007-11-16 15:24:25 +00:00
|
|
|
tmpIsTerminated = true;
|
2011-02-11 20:02:08 +00:00
|
|
|
} else {
|
2008-04-03 19:04:17 +00:00
|
|
|
// Wait for the termination of the application code.
|
2012-03-03 14:16:45 +00:00
|
|
|
tmpIsTerminated = m_isTerminatedSemaphore.timed_wait(boost::posix_time::microsec_clock::universal_time() + boost::posix_time::millisec(msecTimeout));
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
if (tmpIsTerminated) {
|
2007-11-16 15:24:25 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_threadObjMutex);
|
|
|
|
|
// Wait for "real" termination of the thread.
|
2012-03-03 14:16:45 +00:00
|
|
|
if (m_threadObj) {
|
2007-11-16 15:24:25 +00:00
|
|
|
m_threadObj->join();
|
|
|
|
|
m_threadObj.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tmpIsTerminated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::Msleep(unsigned msecs)
|
|
|
|
|
{
|
2011-03-05 12:26:55 +00:00
|
|
|
boost::this_thread::sleep(boost::posix_time::millisec(msecs));
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::MainWrapper()
|
|
|
|
|
{
|
|
|
|
|
this->Main();
|
2012-03-03 14:16:45 +00:00
|
|
|
m_isTerminatedSemaphore.post();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::ShouldTerminate() const
|
|
|
|
|
{
|
2012-03-03 14:16:45 +00:00
|
|
|
return m_shouldTerminateSemaphore.try_wait();
|
2007-11-16 15:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::IsRunning() const
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
|
|
|
|
return (m_threadObj.get() != NULL);
|
|
|
|
|
}
|
|
|
|
|
|