2010-05-01 00:54:24 +02:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest Shared Library (www.glest.org)
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009-2010 Titus Tscharntke (info@titusgames.de) and
|
|
|
|
// Mark Vejvoda (mark_vejvoda@hotmail.com)
|
|
|
|
//
|
|
|
|
// You can redistribute this code 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
|
|
|
|
// ==============================================================
|
|
|
|
#ifndef _SHARED_PLATFORMCOMMON_BASETHREAD_H_
|
|
|
|
#define _SHARED_PLATFORMCOMMON_BASETHREAD_H_
|
|
|
|
|
2010-09-07 07:25:40 +02:00
|
|
|
#include "leak_dumper.h"
|
2010-05-01 00:54:24 +02:00
|
|
|
#include "thread.h"
|
2010-06-18 20:47:20 +02:00
|
|
|
#include <string>
|
2012-04-12 22:43:19 +02:00
|
|
|
#include <map>
|
2010-05-01 00:54:24 +02:00
|
|
|
|
|
|
|
using namespace Shared::Platform;
|
2010-06-18 20:47:20 +02:00
|
|
|
using namespace std;
|
2010-05-01 00:54:24 +02:00
|
|
|
|
|
|
|
namespace Shared { namespace PlatformCommon {
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class BaseThread
|
|
|
|
// =====================================================
|
|
|
|
|
|
|
|
class BaseThread : public Thread
|
|
|
|
{
|
|
|
|
protected:
|
2013-12-25 07:27:44 +01:00
|
|
|
Mutex *mutexRunning;
|
|
|
|
Mutex *mutexQuit;
|
|
|
|
Mutex *mutexBeginExecution;
|
|
|
|
Mutex *mutexDeleteSelfOnExecutionDone;
|
2011-01-02 07:46:48 +01:00
|
|
|
|
2013-12-25 07:27:44 +01:00
|
|
|
Mutex *mutexThreadObjectAccessor;
|
2011-01-02 07:46:48 +01:00
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
bool threadOwnerValid;
|
2013-12-25 07:27:44 +01:00
|
|
|
Mutex *mutexThreadOwnerValid;
|
2011-01-02 10:33:37 +01:00
|
|
|
|
2013-12-25 07:27:44 +01:00
|
|
|
Mutex *mutexExecutingTask;
|
2011-01-02 07:46:48 +01:00
|
|
|
bool executingTask;
|
2010-05-01 00:54:24 +02:00
|
|
|
|
2012-04-12 22:43:19 +02:00
|
|
|
void *ptr;
|
|
|
|
static Mutex mutexMasterThreadList;
|
|
|
|
static std::map<void *,int> masterThreadList;
|
|
|
|
|
2010-05-01 00:54:24 +02:00
|
|
|
bool quit;
|
|
|
|
bool running;
|
2010-06-18 20:47:20 +02:00
|
|
|
string uniqueID;
|
2010-08-27 00:13:41 +02:00
|
|
|
bool hasBeginExecution;
|
2011-01-02 07:46:48 +01:00
|
|
|
bool deleteSelfOnExecutionDone;
|
2010-05-01 00:54:24 +02:00
|
|
|
|
2013-12-25 07:27:44 +01:00
|
|
|
Mutex *mutexStarted;
|
2013-06-22 05:36:18 +02:00
|
|
|
bool started;
|
|
|
|
|
2010-05-08 11:06:30 +02:00
|
|
|
virtual void setQuitStatus(bool value);
|
2011-01-02 07:46:48 +01:00
|
|
|
void deleteSelfIfRequired();
|
2010-05-01 00:54:24 +02:00
|
|
|
|
2011-11-23 09:00:09 +01:00
|
|
|
void *genericData;
|
|
|
|
|
2013-06-22 05:36:18 +02:00
|
|
|
|
2010-05-01 00:54:24 +02:00
|
|
|
public:
|
|
|
|
BaseThread();
|
2010-06-18 01:24:15 +02:00
|
|
|
virtual ~BaseThread();
|
2010-05-01 00:54:24 +02:00
|
|
|
virtual void execute()=0;
|
|
|
|
|
2010-05-08 11:06:30 +02:00
|
|
|
virtual void signalQuit();
|
|
|
|
virtual bool getQuitStatus();
|
|
|
|
virtual bool getRunningStatus();
|
2011-01-02 07:46:48 +01:00
|
|
|
|
2013-06-22 05:36:18 +02:00
|
|
|
virtual bool getStarted();
|
|
|
|
virtual void setStarted(bool value);
|
|
|
|
|
2010-08-27 00:13:41 +02:00
|
|
|
virtual bool getHasBeginExecution();
|
|
|
|
virtual void setHasBeginExecution(bool value);
|
|
|
|
|
2010-12-05 02:52:38 +01:00
|
|
|
static bool shutdownAndWait(BaseThread *ppThread);
|
|
|
|
virtual bool shutdownAndWait();
|
2011-01-02 07:46:48 +01:00
|
|
|
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
|
|
|
|
|
|
|
|
virtual bool getDeleteSelfOnExecutionDone();
|
|
|
|
virtual void setDeleteSelfOnExecutionDone(bool value);
|
2010-06-18 20:47:20 +02:00
|
|
|
|
|
|
|
void setUniqueID(string value) { uniqueID = value; }
|
|
|
|
string getUniqueID() { return uniqueID; }
|
2010-12-19 00:03:53 +01:00
|
|
|
|
|
|
|
virtual void setRunningStatus(bool value);
|
2011-01-02 07:46:48 +01:00
|
|
|
|
|
|
|
void setExecutingTask(bool value);
|
|
|
|
bool getExecutingTask();
|
|
|
|
|
2011-01-02 10:33:37 +01:00
|
|
|
|
|
|
|
void setThreadOwnerValid(bool value);
|
|
|
|
bool getThreadOwnerValid();
|
|
|
|
Mutex * getMutexThreadOwnerValid();
|
|
|
|
|
2013-12-25 07:27:44 +01:00
|
|
|
Mutex * getMutexThreadObjectAccessor();
|
2011-11-23 09:00:09 +01:00
|
|
|
|
2013-05-17 02:08:26 +02:00
|
|
|
template <typename T>
|
|
|
|
T * getGenericData() {
|
|
|
|
return genericData;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void setGenericData(T *value) {
|
|
|
|
genericData = value;
|
|
|
|
}
|
2012-04-12 22:43:19 +02:00
|
|
|
|
|
|
|
static bool isThreadDeleted(void *ptr);
|
2010-12-19 00:03:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class RunningStatusSafeWrapper {
|
|
|
|
protected:
|
|
|
|
BaseThread *thread;
|
|
|
|
public:
|
|
|
|
|
|
|
|
RunningStatusSafeWrapper(BaseThread *thread) {
|
|
|
|
this->thread = thread;
|
|
|
|
Enable();
|
|
|
|
}
|
|
|
|
~RunningStatusSafeWrapper() {
|
|
|
|
Disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable() {
|
|
|
|
if(this->thread != NULL) {
|
|
|
|
this->thread->setRunningStatus(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Disable() {
|
|
|
|
if(this->thread != NULL) {
|
|
|
|
this->thread->setRunningStatus(false);
|
|
|
|
}
|
|
|
|
}
|
2010-05-01 00:54:24 +02:00
|
|
|
};
|
|
|
|
|
2011-01-02 07:46:48 +01:00
|
|
|
class ExecutingTaskSafeWrapper {
|
|
|
|
protected:
|
|
|
|
BaseThread *thread;
|
|
|
|
public:
|
|
|
|
|
|
|
|
ExecutingTaskSafeWrapper(BaseThread *thread) {
|
|
|
|
this->thread = thread;
|
|
|
|
Enable();
|
|
|
|
}
|
|
|
|
~ExecutingTaskSafeWrapper() {
|
|
|
|
Disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable() {
|
|
|
|
if(this->thread != NULL) {
|
|
|
|
this->thread->setExecutingTask(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Disable() {
|
|
|
|
if(this->thread != NULL) {
|
|
|
|
this->thread->setExecutingTask(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-05-01 00:54:24 +02:00
|
|
|
}}//end namespace
|
|
|
|
|
|
|
|
#endif
|