3 Copyright (C) 2010-2012 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"
26 #include <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.
109 template<typename Caller, typename Data>
117 template<typename Key, typename T, typename Caller, typename CallerData>
123 core::list<CallerInfo<Caller, CallerData> > callers;
126 template<typename Key, typename T, typename Caller, typename CallerData>
127 class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
131 template<typename Key, typename T, typename Caller, typename CallerData>
139 GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest)
143 GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest,
154 ResultQueue<Key, T, Caller, CallerData> *dest;
155 core::list<CallerInfo<Caller, CallerData> > callers;
158 template<typename Key, typename T, typename Caller, typename CallerData>
164 return m_queue.size();
167 void add(Key key, Caller caller, CallerData callerdata,
168 ResultQueue<Key, T, Caller, CallerData> *dest)
170 JMutexAutoLock lock(m_queue.getMutex());
173 If the caller is already on the list, only update CallerData
175 for(typename core::list< GetRequest<Key, T, Caller, CallerData> >::Iterator
176 i = m_queue.getList().begin();
177 i != m_queue.getList().end(); i++)
179 GetRequest<Key, T, Caller, CallerData> &request = *i;
181 if(request.key == key)
183 for(typename core::list< CallerInfo<Caller, CallerData> >::Iterator
184 i = request.callers.begin();
185 i != request.callers.end(); i++)
187 CallerInfo<Caller, CallerData> &ca = *i;
188 if(ca.caller == caller)
190 ca.data = callerdata;
194 CallerInfo<Caller, CallerData> ca;
196 ca.data = callerdata;
197 request.callers.push_back(ca);
203 Else add a new request to the queue
206 GetRequest<Key, T, Caller, CallerData> request;
208 CallerInfo<Caller, CallerData> ca;
210 ca.data = callerdata;
211 request.callers.push_back(ca);
214 m_queue.getList().push_back(request);
217 GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
219 return m_queue.pop_front(wait_if_empty);
223 MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;