]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/thread.h
Fix multicaller support in RequestQueue
[dragonfireclient.git] / src / util / thread.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef UTIL_THREAD_HEADER
21 #define UTIL_THREAD_HEADER
22
23 #include "../irrlichttypes.h"
24 #include "../jthread/jthread.h"
25 #include "../jthread/jmutex.h"
26 #include "../jthread/jmutexautolock.h"
27
28 template<typename T>
29 class MutexedVariable
30 {
31 public:
32         MutexedVariable(T value):
33                 m_value(value)
34         {
35                 m_mutex.Init();
36         }
37
38         T get()
39         {
40                 JMutexAutoLock lock(m_mutex);
41                 return m_value;
42         }
43
44         void set(T value)
45         {
46                 JMutexAutoLock lock(m_mutex);
47                 m_value = value;
48         }
49         
50         // You'll want to grab this in a SharedPtr
51         JMutexAutoLock * getLock()
52         {
53                 return new JMutexAutoLock(m_mutex);
54         }
55         
56         // You pretty surely want to grab the lock when accessing this
57         T m_value;
58
59 private:
60         JMutex m_mutex;
61 };
62
63 /*
64         A base class for simple background thread implementation
65 */
66
67 class SimpleThread : public JThread
68 {
69         bool run;
70         JMutex run_mutex;
71
72 public:
73
74         SimpleThread():
75                 JThread(),
76                 run(true)
77         {
78                 run_mutex.Init();
79         }
80
81         virtual ~SimpleThread()
82         {}
83
84         virtual void * Thread() = 0;
85
86         bool getRun()
87         {
88                 JMutexAutoLock lock(run_mutex);
89                 return run;
90         }
91         void setRun(bool a_run)
92         {
93                 JMutexAutoLock lock(run_mutex);
94                 run = a_run;
95         }
96
97         void stop()
98         {
99                 setRun(false);
100                 while(IsRunning())
101                         sleep_ms(100);
102         }
103 };
104
105 /*
106         A single worker thread - multiple client threads queue framework.
107 */
108
109
110
111 template<typename Key, typename T, typename Caller, typename CallerData>
112 class GetResult
113 {
114 public:
115         Key key;
116         T item;
117         std::pair<Caller, CallerData> caller;
118 };
119
120 template<typename Key, typename T, typename Caller, typename CallerData>
121 class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
122 {
123 };
124
125 template<typename Caller, typename Data, typename Key, typename T>
126 class CallerInfo
127 {
128 public:
129         Caller caller;
130         Data data;
131         ResultQueue< Key, T, Caller, Data>* dest;
132 };
133
134 template<typename Key, typename T, typename Caller, typename CallerData>
135 class GetRequest
136 {
137 public:
138         GetRequest()
139         {
140         }
141         GetRequest(Key a_key)
142         {
143                 key = a_key;
144         }
145         ~GetRequest()
146         {
147         }
148         
149         Key key;
150         std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
151 };
152
153 template<typename Key, typename T, typename Caller, typename CallerData>
154 class RequestQueue
155 {
156 public:
157         bool empty()
158         {
159                 return m_queue.empty();
160         }
161
162         void add(Key key, Caller caller, CallerData callerdata,
163                         ResultQueue<Key, T, Caller, CallerData> *dest)
164         {
165                 JMutexAutoLock lock(m_queue.getMutex());
166                 
167                 /*
168                         If the caller is already on the list, only update CallerData
169                 */
170                 for(typename std::list< GetRequest<Key, T, Caller, CallerData> >::iterator
171                                 i = m_queue.getList().begin();
172                                 i != m_queue.getList().end(); ++i)
173                 {
174                         GetRequest<Key, T, Caller, CallerData> &request = *i;
175
176                         if(request.key == key)
177                         {
178                                 for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
179                                                 i = request.callers.begin();
180                                                 i != request.callers.end(); ++i)
181                                 {
182                                         CallerInfo<Caller, CallerData, Key, T> &ca = *i;
183                                         if(ca.caller == caller)
184                                         {
185                                                 ca.data = callerdata;
186                                                 return;
187                                         }
188                                 }
189                                 CallerInfo<Caller, CallerData, Key, T> ca;
190                                 ca.caller = caller;
191                                 ca.data = callerdata;
192                                 ca.dest = dest;
193                                 request.callers.push_back(ca);
194                                 return;
195                         }
196                 }
197
198                 /*
199                         Else add a new request to the queue
200                 */
201
202                 GetRequest<Key, T, Caller, CallerData> request;
203                 request.key = key;
204                 CallerInfo<Caller, CallerData, Key, T> ca;
205                 ca.caller = caller;
206                 ca.data = callerdata;
207                 ca.dest = dest;
208                 request.callers.push_back(ca);
209                 
210                 m_queue.getList().push_back(request);
211         }
212
213         GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
214         {
215                 return m_queue.pop_front(wait_if_empty);
216         }
217
218         void pushResult(GetRequest<Key, T, Caller, CallerData> req,
219                                         T res) {
220
221                 for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
222                                 i = req.callers.begin();
223                                 i != req.callers.end(); ++i)
224                 {
225                         CallerInfo<Caller, CallerData, Key, T> &ca = *i;
226
227                         GetResult<Key,T,Caller,CallerData> result;
228
229                         result.key = req.key;
230                         result.item = res;
231                         result.caller.first = ca.caller;
232                         result.caller.second = ca.data;
233
234                         ca.dest->push_back(result);
235                 }
236         }
237
238 private:
239         MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
240 };
241
242 #endif
243