2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
|
|
|
|
#include "tthread.h"
|
|
|
|
#include "tthreadP.h"
|
|
|
|
|
|
|
|
#include "boost/thread/thread.hpp"
|
|
|
|
#include "boost/thread/condition.hpp"
|
|
|
|
#include "boost/thread/tss.hpp"
|
|
|
|
#include "boost/thread/xtime.hpp"
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#define WM_THREAD_NOTIFICATION (WM_USER + 10)
|
|
|
|
#else
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEFINE_CLASS_CODE(TThread::Runnable, 21)
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef WIN32
|
|
|
|
HWND MainHandle;
|
|
|
|
#else
|
|
|
|
Display *TheDisplay;
|
|
|
|
Window TheMainWindow;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class Thread {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Thread();
|
|
|
|
Thread(const TThread::RunnableP &runnable);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
~Thread();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void join();
|
|
|
|
void cancel();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2017-05-09 00:13:29 +12:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1200)
|
2016-03-19 06:57:51 +13:00
|
|
|
#pragma warning(disable : 4290)
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
static void milestone() throw(TThread::Interrupt);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2017-05-09 00:13:29 +12:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1200)
|
2016-03-19 06:57:51 +13:00
|
|
|
#pragma warning(default : 4290)
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class Imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
friend class ThreadGroup;
|
|
|
|
Imp *m_imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class ThreadGroup {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
ThreadGroup();
|
|
|
|
~ThreadGroup();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void add(Thread *thread);
|
|
|
|
void joinAll();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
class Imp;
|
|
|
|
Imp *m_imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-15 18:43:10 +12:00
|
|
|
class QueueT {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
QueueT(int slotCount)
|
|
|
|
: m_items()
|
|
|
|
, m_slotCount(slotCount)
|
|
|
|
, m_notEmpty()
|
|
|
|
, m_notFull()
|
|
|
|
, m_mutex() {}
|
|
|
|
|
|
|
|
~QueueT() {}
|
|
|
|
|
|
|
|
void put(const T &item) {
|
|
|
|
TThread::ScopedLock sl(m_mutex);
|
|
|
|
while (m_items.size() == m_slotCount) m_notFull.wait(sl);
|
|
|
|
m_items.push(item);
|
|
|
|
m_notEmpty.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
T get() {
|
|
|
|
TThread::ScopedLock sl(m_mutex);
|
|
|
|
|
|
|
|
while (m_items.size() == 0) m_notEmpty.wait(sl);
|
|
|
|
|
|
|
|
m_notFull.notify_one();
|
|
|
|
T item = m_items.front();
|
|
|
|
m_items.pop();
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size() {
|
|
|
|
TThread::ScopedLock sl(m_mutex);
|
|
|
|
int size = m_items.size();
|
|
|
|
return size;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
std::queue<T> m_items;
|
|
|
|
TThread::Condition m_notEmpty;
|
|
|
|
TThread::Condition m_notFull;
|
|
|
|
TThread::Mutex m_mutex;
|
|
|
|
int m_slotCount;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // anonymous namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::setMainThreadId(TThread::ThreadInfo *info) {
|
|
|
|
assert(info);
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
MainHandle = info->mainHandle;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
TheDisplay = info->dpy;
|
|
|
|
TheMainWindow = info->win;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
ULONG TThread::getMainShellHandle() { return ULONG(MainHandle); }
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class BoostRunnable {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
BoostRunnable(const TThread::RunnableP &runnable, Thread::Imp *threadImp)
|
|
|
|
: m_runnable(runnable), m_threadImp(threadImp) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void operator()();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::RunnableP m_runnable;
|
|
|
|
Thread::Imp *m_threadImp;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class Thread::Imp {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Imp() : m_boostThread(0), m_isCanceled(false), m_stateMutex() {}
|
|
|
|
|
|
|
|
Imp(const TThread::RunnableP &runnable)
|
|
|
|
: m_isCanceled(false), m_stateMutex() {
|
|
|
|
m_boostThread = new boost::thread(BoostRunnable(runnable, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
~Imp() {
|
|
|
|
if (m_boostThread) delete m_boostThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::thread *m_boostThread;
|
|
|
|
boost::mutex m_stateMutex;
|
|
|
|
bool m_isCanceled;
|
|
|
|
long m_kkkk;
|
|
|
|
|
|
|
|
enum State { Running, Canceled };
|
|
|
|
|
|
|
|
static boost::mutex m_mutex;
|
|
|
|
static std::map<long, State> m_state;
|
|
|
|
|
|
|
|
class Key {
|
|
|
|
public:
|
|
|
|
Key(long id) : m_id(id) {}
|
|
|
|
long m_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
static boost::thread_specific_ptr<Key> m_key;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
boost::mutex Thread::Imp::m_mutex;
|
|
|
|
std::map<long, Thread::Imp::State> Thread::Imp::m_state;
|
|
|
|
|
|
|
|
boost::thread_specific_ptr<Thread::Imp::Key> Thread::Imp::m_key;
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void BoostRunnable::operator()() {
|
|
|
|
Thread::Imp::m_key.reset(new Thread::Imp::Key(reinterpret_cast<long>(this)));
|
|
|
|
m_threadImp->m_kkkk = reinterpret_cast<long>(this);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock sl(Thread::Imp::m_mutex);
|
|
|
|
Thread::Imp::m_state[m_threadImp->m_kkkk] = Thread::Imp::Running;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
assert(m_runnable);
|
|
|
|
m_runnable->run();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock sl(Thread::Imp::m_mutex);
|
|
|
|
Thread::Imp::m_state.erase(reinterpret_cast<long>(this));
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Thread::Thread() : m_imp(new Thread::Imp) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Thread::Thread(const TThread::RunnableP &runnable) : m_imp(new Imp(runnable)) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Thread::~Thread() { delete m_imp; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void Thread::join() {
|
|
|
|
assert(m_imp->m_boostThread);
|
|
|
|
m_imp->m_boostThread->join();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void Thread::cancel() {
|
|
|
|
boost::mutex::scoped_lock sl(Thread::Imp::m_mutex);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::map<long, Thread::Imp::State>::iterator it =
|
|
|
|
Thread::Imp::m_state.find(m_imp->m_kkkk);
|
|
|
|
if (it != Thread::Imp::m_state.end())
|
|
|
|
Thread::Imp::m_state[m_imp->m_kkkk] = Thread::Imp::Canceled;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// static member function
|
2017-05-09 00:13:29 +12:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1200)
|
2016-03-19 06:57:51 +13:00
|
|
|
#pragma warning(disable : 4290)
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void Thread::milestone() throw(TThread::Interrupt) {
|
|
|
|
boost::mutex::scoped_lock sl(Thread::Imp::m_mutex);
|
|
|
|
Thread::Imp::Key key = *Thread::Imp::m_key.get();
|
|
|
|
Thread::Imp::m_state.find(key.m_id);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (Thread::Imp::m_state[key.m_id]) throw TThread::Interrupt();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:13:29 +12:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1200)
|
2016-03-19 06:57:51 +13:00
|
|
|
#pragma warning(default : 4290)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class ThreadGroup::Imp {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Imp() : m_boostThreadGroup() {}
|
|
|
|
~Imp() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
boost::thread_group m_boostThreadGroup;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
ThreadGroup::ThreadGroup() : m_imp(new Imp) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
ThreadGroup::~ThreadGroup() { delete m_imp; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void ThreadGroup::add(Thread *thread) {
|
|
|
|
m_imp->m_boostThreadGroup.add_thread(thread->m_imp->m_boostThread);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void ThreadGroup::joinAll() { m_imp->m_boostThreadGroup.join_all(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2017-05-09 00:13:29 +12:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1200)
|
2016-03-19 06:57:51 +13:00
|
|
|
#pragma warning(disable : 4290)
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::milestone() throw(TThread::Interrupt) { Thread::milestone(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2017-05-09 00:13:29 +12:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1200)
|
2016-03-19 06:57:51 +13:00
|
|
|
#pragma warning(default : 4290)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class TThread::Mutex::Imp {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
boost::mutex m_mutex;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Imp() : m_mutex() {}
|
|
|
|
~Imp() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Mutex::Mutex() : m_imp(new Imp) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Mutex::~Mutex() { delete m_imp; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class TThread::ScopedLock::Imp {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
boost::mutex::scoped_lock *m_sl;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Imp(boost::mutex &mutex) : m_sl(new boost::mutex::scoped_lock(mutex)) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
~Imp() {
|
|
|
|
m_sl->unlock();
|
|
|
|
delete m_sl;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::ScopedLock::ScopedLock(Mutex &mutex)
|
|
|
|
: m_imp(new Imp(mutex.m_imp->m_mutex)) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::ScopedLock::~ScopedLock() { delete m_imp; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class TThread::Condition::Imp {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
boost::condition m_condition;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Imp() : m_condition() {}
|
|
|
|
~Imp() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Condition::Condition() : m_imp(new Imp()) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Condition::~Condition() { delete m_imp; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Condition::wait(ScopedLock &lock) {
|
|
|
|
m_imp->m_condition.wait(*(lock.m_imp->m_sl));
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TThread::Condition::wait(ScopedLock &lock, long timeout) {
|
|
|
|
boost::xtime xt;
|
|
|
|
boost::xtime_get(&xt, boost::TIME_UTC);
|
|
|
|
xt.nsec += timeout * 1000;
|
|
|
|
xt.sec += timeout / 1000;
|
|
|
|
return m_imp->m_condition.timed_wait(*(lock.m_imp->m_sl), xt);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Condition::notifyOne() { m_imp->m_condition.notify_one(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Condition::notifyAll() { m_imp->m_condition.notify_all(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Msg::Msg() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Msg::send() {
|
|
|
|
Msg *msg = clone();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
/*
|
|
|
|
Non viene utilizzato PostThreadMessage perche' se l'applicazione
|
|
|
|
si trova in un modal loop (esempio MessageBox) oppure si sta
|
2016-03-19 06:57:51 +13:00
|
|
|
facendo move o resize di una finestra i messaggi non giungono al
|
|
|
|
message loop.
|
2016-06-15 18:43:10 +12:00
|
|
|
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q183116&
|
2016-03-19 06:57:51 +13:00
|
|
|
*/
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
/*
|
|
|
|
BOOL rc = PostThreadMessage(
|
|
|
|
getMainThreadId(), // thread identifier
|
|
|
|
WM_THREAD_NOTIFICATION, // message
|
|
|
|
WPARAM(msg), // first message parameter
|
|
|
|
0); // second message parameter
|
2016-03-19 06:57:51 +13:00
|
|
|
*/
|
2016-06-15 18:43:10 +12:00
|
|
|
PostMessage(HWND(getMainShellHandle()), WM_THREAD_NOTIFICATION, WPARAM(msg),
|
|
|
|
0);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
XClientMessageEvent clientMsg;
|
|
|
|
clientMsg.type = ClientMessage;
|
|
|
|
clientMsg.window = TheMainWindow;
|
|
|
|
clientMsg.format = 32;
|
|
|
|
clientMsg.message_type = Msg::MsgId();
|
|
|
|
clientMsg.data.l[0] = (long)msg;
|
|
|
|
// Status status =
|
|
|
|
XSendEvent(TheDisplay, TheMainWindow, 0, NoEventMask, (XEvent *)&clientMsg);
|
|
|
|
XFlush(TheDisplay);
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// statica
|
2016-06-15 18:43:10 +12:00
|
|
|
UINT TThread::Msg::MsgId() {
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return WM_THREAD_NOTIFICATION;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
static Atom atom = 0;
|
|
|
|
if (!atom) {
|
|
|
|
atom = XInternAtom(TheDisplay, "ThreadMessage", false);
|
|
|
|
assert(atom);
|
|
|
|
}
|
|
|
|
return atom;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class TThread::Executor::Imp : public TSmartObject {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef TSmartPointerT<TThread::Executor::Imp> ImpP;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//---------------------------------------------------
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class Worker : public Runnable {
|
|
|
|
public:
|
|
|
|
Worker(ImpP owner) : Runnable(), m_owner(owner) {}
|
|
|
|
~Worker() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void run();
|
|
|
|
void doCleanup();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
ImpP m_owner;
|
|
|
|
};
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//---------------------------------------------------
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool m_suspend;
|
|
|
|
bool m_threadHasToDie;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
UINT m_threadCount;
|
|
|
|
Mutex m_mutex;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Condition m_cond;
|
|
|
|
Condition m_taskQueueEmpty;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//------
|
|
|
|
Condition m_taskQueueNotEmpty;
|
|
|
|
//------
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::queue<TThread::RunnableP> m_tasks;
|
|
|
|
std::map<long, Thread *> m_workerThreads;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Imp(int threadsCount, bool suspend)
|
|
|
|
: TSmartObject()
|
|
|
|
, m_suspend(suspend)
|
|
|
|
, m_threadHasToDie(false)
|
|
|
|
, m_threadCount(threadsCount)
|
|
|
|
, m_tasks()
|
|
|
|
, m_workerThreads()
|
|
|
|
, m_mutex()
|
|
|
|
, m_cond(){};
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
~Imp() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Executor::Executor(int threadsCount, bool suspend)
|
|
|
|
: m_imp(new Imp(threadsCount, suspend)) {
|
|
|
|
m_imp->addRef();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Executor::~Executor() {
|
|
|
|
{
|
|
|
|
TThread::ScopedLock sl(m_imp->m_mutex);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (m_imp->m_suspend) {
|
|
|
|
m_imp->m_threadHasToDie = true;
|
|
|
|
m_imp->m_taskQueueNotEmpty.notifyAll();
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_imp->release();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Executor::Imp::Worker::run() {
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
// check if thread has been canceled
|
|
|
|
Thread::milestone();
|
|
|
|
|
|
|
|
// get the next task
|
|
|
|
RunnableP task = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
ScopedLock sl(m_owner->m_mutex);
|
|
|
|
if (m_owner->m_tasks.empty()) {
|
|
|
|
// la lista di task e' stata esaurita
|
|
|
|
if (m_owner->m_suspend) {
|
|
|
|
// il thread deve sospendersi
|
|
|
|
m_owner->m_taskQueueNotEmpty.wait(sl);
|
|
|
|
|
|
|
|
// a questo punto il thread e' stato risvegliato
|
|
|
|
if (m_owner->m_threadHasToDie) {
|
|
|
|
doCleanup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// il thread sta per morire -> bisogna eliminarlo dalla lista dei
|
|
|
|
// worker thread
|
|
|
|
doCleanup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_owner->m_tasks.empty()) {
|
|
|
|
task = m_owner->m_tasks.front();
|
|
|
|
m_owner->m_tasks.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task) task->run();
|
|
|
|
|
|
|
|
// check if thread has been canceled
|
|
|
|
Thread::milestone();
|
|
|
|
}
|
|
|
|
} catch (TThread::Interrupt &) {
|
|
|
|
// m_owner->m_cond.notifyOne();
|
|
|
|
} catch (...) {
|
|
|
|
// eccezione non prevista --> bisogna eliminare il thread
|
|
|
|
// dalla lista dei worker thread
|
|
|
|
|
|
|
|
ScopedLock sl(m_owner->m_mutex);
|
|
|
|
doCleanup();
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Executor::Imp::Worker::doCleanup() {
|
|
|
|
std::map<long, Thread *>::iterator it =
|
|
|
|
m_owner->m_workerThreads.find(reinterpret_cast<long>(this));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (it != m_owner->m_workerThreads.end()) {
|
|
|
|
Thread *thread = it->second;
|
|
|
|
delete thread;
|
|
|
|
m_owner->m_workerThreads.erase(it);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (m_owner->m_workerThreads.size() == 0)
|
|
|
|
m_owner->m_taskQueueEmpty.notifyAll();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Executor::addTask(const RunnableP &task) {
|
|
|
|
TThread::ScopedLock sl(m_imp->m_mutex);
|
|
|
|
m_imp->m_tasks.push(task);
|
|
|
|
if (m_imp->m_workerThreads.size() < m_imp->m_threadCount) {
|
|
|
|
TThread::Executor::Imp::Worker *worker =
|
|
|
|
new TThread::Executor::Imp::Worker(m_imp);
|
|
|
|
|
|
|
|
m_imp->m_workerThreads[reinterpret_cast<long>(worker)] = new Thread(worker);
|
|
|
|
} else {
|
|
|
|
if (m_imp->m_suspend)
|
|
|
|
// risveglia uno dei thread in attesa
|
|
|
|
m_imp->m_taskQueueNotEmpty.notifyOne();
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Executor::clear() {
|
|
|
|
ScopedLock sl(m_imp->m_mutex);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
while (!m_imp->m_tasks.empty()) m_imp->m_tasks.pop();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Executor::cancel() {
|
|
|
|
{
|
|
|
|
ScopedLock sl(m_imp->m_mutex);
|
|
|
|
|
|
|
|
while (!m_imp->m_tasks.empty()) m_imp->m_tasks.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Thread *thread = 0;
|
|
|
|
{
|
|
|
|
ScopedLock sl(m_imp->m_mutex);
|
|
|
|
if (m_imp->m_workerThreads.empty())
|
|
|
|
break;
|
|
|
|
else {
|
|
|
|
std::map<long, Thread *>::iterator it = m_imp->m_workerThreads.begin();
|
|
|
|
thread = it->second;
|
|
|
|
m_imp->m_workerThreads.erase(it);
|
|
|
|
|
|
|
|
if (thread) thread->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TThread::Executor::wait() {
|
|
|
|
TThread::ScopedLock sl(m_imp->m_mutex);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
while (m_imp->m_workerThreads.size()) m_imp->m_taskQueueEmpty.wait(sl);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool TThread::Executor::wait(long timeout) {
|
|
|
|
TThread::ScopedLock sl(m_imp->m_mutex);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool expired = false;
|
|
|
|
while (m_imp->m_workerThreads.size())
|
|
|
|
expired = m_imp->m_taskQueueEmpty.wait(sl, timeout);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return expired;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int TThread::Executor::getThreadCount() {
|
|
|
|
TThread::ScopedLock sl(m_imp->m_mutex);
|
|
|
|
return m_imp->m_workerThreads.size();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int TThread::Executor::getTaskCount() {
|
|
|
|
TThread::ScopedLock sl(m_imp->m_mutex);
|
|
|
|
return m_imp->m_tasks.size();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|