#include #include "LogFile.h" #include "ThreadMonitor.h" #include "ThreadClass.h" ///////////////////////////////////////////////////////////////////// // ThreadClass ///////////////////////////////////////////////////////////////////// static __thread ThreadClass *thisThread = NULL; static bool _anyThreadStarted = false; bool ThreadClass::anyThreadStarted() { return _anyThreadStarted; } void *ThreadClass::threadAdapter(void *me) { ThreadClass *realMe = static_cast(me); thisThread = realMe; ThreadMonitor::find().setName(realMe->_name); TclList msg; msg<<"Thread" <<"Started" <_name <_thread <threadFunction(); } catch (std::exception &ex) { msg.clear(); msg<<"Thread" <<"Exception" <_name <_thread; LogFile::primary().sendString(msg); LogFile::primary().scheduleShutdown(); } catch (...) { msg.clear(); msg<<"Thread" <<"Exception" <_name <_thread; LogFile::primary().sendString(msg); LogFile::primary().scheduleShutdown(); } msg.clear(); msg<<"Thread" <<"Complete" <_name <_thread; LogFile::primary().sendString(msg); return NULL; } void ThreadClass::startThread() { if (!_threadStarted) { _anyThreadStarted = true; pthread_create(&_thread, NULL, threadAdapter, this); _threadStarted = true; } } void ThreadClass::waitForThread() { // If we created a thread. if (_threadStarted) { // Wait for it to finish. TclList msg; msg<<"Thread" <<"wait_for" <<_name; LogFile::primary().sendString(msg); pthread_join(_thread, NULL); msg.clear(); msg<<"Thread" <<"finished" <<_name; LogFile::primary().sendString(msg); _threadStarted = false; } } ThreadClass::ThreadClass(std::string name) : _name(name), _threadStarted(false) { } ThreadClass::~ThreadClass() { waitForThread(); } ThreadClass *ThreadClass::findThread() { return thisThread; }