Update Copyright Years
[oweals/minetest.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.h>
25 #include <jmutex.h>
26 #include <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 template<typename Caller, typename Data>
110 class CallerInfo
111 {
112 public:
113         Caller caller;
114         Data data;
115 };
116
117 template<typename Key, typename T, typename Caller, typename CallerData>
118 class GetResult
119 {
120 public:
121         Key key;
122         T item;
123         core::list<CallerInfo<Caller, CallerData> > callers;
124 };
125
126 template<typename Key, typename T, typename Caller, typename CallerData>
127 class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
128 {
129 };
130
131 template<typename Key, typename T, typename Caller, typename CallerData>
132 class GetRequest
133 {
134 public:
135         GetRequest()
136         {
137                 dest = NULL;
138         }
139         GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest)
140         {
141                 dest = a_dest;
142         }
143         GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest,
144                         Key a_key)
145         {
146                 dest = a_dest;
147                 key = a_key;
148         }
149         ~GetRequest()
150         {
151         }
152         
153         Key key;
154         ResultQueue<Key, T, Caller, CallerData> *dest;
155         core::list<CallerInfo<Caller, CallerData> > callers;
156 };
157
158 template<typename Key, typename T, typename Caller, typename CallerData>
159 class RequestQueue
160 {
161 public:
162         u32 size()
163         {
164                 return m_queue.size();
165         }
166
167         void add(Key key, Caller caller, CallerData callerdata,
168                         ResultQueue<Key, T, Caller, CallerData> *dest)
169         {
170                 JMutexAutoLock lock(m_queue.getMutex());
171                 
172                 /*
173                         If the caller is already on the list, only update CallerData
174                 */
175                 for(typename core::list< GetRequest<Key, T, Caller, CallerData> >::Iterator
176                                 i = m_queue.getList().begin();
177                                 i != m_queue.getList().end(); i++)
178                 {
179                         GetRequest<Key, T, Caller, CallerData> &request = *i;
180
181                         if(request.key == key)
182                         {
183                                 for(typename core::list< CallerInfo<Caller, CallerData> >::Iterator
184                                                 i = request.callers.begin();
185                                                 i != request.callers.end(); i++)
186                                 {
187                                         CallerInfo<Caller, CallerData> &ca = *i;
188                                         if(ca.caller == caller)
189                                         {
190                                                 ca.data = callerdata;
191                                                 return;
192                                         }
193                                 }
194                                 CallerInfo<Caller, CallerData> ca;
195                                 ca.caller = caller;
196                                 ca.data = callerdata;
197                                 request.callers.push_back(ca);
198                                 return;
199                         }
200                 }
201
202                 /*
203                         Else add a new request to the queue
204                 */
205
206                 GetRequest<Key, T, Caller, CallerData> request;
207                 request.key = key;
208                 CallerInfo<Caller, CallerData> ca;
209                 ca.caller = caller;
210                 ca.data = callerdata;
211                 request.callers.push_back(ca);
212                 request.dest = dest;
213                 
214                 m_queue.getList().push_back(request);
215         }
216
217         GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
218         {
219                 return m_queue.pop_front(wait_if_empty);
220         }
221
222 private:
223         MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
224 };
225
226 #endif
227