blob: 0bad9a7dc31be93e94c77b1602bef3d10d9175eb [file] [log] [blame]
Marat Dukhanda486af2020-04-10 23:46:52 -07001load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
2
3licenses(["notice"])
4
5############################## pthreadpool library #############################
6
7INTERNAL_HDRS = [
8 "src/threadpool-atomics.h",
9 "src/threadpool-common.h",
10 "src/threadpool-object.h",
11 "src/threadpool-utils.h",
12]
13
14PORTABLE_SRCS = [
15 "src/memory.c",
16 "src/portable-api.c",
17]
18
Marat Dukhan0672a7e2020-05-02 22:29:51 -070019ARCH_SPECIFIC_SRCS = [
20 "src/fastpath.c",
21]
22
Marat Dukhanda486af2020-04-10 23:46:52 -070023PTHREADS_IMPL_SRCS = PORTABLE_SRCS + ["src/pthreads.c"]
24
25GCD_IMPL_SRCS = PORTABLE_SRCS + ["src/gcd.c"]
26
27WINDOWS_IMPL_SRCS = PORTABLE_SRCS + ["src/windows.c"]
28
29SHIM_IMPL_SRCS = ["src/shim.c"]
30
31cc_library(
32 name = "pthreadpool",
33 srcs = select({
34 ":pthreadpool_sync_primitive_explicit_condvar": INTERNAL_HDRS + PTHREADS_IMPL_SRCS,
35 ":pthreadpool_sync_primitive_explicit_futex": INTERNAL_HDRS + PTHREADS_IMPL_SRCS,
36 ":pthreadpool_sync_primitive_explicit_gcd": INTERNAL_HDRS + GCD_IMPL_SRCS,
37 ":pthreadpool_sync_primitive_explicit_event": INTERNAL_HDRS + WINDOWS_IMPL_SRCS,
38 ":emscripten_with_threads": INTERNAL_HDRS + PTHREADS_IMPL_SRCS,
Marat Dukhan28f251e2020-05-04 15:00:58 -070039 ":emscripten": INTERNAL_HDRS + SHIM_IMPL_SRCS,
Marat Dukhanda486af2020-04-10 23:46:52 -070040 ":macos_x86": INTERNAL_HDRS + GCD_IMPL_SRCS,
41 ":macos_x86_64": INTERNAL_HDRS + GCD_IMPL_SRCS,
42 ":ios": INTERNAL_HDRS + GCD_IMPL_SRCS,
Marat Dukhan0672a7e2020-05-02 22:29:51 -070043 ":watchos": INTERNAL_HDRS + GCD_IMPL_SRCS,
44 ":tvos": INTERNAL_HDRS + GCD_IMPL_SRCS,
Marat Dukhanda486af2020-04-10 23:46:52 -070045 ":windows_x86_64": INTERNAL_HDRS + WINDOWS_IMPL_SRCS,
Marat Dukhanda486af2020-04-10 23:46:52 -070046 "//conditions:default": INTERNAL_HDRS + PTHREADS_IMPL_SRCS,
Marat Dukhan0672a7e2020-05-02 22:29:51 -070047 }) + select({
48 ":linux_x86_64": ARCH_SPECIFIC_SRCS,
49 ":android_x86": ARCH_SPECIFIC_SRCS,
50 ":android_x86_64": ARCH_SPECIFIC_SRCS,
51 ":windows_x86_64": ARCH_SPECIFIC_SRCS,
52 ":macos_x86": ARCH_SPECIFIC_SRCS,
53 ":macos_x86_64": ARCH_SPECIFIC_SRCS,
54 ":ios_x86": ARCH_SPECIFIC_SRCS,
55 ":ios_x86_64": ARCH_SPECIFIC_SRCS,
56 ":watchos_x86": ARCH_SPECIFIC_SRCS,
57 ":watchos_x86_64": ARCH_SPECIFIC_SRCS,
58 ":tvos_x86_64": ARCH_SPECIFIC_SRCS,
59 "//conditions:default": [],
Marat Dukhanda486af2020-04-10 23:46:52 -070060 }),
61 copts = [
62 "-std=gnu11",
63 ] + select({
64 ":optimized_build": ["-O2"],
65 "//conditions:default": [],
66 }) + select({
67 ":linux_arm": ["-DPTHREADPOOL_USE_CPUINFO=1"],
68 ":linux_armhf": ["-DPTHREADPOOL_USE_CPUINFO=1"],
69 ":linux_aarch64": ["-DPTHREADPOOL_USE_CPUINFO=1"],
70 ":android_armv7": ["-DPTHREADPOOL_USE_CPUINFO=1"],
71 ":android_arm64": ["-DPTHREADPOOL_USE_CPUINFO=1"],
72 "//conditions:default": ["-DPTHREADPOOL_USE_CPUINFO=0"],
73 }) + select({
74 ":pthreadpool_sync_primitive_explicit_condvar": [
75 "-DPTHREADPOOL_USE_CONDVAR=1",
76 "-DPTHREADPOOL_USE_FUTEX=0",
77 "-DPTHREADPOOL_USE_GCD=0",
78 "-DPTHREADPOOL_USE_EVENT=0",
79 ],
80 ":pthreadpool_sync_primitive_explicit_futex": [
81 "-DPTHREADPOOL_USE_CONDVAR=0",
82 "-DPTHREADPOOL_USE_FUTEX=1",
83 "-DPTHREADPOOL_USE_GCD=0",
84 "-DPTHREADPOOL_USE_EVENT=0",
85 ],
86 ":pthreadpool_sync_primitive_explicit_gcd": [
87 "-DPTHREADPOOL_USE_CONDVAR=0",
88 "-DPTHREADPOOL_USE_FUTEX=0",
89 "-DPTHREADPOOL_USE_GCD=1",
90 "-DPTHREADPOOL_USE_EVENT=0",
91 ],
92 ":pthreadpool_sync_primitive_explicit_event": [
93 "-DPTHREADPOOL_USE_CONDVAR=0",
94 "-DPTHREADPOOL_USE_FUTEX=0",
95 "-DPTHREADPOOL_USE_GCD=0",
96 "-DPTHREADPOOL_USE_EVENT=1",
97 ],
98 "//conditions:default": [],
Marat Dukhan0672a7e2020-05-02 22:29:51 -070099 }) + select({
100 ":linux_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
101 ":android_x86": ["-DPTHREADPOOL_USE_FASTPATH=1"],
102 ":android_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
103 ":windows_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
104 ":macos_x86": ["-DPTHREADPOOL_USE_FASTPATH=1"],
105 ":macos_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
106 ":ios_x86": ["-DPTHREADPOOL_USE_FASTPATH=1"],
107 ":ios_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
108 ":watchos_x86": ["-DPTHREADPOOL_USE_FASTPATH=1"],
109 ":watchos_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
110 ":tvos_x86_64": ["-DPTHREADPOOL_USE_FASTPATH=1"],
111 "//conditions:default": ["-DPTHREADPOOL_USE_FASTPATH=0"],
Marat Dukhanda486af2020-04-10 23:46:52 -0700112 }),
113 hdrs = [
114 "include/pthreadpool.h",
115 ],
116 defines = [
117 "PTHREADPOOL_NO_DEPRECATED_API",
118 ],
119 includes = [
120 "include",
121 ],
122 linkopts = select({
123 ":emscripten_with_threads": [
124 "-s ALLOW_BLOCKING_ON_MAIN_THREAD=1",
125 "-s PTHREAD_POOL_SIZE=8",
126 ],
127 "//conditions:default": [],
128 }),
129 strip_include_prefix = "include",
130 deps = [
131 "@FXdiv",
132 ] + select({
133 ":linux_arm": ["@cpuinfo"],
134 ":linux_armhf": ["@cpuinfo"],
135 ":linux_aarch64": ["@cpuinfo"],
136 ":android_armv7": ["@cpuinfo"],
137 ":android_arm64": ["@cpuinfo"],
138 "//conditions:default": [],
139 }),
140 visibility = ["//visibility:public"],
141)
142
143################################## Unit tests ##################################
144
145EMSCRIPTEN_TEST_LINKOPTS = [
146 "-s ASSERTIONS=2",
147 "-s ERROR_ON_UNDEFINED_SYMBOLS=1",
148 "-s DEMANGLE_SUPPORT=1",
149 "-s EXIT_RUNTIME=1",
150 "-s ALLOW_MEMORY_GROWTH=0",
151 "-s TOTAL_MEMORY=67108864", # 64M
152]
153
154cc_test(
155 name = "pthreadpool_test",
156 srcs = ["test/pthreadpool.cc"],
157 linkopts = select({
158 ":emscripten": EMSCRIPTEN_TEST_LINKOPTS,
159 "//conditions:default": [],
160 }),
161 deps = [
162 ":pthreadpool",
163 "@com_google_googletest//:gtest_main",
164 ],
165)
166
167################################## Benchmarks ##################################
168
169EMSCRIPTEN_BENCHMARK_LINKOPTS = [
170 "-s ASSERTIONS=1",
171 "-s ERROR_ON_UNDEFINED_SYMBOLS=1",
172 "-s EXIT_RUNTIME=1",
173 "-s ALLOW_MEMORY_GROWTH=0",
174]
175
176cc_binary(
177 name = "latency_bench",
178 srcs = ["bench/latency.cc"],
179 linkopts = select({
180 ":emscripten": EMSCRIPTEN_BENCHMARK_LINKOPTS,
181 "//conditions:default": [],
182 }),
183 deps = [
184 ":pthreadpool",
185 "@com_google_benchmark//:benchmark",
186 ],
187)
188
189cc_binary(
190 name = "throughput_bench",
191 srcs = ["bench/throughput.cc"],
192 linkopts = select({
193 ":emscripten": EMSCRIPTEN_BENCHMARK_LINKOPTS,
194 "//conditions:default": [],
195 }),
196 deps = [
197 ":pthreadpool",
198 "@com_google_benchmark//:benchmark",
199 ],
200)
201
202############################# Build configurations #############################
203
204# Synchronize workers using pthreads condition variable.
205config_setting(
206 name = "pthreadpool_sync_primitive_explicit_condvar",
207 define_values = {"pthreadpool_sync_primitive": "condvar"},
208)
209
210# Synchronize workers using futex.
211config_setting(
212 name = "pthreadpool_sync_primitive_explicit_futex",
213 define_values = {"pthreadpool_sync_primitive": "futex"},
214)
215
216# Synchronize workers using Grand Central Dispatch.
217config_setting(
218 name = "pthreadpool_sync_primitive_explicit_gcd",
219 define_values = {"pthreadpool_sync_primitive": "gcd"},
220)
221
222# Synchronize workers using WinAPI event.
223config_setting(
224 name = "pthreadpool_sync_primitive_explicit_event",
225 define_values = {"pthreadpool_sync_primitive": "event"},
226)
227
228config_setting(
229 name = "optimized_build",
230 values = {
231 "compilation_mode": "opt",
232 },
233)
234
235config_setting(
Marat Dukhan0672a7e2020-05-02 22:29:51 -0700236 name = "linux_x86_64",
237 values = {"cpu": "k8"},
238)
239
240config_setting(
Marat Dukhanda486af2020-04-10 23:46:52 -0700241 name = "linux_arm",
242 values = {"cpu": "arm"},
243)
244
245config_setting(
246 name = "linux_armhf",
247 values = {"cpu": "armhf"},
248)
249
250config_setting(
251 name = "linux_aarch64",
252 values = {"cpu": "aarch64"},
253)
254
255config_setting(
Marat Dukhan0672a7e2020-05-02 22:29:51 -0700256 name = "android_x86",
257 values = {
258 "crosstool_top": "//external:android/crosstool",
259 "cpu": "x86",
260 },
261)
262
263config_setting(
264 name = "android_x86_64",
265 values = {
266 "crosstool_top": "//external:android/crosstool",
267 "cpu": "x86_64",
268 },
269)
270
271config_setting(
Marat Dukhanda486af2020-04-10 23:46:52 -0700272 name = "android_armv7",
273 values = {
274 "crosstool_top": "//external:android/crosstool",
275 "cpu": "armeabi-v7a",
276 },
277)
278
279config_setting(
280 name = "android_arm64",
281 values = {
282 "crosstool_top": "//external:android/crosstool",
283 "cpu": "arm64-v8a",
284 },
285)
286
287# Note: we need to individually match x86 and x86-64 macOS rather than use
288# catch-all "apple_platform_type": "macos" because that option defaults to
289# "macos" even when building on Linux!
290config_setting(
291 name = "macos_x86",
292 values = {
293 "apple_platform_type": "macos",
294 "cpu": "darwin",
295 },
296)
297
298config_setting(
299 name = "macos_x86_64",
300 values = {
301 "apple_platform_type": "macos",
302 "cpu": "darwin_x86_64",
303 },
304)
305
306config_setting(
307 name = "ios",
308 values = {
309 "crosstool_top": "@bazel_tools//tools/cpp:toolchain",
310 "apple_platform_type": "ios",
311 },
312)
313
314config_setting(
Marat Dukhan0672a7e2020-05-02 22:29:51 -0700315 name = "ios_x86",
Marat Dukhanda486af2020-04-10 23:46:52 -0700316 values = {
Marat Dukhan0672a7e2020-05-02 22:29:51 -0700317 "apple_platform_type": "ios",
318 "cpu": "ios_i386",
Marat Dukhanda486af2020-04-10 23:46:52 -0700319 },
320)
321
322config_setting(
Marat Dukhan0672a7e2020-05-02 22:29:51 -0700323 name = "ios_x86_64",
Marat Dukhanda486af2020-04-10 23:46:52 -0700324 values = {
Marat Dukhan0672a7e2020-05-02 22:29:51 -0700325 "apple_platform_type": "ios",
326 "cpu": "ios_x86_64",
327 },
328)
329
330config_setting(
331 name = "watchos",
332 values = {
333 "crosstool_top": "@bazel_tools//tools/cpp:toolchain",
334 "apple_platform_type": "watchos",
335 },
336)
337
338config_setting(
339 name = "watchos_x86",
340 values = {
341 "apple_platform_type": "watchos",
342 "cpu": "watchos_i386",
343 },
344)
345
346config_setting(
347 name = "watchos_x86_64",
348 values = {
349 "apple_platform_type": "watchos",
350 "cpu": "watchos_x86_64",
351 },
352)
353
354config_setting(
355 name = "tvos",
356 values = {
357 "crosstool_top": "@bazel_tools//tools/cpp:toolchain",
358 "apple_platform_type": "tvos",
359 },
360)
361
362config_setting(
363 name = "tvos_x86_64",
364 values = {
365 "apple_platform_type": "tvos",
366 "cpu": "tvos_x86_64",
367 },
368)
369
370config_setting(
371 name = "windows_x86_64",
372 values = {
373 "cpu": "x64_windows",
Marat Dukhanda486af2020-04-10 23:46:52 -0700374 },
375)
376
377config_setting(
378 name = "emscripten",
379 values = {
380 "crosstool_top": "//toolchain:emscripten",
381 }
382)
383
384config_setting(
385 name = "emscripten_with_threads",
386 values = {
387 "crosstool_top": "//toolchain:emscripten",
388 "copt": "-pthread",
389 }
390)