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"
|
2013-09-16 05:00:01 +02:00
|
|
|
#include "../jthread/jthread.h"
|
|
|
|
#include "../jthread/jmutex.h"
|
|
|
|
#include "../jthread/jmutexautolock.h"
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class MutexedVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MutexedVariable(T value):
|
|
|
|
m_value(value)
|
|
|
|
{
|
|
|
|
m_mutex.Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 base class for simple background thread implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SimpleThread : public JThread
|
|
|
|
{
|
|
|
|
bool run;
|
|
|
|
JMutex run_mutex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
SimpleThread():
|
|
|
|
JThread(),
|
|
|
|
run(true)
|
|
|
|
{
|
|
|
|
run_mutex.Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SimpleThread()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual void * Thread() = 0;
|
|
|
|
|
|
|
|
bool getRun()
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(run_mutex);
|
|
|
|
return run;
|
|
|
|
}
|
|
|
|
void setRun(bool a_run)
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(run_mutex);
|
|
|
|
run = a_run;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
setRun(false);
|
|
|
|
while(IsRunning())
|
|
|
|
sleep_ms(100);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
A single worker thread - multiple client threads queue framework.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
class GetResult
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2013-11-13 21:46:14 +01:00
|
|
|
template<typename Caller, typename Data, typename Key, typename T>
|
|
|
|
class CallerInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Caller caller;
|
|
|
|
Data data;
|
|
|
|
ResultQueue< Key, T, Caller, Data>* dest;
|
|
|
|
};
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
template<typename Key, typename T, typename Caller, typename CallerData>
|
|
|
|
class GetRequest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetRequest()
|
|
|
|
{
|
|
|
|
}
|
2013-11-13 21:46:14 +01:00
|
|
|
GetRequest(Key a_key)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
key = a_key;
|
|
|
|
}
|
|
|
|
~GetRequest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Key, typename T, typename Caller, typename CallerData>
|
|
|
|
class RequestQueue
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
ResultQueue<Key, T, Caller, CallerData> *dest)
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_queue.getMutex());
|
|
|
|
|
|
|
|
/*
|
|
|
|
If the caller is already on the list, only update CallerData
|
|
|
|
*/
|
2012-12-20 18:19:49 +01:00
|
|
|
for(typename std::list< GetRequest<Key, T, Caller, CallerData> >::iterator
|
2012-06-17 00:29:13 +02:00
|
|
|
i = m_queue.getList().begin();
|
2012-12-20 18:19:49 +01:00
|
|
|
i != m_queue.getList().end(); ++i)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
GetRequest<Key, T, Caller, CallerData> &request = *i;
|
|
|
|
|
|
|
|
if(request.key == key)
|
|
|
|
{
|
2013-11-13 21:46:14 +01:00
|
|
|
for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
|
2012-06-17 00:29:13 +02:00
|
|
|
i = request.callers.begin();
|
2012-12-20 18:19:49 +01:00
|
|
|
i != request.callers.end(); ++i)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2013-11-13 21:46:14 +01:00
|
|
|
CallerInfo<Caller, CallerData, Key, T> &ca = *i;
|
2012-06-17 00:29:13 +02:00
|
|
|
if(ca.caller == caller)
|
|
|
|
{
|
|
|
|
ca.data = callerdata;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
|
|
|
|
m_queue.getList().push_back(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
|
|
|
|
{
|
|
|
|
return m_queue.pop_front(wait_if_empty);
|
|
|
|
}
|
|
|
|
|
2013-11-13 21:46:14 +01: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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
private:
|
|
|
|
MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|