1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// ==============================================================
// 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_
#include "leak_dumper.h"
#include "thread.h"
#include <string>
#include <map>
using namespace Shared::Platform;
using namespace std;
namespace Shared { namespace PlatformCommon {
// =====================================================
// class BaseThread
// =====================================================
class BaseThread : public Thread
{
protected:
Mutex *mutexRunning;
Mutex *mutexQuit;
Mutex *mutexBeginExecution;
Mutex *mutexDeleteSelfOnExecutionDone;
Mutex *mutexThreadObjectAccessor;
bool threadOwnerValid;
Mutex *mutexThreadOwnerValid;
Mutex *mutexExecutingTask;
bool executingTask;
void *ptr;
static Mutex mutexMasterThreadList;
static std::map<void *,int> masterThreadList;
bool quit;
bool running;
string uniqueID;
bool hasBeginExecution;
bool deleteSelfOnExecutionDone;
Mutex *mutexStarted;
bool started;
virtual void setQuitStatus(bool value);
void deleteSelfIfRequired();
void *genericData;
public:
BaseThread();
virtual ~BaseThread();
virtual void execute()=0;
virtual void signalQuit();
virtual bool getQuitStatus();
virtual bool getRunningStatus();
virtual bool getStarted();
virtual void setStarted(bool value);
virtual bool getHasBeginExecution();
virtual void setHasBeginExecution(bool value);
static bool shutdownAndWait(BaseThread *ppThread);
virtual bool shutdownAndWait();
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
virtual bool getDeleteSelfOnExecutionDone();
virtual void setDeleteSelfOnExecutionDone(bool value);
void setUniqueID(string value) { uniqueID = value; }
string getUniqueID() { return uniqueID; }
virtual void setRunningStatus(bool value);
void setExecutingTask(bool value);
bool getExecutingTask();
void setThreadOwnerValid(bool value);
bool getThreadOwnerValid();
Mutex * getMutexThreadOwnerValid();
Mutex * getMutexThreadObjectAccessor();
template <typename T>
T * getGenericData() {
return genericData;
}
template <typename T>
void setGenericData(T *value) {
genericData = value;
}
static bool isThreadDeleted(void *ptr);
};
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);
}
}
};
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);
}
}
};
}}//end namespace
#endif
|