2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
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"
|
2015-04-07 12:13:12 +02:00
|
|
|
#include "../threading/thread.h"
|
|
|
|
#include "../threading/mutex.h"
|
|
|
|
#include "../threading/mutex_auto_lock.h"
|
2014-01-06 12:45:42 +01:00
|
|
|
#include "porting.h"
|
2015-06-29 11:06:03 +02:00
|
|
|
#include "log.h"
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2015-07-08 17:53:02 +02:00
|
|
|
class MutexedVariable {
|
2012-06-17 00:29:13 +02:00
|
|
|
public:
|
|
|
|
MutexedVariable(T value):
|
|
|
|
m_value(value)
|
2015-07-08 17:53:02 +02:00
|
|
|
{}
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
T get()
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2012-06-17 00:29:13 +02:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(T value)
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2012-06-17 00:29:13 +02:00
|
|
|
m_value = value;
|
|
|
|
}
|
2015-07-08 17:53:02 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
// You'll want to grab this in a SharedPtr
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock *getLock()
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
return new MutexAutoLock(m_mutex);
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2015-07-08 17:53:02 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
// You pretty surely want to grab the lock when accessing this
|
|
|
|
T m_value;
|
|
|
|
|
|
|
|
private:
|
2015-04-07 12:13:12 +02:00
|
|
|
Mutex m_mutex;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
A single worker thread - multiple client threads queue framework.
|
|
|
|
*/
|
|
|
|
template<typename Key, typename T, typename Caller, typename CallerData>
|
2015-07-08 17:53:02 +02:00
|
|
|
class GetResult {
|
2012-06-17 00:29:13 +02:00
|
|
|
public:
|
|
|
|
Key key;
|
|
|
|
T item;
|
2013-11-13 21:46:14 +01:00
|
|
|
std::pair<Caller, CallerData> caller;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Key, typename T, typename Caller, typename CallerData>
|
2015-07-08 17:53:02 +02:00
|
|
|
class ResultQueue : public MutexedQueue<GetResult<Key, T, Caller, CallerData> > {
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
2013-11-13 21:46:14 +01:00
|
|
|
template<typename Caller, typename Data, typename Key, typename T>
|
2015-07-08 17:53:02 +02:00
|
|
|
class CallerInfo {
|
2013-11-13 21:46:14 +01:00
|
|
|
public:
|
|
|
|
Caller caller;
|
|
|
|
Data data;
|
2015-07-08 17:53:02 +02:00
|
|
|
ResultQueue<Key, T, Caller, Data> *dest;
|
2013-11-13 21:46:14 +01:00
|
|
|
};
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
template<typename Key, typename T, typename Caller, typename CallerData>
|
2015-07-08 17:53:02 +02:00
|
|
|
class GetRequest {
|
2012-06-17 00:29:13 +02:00
|
|
|
public:
|
2015-07-08 17:53:02 +02:00
|
|
|
GetRequest() {}
|
|
|
|
~GetRequest() {}
|
|
|
|
|
|
|
|
GetRequest(Key a_key) {
|
2012-06-17 00:29:13 +02:00
|
|
|
key = a_key;
|
|
|
|
}
|
2015-07-08 17:53:02 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
Key key;
|
2013-11-13 21:46:14 +01:00
|
|
|
std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
2013-11-13 21:49:50 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-06-17 00:29:13 +02:00
|
|
|
template<typename Key, typename T, typename Caller, typename CallerData>
|
2015-07-08 17:53:02 +02:00
|
|
|
class RequestQueue {
|
2012-06-17 00:29:13 +02:00
|
|
|
public:
|
2012-12-20 18:19:49 +01:00
|
|
|
bool empty()
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
return m_queue.empty();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void add(Key key, Caller caller, CallerData callerdata,
|
2015-07-08 17:53:02 +02:00
|
|
|
ResultQueue<Key, T, Caller, CallerData> *dest)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-07-08 17:53:02 +02:00
|
|
|
typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
|
|
|
|
typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator j;
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_queue.getMutex());
|
2014-01-06 12:45:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
If the caller is already on the list, only update CallerData
|
|
|
|
*/
|
2015-07-08 17:53:02 +02:00
|
|
|
for (i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) {
|
2014-01-06 12:45:42 +01:00
|
|
|
GetRequest<Key, T, Caller, CallerData> &request = *i;
|
2015-07-08 17:53:02 +02:00
|
|
|
if (request.key != key)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = request.callers.begin(); j != request.callers.end(); ++j) {
|
|
|
|
CallerInfo<Caller, CallerData, Key, T> &ca = *j;
|
|
|
|
if (ca.caller == caller) {
|
|
|
|
ca.data = callerdata;
|
|
|
|
return;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 17:53:02 +02:00
|
|
|
|
|
|
|
CallerInfo<Caller, CallerData, Key, T> ca;
|
|
|
|
ca.caller = caller;
|
|
|
|
ca.data = callerdata;
|
|
|
|
ca.dest = dest;
|
|
|
|
request.callers.push_back(ca);
|
|
|
|
return;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Else add a new request to the queue
|
|
|
|
*/
|
|
|
|
|
|
|
|
GetRequest<Key, T, Caller, CallerData> request;
|
|
|
|
request.key = key;
|
2013-11-13 21:46:14 +01:00
|
|
|
CallerInfo<Caller, CallerData, Key, T> ca;
|
2012-06-17 00:29:13 +02:00
|
|
|
ca.caller = caller;
|
|
|
|
ca.data = callerdata;
|
2013-11-13 21:46:14 +01:00
|
|
|
ca.dest = dest;
|
2012-06-17 00:29:13 +02:00
|
|
|
request.callers.push_back(ca);
|
2015-07-08 17:53:02 +02:00
|
|
|
|
2014-01-06 12:45:42 +01:00
|
|
|
m_queue.push_back(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
GetRequest<Key, T, Caller, CallerData> pop(unsigned int timeout_ms)
|
|
|
|
{
|
|
|
|
return m_queue.pop_front(timeout_ms);
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-01-06 12:45:42 +01:00
|
|
|
GetRequest<Key, T, Caller, CallerData> pop()
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-01-06 12:45:42 +01:00
|
|
|
return m_queue.pop_frontNoEx();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-08 17:53:02 +02:00
|
|
|
void pushResult(GetRequest<Key, T, Caller, CallerData> req, T res)
|
|
|
|
{
|
|
|
|
for (typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator
|
2013-11-13 21:46:14 +01:00
|
|
|
i = req.callers.begin();
|
2015-07-08 17:53:02 +02:00
|
|
|
i != req.callers.end(); ++i) {
|
2013-11-13 21:46:14 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
private:
|
2015-07-08 17:53:02 +02:00
|
|
|
MutexedQueue<GetRequest<Key, T, Caller, CallerData> > m_queue;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
class UpdateThread : public Thread
|
|
|
|
{
|
2015-06-29 11:06:03 +02:00
|
|
|
public:
|
2015-04-07 12:13:12 +02:00
|
|
|
UpdateThread(const std::string &name) : Thread(name + "Update") {}
|
|
|
|
~UpdateThread() {}
|
2015-06-29 11:06:03 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void deferUpdate() { m_update_sem.post(); }
|
2015-06-29 11:06:03 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void stop()
|
2015-06-29 11:06:03 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
Thread::stop();
|
2015-06-29 11:06:03 +02:00
|
|
|
|
|
|
|
// give us a nudge
|
2015-04-07 12:13:12 +02:00
|
|
|
m_update_sem.post();
|
2015-06-29 11:06:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void *run()
|
2015-06-29 11:06:03 +02:00
|
|
|
{
|
2015-10-14 08:39:30 +02:00
|
|
|
DSTACK(FUNCTION_NAME);
|
2015-06-29 11:06:03 +02:00
|
|
|
BEGIN_DEBUG_EXCEPTION_HANDLER
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
while (!stopRequested()) {
|
|
|
|
m_update_sem.wait();
|
|
|
|
// Set semaphore to 0
|
|
|
|
while (m_update_sem.wait(0));
|
2015-06-29 11:06:03 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
if (stopRequested()) break;
|
2015-06-29 11:06:03 +02:00
|
|
|
|
|
|
|
doUpdate();
|
|
|
|
}
|
2015-07-08 17:53:02 +02:00
|
|
|
|
2015-10-14 08:33:04 +02:00
|
|
|
END_DEBUG_EXCEPTION_HANDLER
|
2015-06-29 11:06:03 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-07-08 17:53:02 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void doUpdate() = 0;
|
|
|
|
|
|
|
|
private:
|
2015-04-07 12:13:12 +02:00
|
|
|
Semaphore m_update_sem;
|
2015-06-29 11:06:03 +02:00
|
|
|
};
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
#endif
|
|
|
|
|