summaryrefslogtreecommitdiff
path: root/src/util/thread.h
blob: faa5869ca8981bc5aa44e0e1964fead235abe71a (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef UTIL_THREAD_HEADER
#define UTIL_THREAD_HEADER

#include "../irrlichttypes.h"
#include "../jthread/jthread.h"
#include "../jthread/jmutex.h"
#include "../jthread/jmutexautolock.h"
#include "porting.h"
#include "log.h"

template<typename T>
class MutexedVariable
{
public:
	MutexedVariable(T value):
		m_value(value)
	{
	}

	T get()
	{
		JMutexAutoLock lock(m_mutex);
		return m_value;
	}

	void set(T value)
	{
		JMutexAutoLock lock(m_mutex);
		m_value = value;
	}
	
	// You'll want to grab this in a SharedPtr
	JMutexAutoLock * getLock()
	{
		return new JMutexAutoLock(m_mutex);
	}
	
	// You pretty surely want to grab the lock when accessing this
	T m_value;

private:
	JMutex m_mutex;
};

/*
	A single worker thread - multiple client threads queue framework.
*/
template<typename Key, typename T, typename Caller, typename CallerData>
class GetResult
{
public:
	Key key;
	T item;
	std::pair<Caller, CallerData> caller;
};

template<typename Key, typename T, typename Caller, typename CallerData>
class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
{
};

template<typename Caller, typename Data, typename Key, typename T>
class CallerInfo
{
public:
	Caller caller;
	Data data;
	ResultQueue< Key, T, Caller, Data>* dest;
};

template<typename Key, typename T, typename Caller, typename CallerData>
class GetRequest
{
public:
	GetRequest()
	{
	}
	GetRequest(Key a_key)
	{
		key = a_key;
	}
	~GetRequest()
	{
	}
	
	Key key;
	std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
};

/**
 * Notes for RequestQueue usage
 * @param Key unique key to identify a request for a specific resource
 * @param T ?
 * @param Caller unique id of calling thread
 * @param CallerData data passed back to caller
 */
template<typename Key, typename T, typename Caller, typename CallerData>
class RequestQueue
{
public:
	bool empty()
	{
		return m_queue.empty();
	}

	void add(Key key, Caller caller, CallerData callerdata,
			ResultQueue<Key, T, Caller, CallerData> *dest)
	{
		{
			JMutexAutoLock lock(m_queue.getMutex());

			/*
				If the caller is already on the list, only update CallerData
			*/
			for(typename std::deque< GetRequest<Key, T, Caller, CallerData> >::iterator
					i = m_queue.getQueue().begin();
					i != m_queue.getQueue().end(); ++i)
			{
				GetRequest<Key, T, Caller, CallerData> &request = *i;

				if(request.key == key)
				{
					for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
							i = request.callers.begin();
							i != request.callers.end(); ++i)
					{
						CallerInfo<Caller, CallerData, Key, T> &ca = *i;
						if(ca.caller == caller)
						{
							ca.data = callerdata;
							return;
						}
					}
					CallerInfo<Caller, CallerData, Key, T> ca;
					ca.caller = caller;
					ca.data = callerdata;
					ca.dest = dest;
					request.callers.push_back(ca);
					return;
				}
			}
		}

		/*
			Else add a new request to the queue
		*/

		GetRequest<Key, T, Caller, CallerData> request;
		request.key = key;
		CallerInfo<Caller, CallerData, Key, T> ca;
		ca.caller = caller;
		ca.data = callerdata;
		ca.dest = dest;
		request.callers.push_back(ca);
		
		m_queue.push_back(request);
	}

	GetRequest<Key, T, Caller, CallerData> pop(unsigned int timeout_ms)
	{
		return m_queue.pop_front(timeout_ms);
	}

	GetRequest<Key, T, Caller, CallerData> pop()
	{
		return m_queue.pop_frontNoEx();
	}

	void pushResult(GetRequest<Key, T, Caller, CallerData> req,
					T res) {

		for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
				i = req.callers.begin();
				i != req.callers.end(); ++i)
		{
			CallerInfo<Caller, CallerData, Key, T> &ca = *i;

			GetResult<Key,T,Caller,CallerData> result;

			result.key = req.key;
			result.item = res;
			result.caller.first = ca.caller;
			result.caller.second = ca.data;

			ca.dest->push_back(result);
		}
	}

private:
	MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
};

class UpdateThread : public JThread
{
private:
	JSemaphore m_update_sem;

protected:
	virtual void doUpdate() = 0;
	virtual const char *getName() = 0;

public:
	UpdateThread()
	{
	}
	~UpdateThread()
	{}

	void deferUpdate()
	{
		m_update_sem.Post();
	}

	void Stop()
	{
		JThread::Stop();

		// give us a nudge
		m_update_sem.Post();
	}

	void *Thread()
	{
		ThreadStarted();

		const char *thread_name = getName();

		log_register_thread(thread_name);

		DSTACK(__FUNCTION_NAME);

		BEGIN_DEBUG_EXCEPTION_HANDLER

		porting::setThreadName(thread_name);

		while (!StopRequested()) {

			m_update_sem.Wait();

			// Empty the queue, just in case doUpdate() is expensive
			while (m_update_sem.GetValue()) m_update_sem.Wait();

			if (StopRequested()) break;

			doUpdate();
		}
		END_DEBUG_EXCEPTION_HANDLER(errorstream)

		return NULL;
	}
};

#endif