1 // RUN: %clang_tsan %s -o %t -undefined dynamic_lookup
2 // RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
4 #include "dispatch/dispatch.h"
8 // Allow compilation with pre-macOS 10.14 (and aligned) SDKs
9 API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
10 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
11 void dispatch_async_and_wait(dispatch_queue_t queue
,
12 DISPATCH_NOESCAPE dispatch_block_t block
);
17 // Guard execution on pre-macOS 10.14 (and aligned) platforms
18 if (dispatch_async_and_wait
== NULL
) {
19 fprintf(stderr
, "Done.\n");
23 dispatch_queue_t q
= dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL
);
24 dispatch_semaphore_t s
= dispatch_semaphore_create(0);
26 // Force queue to context switch onto separate thread.
28 dispatch_semaphore_wait(s
, DISPATCH_TIME_FOREVER
);
30 dispatch_semaphore_signal(s
);
33 dispatch_async_and_wait(q
, ^{
34 // The queue continues to execute on separate thread. This would cause a
35 // race if we had used `dispatch_async()` without the `_and_wait` part.
40 fprintf(stderr
, "Done.\n");