Merged revision(s) 58452-58584 from trunk/blender into soc-2013-dingto.
[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 <pthread.h>
24 #include <queue>
25
26 #include "util_function.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 /* use boost for mutexes */
31
32 typedef boost::mutex thread_mutex;
33 typedef boost::mutex::scoped_lock thread_scoped_lock;
34 typedef boost::condition_variable thread_condition_variable;
35
36 /* own pthread based implementation, to avoid boost version conflicts with
37  * dynamically loaded blender plugins */
38
39 class thread {
40 public:
41         thread(boost::function<void(void)> run_cb_)
42         {
43                 joined = false;
44                 run_cb = run_cb_;
45
46                 pthread_create(&pthread_id, NULL, run, (void*)this);
47         }
48
49         ~thread()
50         {
51                 if(!joined)
52                         join();
53         }
54
55         static void *run(void *arg)
56         {
57                 ((thread*)arg)->run_cb();
58                 return NULL;
59         }
60
61         bool join()
62         {
63                 joined = true;
64                 return pthread_join(pthread_id, NULL) == 0;
65         }
66
67 protected:
68         boost::function<void(void)> run_cb;
69         pthread_t pthread_id;
70         bool joined;
71 };
72
73 CCL_NAMESPACE_END
74
75 #endif /* __UTIL_THREAD_H__ */
76