forked from UWQuickstep/quickstep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
410 lines (362 loc) · 12.1 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
include(CheckIncludeFileCXX)
include(CheckCXXSourceCompiles)
# Find a threading library we can use.
set(FOUND_THREADS FALSE)
if(NOT FOUND_THREADS)
CHECK_CXX_SOURCE_COMPILES("
#include <thread>
#include <mutex>
#include <condition_variable>
int main() {
std::thread t;
std::mutex m;
std::recursive_mutex rm;
std::condition_variable_any cv;
std::lock_guard<std::mutex> lock_m(m);
std::lock_guard<std::recursive_mutex> lock_rm(rm);
return 0;
}
" QUICKSTEP_HAVE_CPP11_THREADS)
if (QUICKSTEP_HAVE_CPP11_THREADS)
CHECK_CXX_SOURCE_COMPILES("
#include <shared_mutex>
int main() {
std::shared_mutex m;
m.lock();
m.unlock();
m.lock_shared();
m.unlock_shared();
return 0;
}
" QUICKSTEP_HAVE_CPP17_SHARED_MUTEX)
if (NOT QUICKSTEP_HAVE_CPP17_SHARED_MUTEX)
CHECK_CXX_SOURCE_COMPILES("
#include <shared_mutex>
int main() {
std::shared_timed_mutex m;
m.lock();
m.unlock();
m.lock_shared();
m.unlock_shared();
return 0;
}
" QUICKSTEP_HAVE_CPP14_SHARED_TIMED_MUTEX)
endif()
message("Using threading implementation: cpp11")
set(FOUND_THREADS TRUE)
endif()
endif()
if(NOT FOUND_THREADS)
if(CMAKE_HAVE_PTHREAD_H)
message("Using threading implementation: posix")
set(QUICKSTEP_HAVE_POSIX_THREADS TRUE)
set(FOUND_THREADS TRUE)
endif()
endif()
if(NOT FOUND_THREADS)
CHECK_CXX_SOURCE_COMPILES("
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int main() {
CRITICAL_SECTION cs;
InitializeCriticalSection(&cs);
EnterCriticalSection(&cs);
LeaveCriticalSection(&cs);
DeleteCriticalSection(&cs);
return 0;
}
" QUICKSTEP_HAVE_WINDOWS_THREADS)
if (QUICKSTEP_HAVE_WINDOWS_THREADS)
message("Using threading implementation: windows")
set(FOUND_THREADS TRUE)
endif()
endif()
if(NOT FOUND_THREADS)
message(FATAL_ERROR "No viable threading library found.")
endif()
# Try to find some yield function we can use.
set(FOUND_YIELD FALSE)
if (NOT FOUND_YIELD)
CHECK_CXX_SOURCE_COMPILES("
#include <thread>
int main() {
std::this_thread::yield();
return 0;
}
" QUICKSTEP_HAVE_CPP11_YIELD)
if (QUICKSTEP_HAVE_CPP11_YIELD)
set(FOUND_YIELD TRUE)
endif()
endif()
if (NOT FOUND_YIELD)
CHECK_CXX_SOURCE_COMPILES("
#include <sched.h>
int main() {
sched_yield();
return 0;
}
" QUICKSTEP_HAVE_SCHED_YIELD)
if (QUICKSTEP_HAVE_SCHED_YIELD)
set(FOUND_YIELD TRUE)
endif()
endif()
if (NOT FOUND_YIELD)
CHECK_CXX_SOURCE_COMPILES("
#include <pthread.h>
int main() {
pthread_yield();
return 0;
}
" QUICKSTEP_HAVE_PTHREAD_YIELD)
if (QUICKSTEP_HAVE_PTHREAD_YIELD)
set(FOUND_YIELD TRUE)
endif()
endif()
if (NOT FOUND_YIELD)
CHECK_CXX_SOURCE_COMPILES("
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int main() {
SwitchToThread();
return 0;
}
" QUICKSTEP_HAVE_WIN_SWITCHTOTHREAD)
if (QUICKSTEP_HAVE_WIN_SWITCHTOTHREAD)
set(FOUND_YIELD TRUE)
endif()
endif()
if(NOT FOUND_YIELD)
message(WARNING "No yield function found. ThreadUtil::Yield() will be a no-op.")
endif()
# Try to find a thread-affinitization function that we can use. First, try the
# Linux version of pthread_setaffinity_np().
CHECK_CXX_SOURCE_COMPILES("
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <pthread.h>
#include <sched.h>
int main() {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
if (pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) != 0) {
return 1;
}
return 0;
}
" QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_LINUX)
# Next, try the FreeBSD version of pthread_setaffinity_np(). It is almost
# identical to the Linux version, except that it is in the special header
# pthread_np.h, and the CPU set type is called cpuset_t instead of cpu_set_t.
if (NOT QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_LINUX)
CHECK_CXX_SOURCE_COMPILES("
#ifndef _BSD_SOURCE
#define _BSD_SOURCE 1
#endif
#include <pthread.h>
#include <pthread_np.h>
#include <sched.h>
int main() {
cpuset_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
if (pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) != 0) {
return 1;
}
return 0;
}
" QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_FREEBSD)
endif()
# Finally, try the NetBSD version of pthread_setaffinity_np(). Unlike Linux or
# FreeBSD, NetBSD's version of cpuset_t is dynamically allocated/freed on the
# heap by some special functions in sched.h.
if (NOT (QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_LINUX OR QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_FREEBSD))
CHECK_CXX_SOURCE_COMPILES("
#ifndef _BSD_SOURCE
#define _BSD_SOURCE 1
#endif
#include <pthread.h>
#include <sched.h>
int main() {
cpuset_t *cset = cpuset_create();
if (cpuset_set(0, cset) != 0) {
return 1;
}
if (pthread_setaffinity_np(pthread_self(), cpuset_size(cset), cset) != 0) {
return 1;
}
cpuset_destroy(cset);
return 0;
}
" QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_NETBSD)
endif()
if (NOT (QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_LINUX
OR QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_FREEBSD
OR QUICKSTEP_HAVE_PTHREAD_SETAFFINITY_NP_NETBSD))
message(WARNING "Thread pinning operation will be a no-op.")
endif()
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/ThreadingConfig.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/ThreadingConfig.h"
)
# Declare micro-libs:
if (QUICKSTEP_HAVE_CPP11_THREADS)
add_library(quickstep_threading_ConditionVariable
ConditionVariable.cpp
ConditionVariable.hpp
cpp11/ConditionVariable.hpp)
add_library(quickstep_threading_Mutex
Mutex.cpp
Mutex.hpp
cpp11/Mutex.hpp)
if (QUICKSTEP_HAVE_CPP17_SHARED_MUTEX)
add_library(quickstep_threading_SharedMutex
../empty_src.cpp
SharedMutex.hpp
cpp11/cpp17/SharedMutex.hpp)
elseif (QUICKSTEP_HAVE_CPP14_SHARED_MUTEX)
add_library(quickstep_threading_SharedMutex
../empty_src.cpp
SharedMutex.hpp
cpp11/cpp14/SharedMutex.hpp)
else()
add_library(quickstep_threading_SharedMutex
../empty_src.cpp
SharedMutex.hpp
cpp11/SharedMutex.hpp)
endif()
add_library(quickstep_threading_Thread
Thread.cpp
Thread.hpp
cpp11/Thread.hpp)
elseif (QUICKSTEP_HAVE_POSIX_THREADS)
add_library(quickstep_threading_ConditionVariable
ConditionVariable.cpp
ConditionVariable.hpp
posix/ConditionVariable.hpp)
add_library(quickstep_threading_Mutex
Mutex.cpp
Mutex.hpp
posix/Mutex.hpp)
add_library(quickstep_threading_SharedMutex
../empty_src.cpp
SharedMutex.hpp
posix/SharedMutex.hpp)
add_library(quickstep_threading_Thread
Thread.cpp
Thread.hpp
posix/Thread.hpp)
elseif (QUICKSTEP_HAVE_WINDOWS_THREADS)
add_library(quickstep_threading_ConditionVariable
ConditionVariable.cpp
ConditionVariable.hpp
windows/ConditionVariable.hpp)
add_library(quickstep_threading_Mutex
Mutex.cpp
Mutex.hpp
windows/Mutex.hpp)
add_library(quickstep_threading_SharedMutex
../empty_src.cpp
SharedMutex.hpp
windows/SharedMutex.hpp)
add_library(quickstep_threading_Thread
Thread.cpp
Thread.hpp
windows/Thread.hpp)
endif()
add_library(quickstep_threading_SpinMutex ../empty_src.cpp SpinMutex.hpp)
add_library(quickstep_threading_SpinSharedMutex ../empty_src.cpp SpinSharedMutex.hpp)
add_library(quickstep_threading_ThreadIDBasedMap ../empty_src.cpp ThreadIDBasedMap.hpp)
add_library(quickstep_threading_ThreadUtil ../empty_src.cpp ThreadUtil.hpp)
add_library(quickstep_threading_WinThreadsAPI ../empty_src.cpp WinThreadsAPI.hpp)
# Link dependencies:
target_link_libraries(quickstep_threading_ConditionVariable
glog
quickstep_utility_Macros)
target_link_libraries(quickstep_threading_Mutex
glog
quickstep_threading_ConditionVariable
quickstep_utility_Macros
quickstep_utility_PtrVector)
target_link_libraries(quickstep_threading_SpinMutex
quickstep_threading_Mutex
quickstep_utility_Macros)
target_link_libraries(quickstep_threading_SpinSharedMutex
quickstep_threading_Mutex
quickstep_threading_SharedMutex
quickstep_threading_ThreadUtil
quickstep_utility_Macros)
target_link_libraries(quickstep_threading_Thread
quickstep_utility_Macros)
target_link_libraries(quickstep_threading_ThreadIDBasedMap
glog
quickstep_storage_StorageConstants
quickstep_threading_Mutex
quickstep_threading_SharedMutex
quickstep_threading_SpinSharedMutex
quickstep_utility_Macros)
target_link_libraries(quickstep_threading_ThreadUtil
quickstep_threading_WinThreadsAPI
quickstep_utility_Macros)
if (QUICKSTEP_HAVE_CPP11_THREADS AND NOT QUICKSTEP_HAVE_CPP14_SHARED_MUTEX)
target_link_libraries(quickstep_threading_SharedMutex
glog
quickstep_threading_ConditionVariable
quickstep_threading_Mutex
quickstep_utility_Macros)
else()
target_link_libraries(quickstep_threading_SharedMutex
quickstep_threading_Mutex
quickstep_utility_Macros)
endif()
# Module all-in-one library:
add_library(quickstep_threading ../empty_src.cpp ThreadingModule.hpp)
target_link_libraries(quickstep_threading
quickstep_threading_ConditionVariable
quickstep_threading_Mutex
quickstep_threading_SharedMutex
quickstep_threading_SpinMutex
quickstep_threading_SpinSharedMutex
quickstep_threading_Thread
quickstep_threading_ThreadIDBasedMap
quickstep_threading_ThreadUtil
quickstep_threading_WinThreadsAPI)
# Tests:
add_executable(Mutex_unittest "${CMAKE_CURRENT_SOURCE_DIR}/tests/Mutex_unittest.cpp")
target_link_libraries(Mutex_unittest
${CMAKE_THREAD_LIBS_INIT}
gtest
gtest_main
quickstep_threading_Mutex
quickstep_threading_SpinMutex
quickstep_threading_Thread
quickstep_utility_Macros)
add_test(Mutex_unittest Mutex_unittest)
add_executable(SharedMutex_unittest "${CMAKE_CURRENT_SOURCE_DIR}/tests/SharedMutex_unittest.cpp")
target_link_libraries(SharedMutex_unittest
${CMAKE_THREAD_LIBS_INIT}
gtest
gtest_main
quickstep_threading_Mutex
quickstep_threading_SharedMutex
quickstep_threading_SpinSharedMutex
quickstep_threading_Thread
quickstep_utility_Macros)
add_test(SharedMutex_unittest SharedMutex_unittest)