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 "../threading/thread.h"
25 #include "../threading/mutex.h"
26 #include "../threading/mutex_auto_lock.h"
29 #include "container.h"
35 MutexedVariable(const T &value):
41 MutexAutoLock lock(m_mutex);
45 void set(const T &value)
47 MutexAutoLock lock(m_mutex);
51 // You pretty surely want to grab the lock when accessing this
58 A single worker thread - multiple client threads queue framework.
60 template<typename Key, typename T, typename Caller, typename CallerData>
65 std::pair<Caller, CallerData> caller;
68 template<typename Key, typename T, typename Caller, typename CallerData>
69 class ResultQueue : public MutexedQueue<GetResult<Key, T, Caller, CallerData> > {
72 template<typename Caller, typename Data, typename Key, typename T>
77 ResultQueue<Key, T, Caller, Data> *dest;
80 template<typename Key, typename T, typename Caller, typename CallerData>
86 GetRequest(const Key &a_key): key(a_key)
91 std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
95 * Notes for RequestQueue usage
96 * @param Key unique key to identify a request for a specific resource
98 * @param Caller unique id of calling thread
99 * @param CallerData data passed back to caller
101 template<typename Key, typename T, typename Caller, typename CallerData>
106 return m_queue.empty();
109 void add(const Key &key, Caller caller, CallerData callerdata,
110 ResultQueue<Key, T, Caller, CallerData> *dest)
112 typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
113 typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator j;
116 MutexAutoLock lock(m_queue.getMutex());
119 If the caller is already on the list, only update CallerData
121 for (i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) {
122 GetRequest<Key, T, Caller, CallerData> &request = *i;
123 if (request.key != key)
126 for (j = request.callers.begin(); j != request.callers.end(); ++j) {
127 CallerInfo<Caller, CallerData, Key, T> &ca = *j;
128 if (ca.caller == caller) {
129 ca.data = callerdata;
134 CallerInfo<Caller, CallerData, Key, T> ca;
136 ca.data = callerdata;
138 request.callers.push_back(ca);
144 Else add a new request to the queue
147 GetRequest<Key, T, Caller, CallerData> request;
149 CallerInfo<Caller, CallerData, Key, T> ca;
151 ca.data = callerdata;
153 request.callers.push_back(ca);
155 m_queue.push_back(request);
158 GetRequest<Key, T, Caller, CallerData> pop(unsigned int timeout_ms)
160 return m_queue.pop_front(timeout_ms);
163 GetRequest<Key, T, Caller, CallerData> pop()
165 return m_queue.pop_frontNoEx();
168 void pushResult(GetRequest<Key, T, Caller, CallerData> req, T res)
170 for (typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator
171 i = req.callers.begin();
172 i != req.callers.end(); ++i) {
173 CallerInfo<Caller, CallerData, Key, T> &ca = *i;
175 GetResult<Key,T,Caller,CallerData> result;
177 result.key = req.key;
179 result.caller.first = ca.caller;
180 result.caller.second = ca.data;
182 ca.dest->push_back(result);
187 MutexedQueue<GetRequest<Key, T, Caller, CallerData> > m_queue;
190 class UpdateThread : public Thread
193 UpdateThread(const std::string &name) : Thread(name + "Update") {}
196 void deferUpdate() { m_update_sem.post(); }
208 DSTACK(FUNCTION_NAME);
209 BEGIN_DEBUG_EXCEPTION_HANDLER
211 while (!stopRequested()) {
213 // Set semaphore to 0
214 while (m_update_sem.wait(0));
216 if (stopRequested()) break;
221 END_DEBUG_EXCEPTION_HANDLER
227 virtual void doUpdate() = 0;
230 Semaphore m_update_sem;