Cycles: Make all #include statements relative to cycles source directory
[blender.git] / intern / cycles / util / util_thread.cpp
1 /*
2  * Copyright 2011-2016 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "util/util_thread.h"
18
19 #include "util/util_system.h"
20 #include "util/util_windows.h"
21
22 CCL_NAMESPACE_BEGIN
23
24 thread::thread(function<void(void)> run_cb, int group)
25   : run_cb_(run_cb),
26     joined_(false),
27         group_(group)
28 {
29         pthread_create(&pthread_id_, NULL, run, (void*)this);
30 }
31
32 thread::~thread()
33 {
34         if(!joined_) {
35                 join();
36         }
37 }
38
39 void *thread::run(void *arg)
40 {
41         thread *self = (thread*)(arg);
42         if(self->group_ != -1) {
43 #ifdef _WIN32
44                 HANDLE thread_handle = GetCurrentThread();
45                 GROUP_AFFINITY group_affinity = { 0 };
46                 int num_threads = system_cpu_group_thread_count(self->group_);
47                 group_affinity.Group = self->group_;
48                 group_affinity.Mask = (num_threads == 64)
49                                               ? -1
50                                               :  (1ull << num_threads) - 1;
51                 if(SetThreadGroupAffinity(thread_handle, &group_affinity, NULL) == 0) {
52                         fprintf(stderr, "Error setting thread affinity.\n");
53                 }
54 #endif
55         }
56         self->run_cb_();
57         return NULL;
58 }
59
60 bool thread::join()
61 {
62         joined_ = true;
63         return pthread_join(pthread_id_, NULL) == 0;
64 }
65
66 CCL_NAMESPACE_END