File: simple_threads.h

package info (click to toggle)
megaglest 3.13.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,416 kB
  • sloc: cpp: 144,271; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 22
file content (174 lines) | stat: -rw-r--r-- 5,069 bytes parent folder | download | duplicates (6)
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
167
168
169
170
171
172
173
174
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2005 Matthias Braun <matze@braunis.de>
//
//	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_SIMPLETHREAD_H_
#define _SHARED_PLATFORMCOMMON_SIMPLETHREAD_H_

#include "base_thread.h"
#include <vector>
#include <string>
#include "util.h"
#include "texture.h"
#include "leak_dumper.h"

using namespace std;
using namespace Shared::Util;
using namespace Shared::Graphics;

namespace Shared { namespace PlatformCommon {

//
// This interface describes the methods a callback object must implement
//
class FileCRCPreCacheThreadCallbackInterface {
public:
	virtual vector<Texture2D *> processTech(string techName) = 0;
	virtual ~FileCRCPreCacheThreadCallbackInterface() {}
};

// =====================================================
//	class FileCRCPreCacheThread
// =====================================================

class FileCRCPreCacheThread : public BaseThread
{
protected:
	vector<string> techDataPaths;
	vector<string> workerThreadTechPaths;
	FileCRCPreCacheThreadCallbackInterface *processTechCB;

	static string preCacheThreadCacheLookupKey;
	Mutex *mutexPauseForGame;
	bool pauseForGame;
	std::vector<FileCRCPreCacheThread *> preCacheWorkerThreadList;

public:
	FileCRCPreCacheThread();
	FileCRCPreCacheThread(vector<string> techDataPaths,vector<string> workerThreadTechPaths,FileCRCPreCacheThreadCallbackInterface *processTechCB);
	virtual ~FileCRCPreCacheThread();

	static void setPreCacheThreadCacheLookupKey(string value) { preCacheThreadCacheLookupKey = value; }

    virtual void execute();
    void setTechDataPaths(vector<string> value) { this->techDataPaths = value; }
    void setWorkerThreadTechPaths(vector<string> value) { this->workerThreadTechPaths = value; }
    void setFileCRCPreCacheThreadCallbackInterface(FileCRCPreCacheThreadCallbackInterface *value) { processTechCB = value; }

	virtual bool canShutdown(bool deleteSelfIfShutdownDelayed);

	void setPauseForGame(bool pauseForGame);
	bool getPauseForGame();
};

// =====================================================
//	class SimpleTaskThread
// =====================================================
typedef void taskFunctionCallback(BaseThread *callingThread);
//
// This interface describes the methods a callback object must implement
//
class SimpleTaskCallbackInterface {
public:
	virtual void simpleTask(BaseThread *callingThread,void *userdata) = 0;

	virtual void setupTask(BaseThread *callingThread,void *userdata) { }
	virtual void shutdownTask(BaseThread *callingThread,void *userdata) { }

	virtual ~SimpleTaskCallbackInterface() {}
};

class SimpleTaskThread : public BaseThread
{
protected:

	Mutex *mutexSimpleTaskInterfaceValid;
	bool simpleTaskInterfaceValid;
	SimpleTaskCallbackInterface *simpleTaskInterface;
	unsigned int executionCount;
	unsigned int millisecsBetweenExecutions;

	Mutex *mutexTaskSignaller;
	bool taskSignalled;
	bool needTaskSignal;

	Mutex *mutexLastExecuteTimestamp;
	time_t lastExecuteTimestamp;

	taskFunctionCallback *overrideShutdownTask;
	void *userdata;
	bool wantSetupAndShutdown;

public:
	SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface,
					 unsigned int executionCount=0,
					 unsigned int millisecsBetweenExecutions=0,
					 bool needTaskSignal = false,
					 void *userdata=NULL,
					 bool wantSetupAndShutdown=true);
	virtual ~SimpleTaskThread();

	virtual void * getUserdata() { return userdata; }
	virtual int getUserdataAsInt() {
		int value = 0;
		if(userdata) {
			value = *((int*)&userdata);
		}
		return value;
	}
    virtual void execute();
    virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);

    void setTaskSignalled(bool value);
    bool getTaskSignalled();

    bool isThreadExecutionLagging();

    void cleanup();

    void setOverrideShutdownTask(taskFunctionCallback *ptr);

    bool getSimpleTaskInterfaceValid();
    void setSimpleTaskInterfaceValid(bool value);
};

// =====================================================
//	class LogFileThread
// =====================================================

class LogFileEntry {
public:
    SystemFlags::DebugType type;
    string entry;
    time_t entryDateTime;
};

class LogFileThread : public BaseThread
{
protected:

    Mutex *mutexLogList;
	vector<LogFileEntry> logList;
	time_t lastSaveToDisk;

    void saveToDisk(bool forceSaveAll,bool logListAlreadyLocked);
    bool checkSaveCurrentLogBufferToDisk();

public:
	LogFileThread();
	virtual ~LogFileThread();
    virtual void execute();
    void addLogEntry(SystemFlags::DebugType type, string logEntry);
    std::size_t getLogEntryBufferCount();
    virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
};

}}//end namespace

#endif