minetest/src/util/thread.h

240 lines
5.2 KiB
C
Raw Normal View History

/*
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>
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 "../threading/thread.h"
#include "../threading/mutex.h"
#include "../threading/mutex_auto_lock.h"
#include "porting.h"
#include "log.h"
template<typename T>
2015-07-08 17:53:02 +02:00
class MutexedVariable {
public:
MutexedVariable(T value):
m_value(value)
2015-07-08 17:53:02 +02:00
{}
T get()
{
MutexAutoLock lock(m_mutex);
return m_value;
}
void set(T value)
{
MutexAutoLock lock(m_mutex);
m_value = value;
}
2015-07-08 17:53:02 +02:00
// You'll want to grab this in a SharedPtr
MutexAutoLock *getLock()
{
return new MutexAutoLock(m_mutex);
}
2015-07-08 17:53:02 +02:00
// You pretty surely want to grab the lock when accessing this
T m_value;
private:
Mutex m_mutex;
};
/*
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 {
public:
Key key;
T item;
std::pair<Caller, CallerData> caller;
};
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> > {
};
template<typename Caller, typename Data, typename Key, typename T>
2015-07-08 17:53:02 +02:00
class CallerInfo {
public:
Caller caller;
Data data;
2015-07-08 17:53:02 +02:00
ResultQueue<Key, T, Caller, Data> *dest;
};
template<typename Key, typename T, typename Caller, typename CallerData>
2015-07-08 17:53:02 +02:00
class GetRequest {
public:
2015-07-08 17:53:02 +02:00
GetRequest() {}
~GetRequest() {}
GetRequest(Key a_key) {
key = a_key;
}
2015-07-08 17:53:02 +02:00
Key key;
std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
};
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
*/
template<typename Key, typename T, typename Caller, typename CallerData>
2015-07-08 17:53:02 +02:00
class RequestQueue {
public:
2012-12-20 18:19:49 +01:00
bool empty()
{
2012-12-20 18:19:49 +01:00
return m_queue.empty();
}
void add(Key key, Caller caller, CallerData callerdata,
2015-07-08 17:53:02 +02:00
ResultQueue<Key, T, Caller, CallerData> *dest)
{
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;
{
MutexAutoLock lock(m_queue.getMutex());
/*
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) {
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;
}
}
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;
}
}
/*
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);
2015-07-08 17:53:02 +02:00
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();
}
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
i = req.callers.begin();
2015-07-08 17:53:02 +02:00
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:
2015-07-08 17:53:02 +02:00
MutexedQueue<GetRequest<Key, T, Caller, CallerData> > m_queue;
};
class UpdateThread : public Thread
{
public:
UpdateThread(const std::string &name) : Thread(name + "Update") {}
~UpdateThread() {}
void deferUpdate() { m_update_sem.post(); }
void stop()
{
Thread::stop();
// give us a nudge
m_update_sem.post();
}
void *run()
{
DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER
while (!stopRequested()) {
m_update_sem.wait();
// Set semaphore to 0
while (m_update_sem.wait(0));
if (stopRequested()) break;
doUpdate();
}
2015-07-08 17:53:02 +02:00
END_DEBUG_EXCEPTION_HANDLER(errorstream)
return NULL;
}
2015-07-08 17:53:02 +02:00
protected:
virtual void doUpdate() = 0;
private:
Semaphore m_update_sem;
};
#endif