3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef UTIL_THREAD_HEADER
21 #define UTIL_THREAD_HEADER
23 #include "../irrlichttypes.h"
24 #include "../jthread/jthread.h"
25 #include "../jthread/jmutex.h"
26 #include "../jthread/jmutexautolock.h"
32 MutexedVariable(T value):
40 JMutexAutoLock lock(m_mutex);
46 JMutexAutoLock lock(m_mutex);
50 // You'll want to grab this in a SharedPtr
51 JMutexAutoLock * getLock()
53 return new JMutexAutoLock(m_mutex);
56 // You pretty surely want to grab the lock when accessing this
64 A base class for simple background thread implementation
67 class SimpleThread : public JThread
81 virtual ~SimpleThread()
84 virtual void * Thread() = 0;
88 JMutexAutoLock lock(run_mutex);
91 void setRun(bool a_run)
93 JMutexAutoLock lock(run_mutex);
106 A single worker thread - multiple client threads queue framework.
111 template<typename Key, typename T, typename Caller, typename CallerData>
117 std::pair<Caller, CallerData> caller;
120 template<typename Key, typename T, typename Caller, typename CallerData>
121 class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
125 template<typename Caller, typename Data, typename Key, typename T>
131 ResultQueue< Key, T, Caller, Data>* dest;
134 template<typename Key, typename T, typename Caller, typename CallerData>
141 GetRequest(Key a_key)
150 std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
154 * Notes for RequestQueue usage
155 * @param Key unique key to identify a request for a specific resource
157 * @param Caller unique id of calling thread
158 * @param CallerData data passed back to caller
160 template<typename Key, typename T, typename Caller, typename CallerData>
166 return m_queue.empty();
169 void add(Key key, Caller caller, CallerData callerdata,
170 ResultQueue<Key, T, Caller, CallerData> *dest)
172 JMutexAutoLock lock(m_queue.getMutex());
175 If the caller is already on the list, only update CallerData
177 for(typename std::list< GetRequest<Key, T, Caller, CallerData> >::iterator
178 i = m_queue.getList().begin();
179 i != m_queue.getList().end(); ++i)
181 GetRequest<Key, T, Caller, CallerData> &request = *i;
183 if(request.key == key)
185 for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
186 i = request.callers.begin();
187 i != request.callers.end(); ++i)
189 CallerInfo<Caller, CallerData, Key, T> &ca = *i;
190 if(ca.caller == caller)
192 ca.data = callerdata;
196 CallerInfo<Caller, CallerData, Key, T> ca;
198 ca.data = callerdata;
200 request.callers.push_back(ca);
206 Else add a new request to the queue
209 GetRequest<Key, T, Caller, CallerData> request;
211 CallerInfo<Caller, CallerData, Key, T> ca;
213 ca.data = callerdata;
215 request.callers.push_back(ca);
217 m_queue.getList().push_back(request);
220 GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
222 return m_queue.pop_front(wait_if_empty);
225 void pushResult(GetRequest<Key, T, Caller, CallerData> req,
228 for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
229 i = req.callers.begin();
230 i != req.callers.end(); ++i)
232 CallerInfo<Caller, CallerData, Key, T> &ca = *i;
234 GetResult<Key,T,Caller,CallerData> result;
236 result.key = req.key;
238 result.caller.first = ca.caller;
239 result.caller.second = ca.data;
241 ca.dest->push_back(result);
246 MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;