1 // ==============================================================
2 //	This file is part of Glest Shared Library (www.glest.org)
3 //
4 //	Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 #ifndef _SHARED_PLATFORMCOMMON_SIMPLETHREAD_H_
12 #define _SHARED_PLATFORMCOMMON_SIMPLETHREAD_H_
13 
14 #include "base_thread.h"
15 #include <vector>
16 #include <string>
17 #include "util.h"
18 #include "texture.h"
19 #include "leak_dumper.h"
20 
21 using namespace std;
22 using namespace Shared::Util;
23 using namespace Shared::Graphics;
24 
25 namespace Shared { namespace PlatformCommon {
26 
27 //
28 // This interface describes the methods a callback object must implement
29 //
30 class FileCRCPreCacheThreadCallbackInterface {
31 public:
32 	virtual vector<Texture2D *> processTech(string techName) = 0;
~FileCRCPreCacheThreadCallbackInterface()33 	virtual ~FileCRCPreCacheThreadCallbackInterface() {}
34 };
35 
36 // =====================================================
37 //	class FileCRCPreCacheThread
38 // =====================================================
39 
40 class FileCRCPreCacheThread : public BaseThread
41 {
42 protected:
43 	vector<string> techDataPaths;
44 	vector<string> workerThreadTechPaths;
45 	FileCRCPreCacheThreadCallbackInterface *processTechCB;
46 
47 	static string preCacheThreadCacheLookupKey;
48 	Mutex *mutexPauseForGame;
49 	bool pauseForGame;
50 	std::vector<FileCRCPreCacheThread *> preCacheWorkerThreadList;
51 
52 public:
53 	FileCRCPreCacheThread();
54 	FileCRCPreCacheThread(vector<string> techDataPaths,vector<string> workerThreadTechPaths,FileCRCPreCacheThreadCallbackInterface *processTechCB);
55 	virtual ~FileCRCPreCacheThread();
56 
setPreCacheThreadCacheLookupKey(string value)57 	static void setPreCacheThreadCacheLookupKey(string value) { preCacheThreadCacheLookupKey = value; }
58 
59     virtual void execute();
setTechDataPaths(vector<string> value)60     void setTechDataPaths(vector<string> value) { this->techDataPaths = value; }
setWorkerThreadTechPaths(vector<string> value)61     void setWorkerThreadTechPaths(vector<string> value) { this->workerThreadTechPaths = value; }
setFileCRCPreCacheThreadCallbackInterface(FileCRCPreCacheThreadCallbackInterface * value)62     void setFileCRCPreCacheThreadCallbackInterface(FileCRCPreCacheThreadCallbackInterface *value) { processTechCB = value; }
63 
64 	virtual bool canShutdown(bool deleteSelfIfShutdownDelayed);
65 
66 	void setPauseForGame(bool pauseForGame);
67 	bool getPauseForGame();
68 };
69 
70 // =====================================================
71 //	class SimpleTaskThread
72 // =====================================================
73 typedef void taskFunctionCallback(BaseThread *callingThread);
74 //
75 // This interface describes the methods a callback object must implement
76 //
77 class SimpleTaskCallbackInterface {
78 public:
79 	virtual void simpleTask(BaseThread *callingThread,void *userdata) = 0;
80 
setupTask(BaseThread * callingThread,void * userdata)81 	virtual void setupTask(BaseThread *callingThread,void *userdata) { }
shutdownTask(BaseThread * callingThread,void * userdata)82 	virtual void shutdownTask(BaseThread *callingThread,void *userdata) { }
83 
~SimpleTaskCallbackInterface()84 	virtual ~SimpleTaskCallbackInterface() {}
85 };
86 
87 class SimpleTaskThread : public BaseThread
88 {
89 protected:
90 
91 	Mutex *mutexSimpleTaskInterfaceValid;
92 	bool simpleTaskInterfaceValid;
93 	SimpleTaskCallbackInterface *simpleTaskInterface;
94 	unsigned int executionCount;
95 	unsigned int millisecsBetweenExecutions;
96 
97 	Mutex *mutexTaskSignaller;
98 	bool taskSignalled;
99 	bool needTaskSignal;
100 
101 	Mutex *mutexLastExecuteTimestamp;
102 	time_t lastExecuteTimestamp;
103 
104 	taskFunctionCallback *overrideShutdownTask;
105 	void *userdata;
106 	bool wantSetupAndShutdown;
107 
108 public:
109 	SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface,
110 					 unsigned int executionCount=0,
111 					 unsigned int millisecsBetweenExecutions=0,
112 					 bool needTaskSignal = false,
113 					 void *userdata=NULL,
114 					 bool wantSetupAndShutdown=true);
115 	virtual ~SimpleTaskThread();
116 
getUserdata()117 	virtual void * getUserdata() { return userdata; }
getUserdataAsInt()118 	virtual int getUserdataAsInt() {
119 		int value = 0;
120 		if(userdata) {
121 			value = *((int*)&userdata);
122 		}
123 		return value;
124 	}
125     virtual void execute();
126     virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
127 
128     void setTaskSignalled(bool value);
129     bool getTaskSignalled();
130 
131     bool isThreadExecutionLagging();
132 
133     void cleanup();
134 
135     void setOverrideShutdownTask(taskFunctionCallback *ptr);
136 
137     bool getSimpleTaskInterfaceValid();
138     void setSimpleTaskInterfaceValid(bool value);
139 };
140 
141 // =====================================================
142 //	class LogFileThread
143 // =====================================================
144 
145 class LogFileEntry {
146 public:
147     SystemFlags::DebugType type;
148     string entry;
149     time_t entryDateTime;
150 };
151 
152 class LogFileThread : public BaseThread
153 {
154 protected:
155 
156     Mutex *mutexLogList;
157 	vector<LogFileEntry> logList;
158 	time_t lastSaveToDisk;
159 
160     void saveToDisk(bool forceSaveAll,bool logListAlreadyLocked);
161     bool checkSaveCurrentLogBufferToDisk();
162 
163 public:
164 	LogFileThread();
165 	virtual ~LogFileThread();
166     virtual void execute();
167     void addLogEntry(SystemFlags::DebugType type, string logEntry);
168     std::size_t getLogEntryBufferCount();
169     virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
170 };
171 
172 }}//end namespace
173 
174 #endif
175