Cycles: svn merge -r39870:r40266 https://svn.blender.org/svnroot/bf-blender/trunk...
[blender-staging.git] / intern / cycles / util / util_thread.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifndef __UTIL_THREAD_H__
20 #define __UTIL_THREAD_H__
21
22 #include <boost/thread.hpp>
23 #include <queue>
24
25 CCL_NAMESPACE_BEGIN
26
27 #if 0
28
29 /* Use STL for threading */
30
31 using std::thread;
32 using std::thread_mutex;
33 typedef std::lock_guard thread_scoped_lock;
34 using std::condition_variable;
35
36 #else
37
38 /* Use boost for threading */
39
40 using boost::thread;
41 typedef boost::mutex thread_mutex;
42 typedef boost::mutex::scoped_lock thread_scoped_lock;
43 typedef boost::condition_variable thread_condition_variable;
44
45 #endif
46
47 /* Thread Safe Queue to pass tasks from one thread to another. Tasks should be
48  * pushed into the queue, while the worker thread waits to pop the next task
49  * off the queue. Once all tasks are into the queue, calling stop() will stop
50  * the worker threads from waiting for more tasks once all tasks are done. */
51
52 template<typename T> class ThreadQueue
53 {
54 public:
55         ThreadQueue()
56         {
57                 tot = 0;
58                 tot_done = 0;
59                 do_stop = false;
60                 do_cancel = false;
61         }
62
63         /* Main thread functions */
64
65         /* push a task to be executed */
66         void push(const T& value)
67         {
68                 thread_scoped_lock lock(queue_mutex);
69                 queue.push(value);
70                 tot++;
71                 lock.unlock();
72
73                 queue_cond.notify_one();
74         }
75
76         /* wait until all tasks are done */
77         void wait_done()
78         {
79                 thread_scoped_lock lock(done_mutex);
80
81                 while(tot_done != tot)
82                         done_cond.wait(lock);
83         }
84
85         /* stop all worker threads */
86         void stop()
87         {
88                 clear();
89                 do_stop = true;
90                 queue_cond.notify_all();
91         }
92
93         /* cancel all tasks, but keep worker threads running */
94         void cancel()
95         {
96                 clear();
97                 do_cancel = true;
98                 wait_done();
99                 do_cancel = false;
100         }
101
102         /* Worker thread functions
103      *
104          * while(queue.worker_wait_pop(task)) {
105          *              for(..) {
106          *                      ... do work ...
107          *
108          *                      if(queue.worker_cancel())
109          *                              break;
110          *      }
111          *              
112          *              queue.worker_done();
113          * }
114          */
115
116         bool worker_wait_pop(T& value)
117         {
118                 thread_scoped_lock lock(queue_mutex);
119
120                 while(queue.empty() && !do_stop)
121                         queue_cond.wait(lock);
122
123                 if(queue.empty())
124                         return false;
125                 
126                 value = queue.front();
127                 queue.pop();
128
129                 return true;
130         }
131
132         void worker_done()
133         {
134                 thread_scoped_lock lock(done_mutex);
135                 tot_done++;
136                 lock.unlock();
137
138                 assert(tot_done <= tot);
139
140                 done_cond.notify_all();
141         }
142
143         bool worker_cancel()
144         {
145                 return do_cancel;
146         }
147
148 protected:
149         void clear()
150         {
151                 thread_scoped_lock lock(queue_mutex);
152
153                 while(!queue.empty()) {
154                         thread_scoped_lock done_lock(done_mutex);
155                         tot_done++;
156                         done_lock.unlock();
157
158                         queue.pop();
159                 }
160
161                 done_cond.notify_all();
162         }
163
164         std::queue<T> queue;
165         thread_mutex queue_mutex;
166         thread_mutex done_mutex;
167         thread_condition_variable queue_cond;
168         thread_condition_variable done_cond;
169         volatile bool do_stop;
170         volatile bool do_cancel;
171         volatile int tot, tot_done;
172 };
173
174 /* Thread Local Storage
175  *
176  * Boost implementation is a bit slow, and Mac OS X __thread is not supported
177  * but the pthreads implementation is optimized, so we use these macros. */
178
179 #ifdef __APPLE__
180
181 #define tls_ptr(type, name) \
182         pthread_key_t name
183 #define tls_set(name, value) \
184         pthread_setspecific(name, value)
185 #define tls_get(type, name) \
186         ((type*)pthread_getspecific(name))
187 #define tls_create(type, name) \
188         pthread_key_create(&name, NULL)
189 #define tls_delete(type, name) \
190         pthread_key_delete(name);
191
192 #else
193
194 #ifdef __WIN32
195 #define __thread __declspec(thread)
196 #endif
197
198 #define tls_ptr(type, name) \
199         __thread type *name
200 #define tls_set(name, value) \
201         name = value
202 #define tls_get(type, name) \
203         name
204 #define tls_create(type, name)
205 #define tls_delete(type, name)
206
207 #endif
208
209 CCL_NAMESPACE_END
210
211 #endif /* __UTIL_THREAD_H__ */
212