[compiler-rt] 0c4863a - Reland "[TSan][libdispatch] Add interceptors for dispatch_async_and_wait()"
Julian Lettner via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 18 18:35:29 PDT 2020
Author: Julian Lettner
Date: 2020-08-18T18:34:14-07:00
New Revision: 0c4863a253957db04549815be218237d6650e5be
URL: https://github.com/llvm/llvm-project/commit/0c4863a253957db04549815be218237d6650e5be
DIFF: https://github.com/llvm/llvm-project/commit/0c4863a253957db04549815be218237d6650e5be.diff
LOG: Reland "[TSan][libdispatch] Add interceptors for dispatch_async_and_wait()"
The linker errors caused by this revision have been addressed.
Add interceptors for `dispatch_async_and_wait[_f]()` which was added in
macOS 10.14. This pair of functions is similar to `dispatch_sync()`,
but does not force a context switch of the queue onto the caller thread
when the queue is active (and hence is more efficient). For TSan, we
can apply the same semantics as for `dispatch_sync()`.
>From the header docs:
> Differences with dispatch_sync()
>
> When the runtime has brought up a thread to invoke the asynchronous
> workitems already submitted to the specified queue, that servicing
> thread will also be used to execute synchronous work submitted to the
> queue with dispatch_async_and_wait().
>
> However, if the runtime has not brought up a thread to service the
> specified queue (because it has no workitems enqueued, or only
> synchronous workitems), then dispatch_async_and_wait() will invoke the
> workitem on the calling thread, similar to the behaviour of functions
> in the dispatch_sync family.
Additional context:
> The guidance is to use `dispatch_async_and_wait()` instead of
> `dispatch_sync()` when it is necessary to mix async and sync calls on
> the same queue. `dispatch_async_and_wait()` does not guarantee
> execution on the caller thread which allows to reduce context switches
> when the target queue is active.
> https://gist.github.com/tclementdev/6af616354912b0347cdf6db159c37057
rdar://35757961
Reviewed By: kubamracek
Differential Revision: https://reviews.llvm.org/D85854
Added:
compiler-rt/test/tsan/libdispatch/async_and_wait.c
Modified:
compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp
index 5dacd3256abc..292ea5fbb239 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp
@@ -219,6 +219,9 @@ static void invoke_and_release_block(void *param) {
DISPATCH_INTERCEPT(dispatch, false)
DISPATCH_INTERCEPT(dispatch_barrier, true)
+DISPATCH_INTERCEPT_SYNC_F(dispatch_async_and_wait_f, false)
+DISPATCH_INTERCEPT_SYNC_B(dispatch_async_and_wait, false)
+
DECLARE_REAL(void, dispatch_after_f, dispatch_time_t when,
dispatch_queue_t queue, void *context, dispatch_function_t work)
@@ -746,6 +749,8 @@ void InitializeLibdispatchInterceptors() {
INTERCEPT_FUNCTION(dispatch_barrier_async_f);
INTERCEPT_FUNCTION(dispatch_barrier_sync);
INTERCEPT_FUNCTION(dispatch_barrier_sync_f);
+ INTERCEPT_FUNCTION(dispatch_async_and_wait);
+ INTERCEPT_FUNCTION(dispatch_async_and_wait_f);
INTERCEPT_FUNCTION(dispatch_after);
INTERCEPT_FUNCTION(dispatch_after_f);
INTERCEPT_FUNCTION(dispatch_once);
diff --git a/compiler-rt/test/tsan/libdispatch/async_and_wait.c b/compiler-rt/test/tsan/libdispatch/async_and_wait.c
new file mode 100644
index 000000000000..5e63c118aef5
--- /dev/null
+++ b/compiler-rt/test/tsan/libdispatch/async_and_wait.c
@@ -0,0 +1,31 @@
+// RUN: %clang_tsan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
+
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
+
+long global;
+
+int main() {
+ dispatch_queue_t q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL);
+ dispatch_semaphore_t s = dispatch_semaphore_create(0);
+
+ // Force queue to context switch onto separate thread.
+ dispatch_async(q, ^{
+ dispatch_semaphore_wait(s, DISPATCH_TIME_FOREVER);
+ });
+ dispatch_semaphore_signal(s);
+
+ global++;
+ dispatch_async_and_wait(q, ^{
+ // The queue continues to execute on separate thread. This would cause a
+ // race if we had used `dispatch_async()` without the `_and_wait` part.
+ global++;
+ });
+ global++;
+
+ fprintf(stderr, "Done.\n");
+}
+
+// CHECK: Done.
More information about the llvm-commits
mailing list