Re: [PATCH] D21816: [tsan] Stop extending the block’s lifetime in dispatch_group_async

Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 29 03:37:57 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL274117: [tsan] Stop extending the block’s lifetime in dispatch_group_async (authored by kuba.brecka).

Changed prior to commit:
  http://reviews.llvm.org/D21816?vs=62139&id=62200#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D21816

Files:
  compiler-rt/trunk/lib/tsan/rtl/tsan_libdispatch_mac.cc
  compiler-rt/trunk/test/tsan/Darwin/gcd-groups-destructor.mm

Index: compiler-rt/trunk/lib/tsan/rtl/tsan_libdispatch_mac.cc
===================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_libdispatch_mac.cc
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_libdispatch_mac.cc
@@ -283,8 +283,10 @@
   SCOPED_TSAN_INTERCEPTOR(dispatch_group_async, group, queue, block);
   dispatch_retain(group);
   dispatch_group_enter(group);
+  __block dispatch_block_t block_copy = (dispatch_block_t)_Block_copy(block);
   WRAP(dispatch_async)(queue, ^(void) {
-    block();
+    block_copy();
+    _Block_release(block_copy);
     WRAP(dispatch_group_leave)(group);
     dispatch_release(group);
   });
Index: compiler-rt/trunk/test/tsan/Darwin/gcd-groups-destructor.mm
===================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-groups-destructor.mm
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-groups-destructor.mm
@@ -0,0 +1,43 @@
+// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s
+
+#import <Foundation/Foundation.h>
+
+#import <memory>
+#import <stdatomic.h>
+
+_Atomic(long) destructor_counter = 0;
+
+struct MyStruct {
+  virtual ~MyStruct() {
+    usleep(10000);
+    atomic_fetch_add_explicit(&destructor_counter, 1, memory_order_relaxed);
+  }
+};
+
+int main(int argc, const char *argv[]) {
+  fprintf(stderr, "Hello world.\n");
+
+  dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+  dispatch_group_t g = dispatch_group_create();
+
+  for (int i = 0; i < 100; i++) {
+    std::shared_ptr<MyStruct> shared(new MyStruct());
+
+    dispatch_group_async(g, q, ^{
+      shared.get(); // just to make sure the object is captured by the block
+    });
+  }
+
+  dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
+
+  if (destructor_counter != 100) {
+    abort();
+  }
+
+  fprintf(stderr, "Done.\n");
+}
+
+// CHECK: Hello world.
+// CHECK-NOT: WARNING: ThreadSanitizer
+// CHECK: Done.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21816.62200.patch
Type: text/x-patch
Size: 2020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160629/0cb5875f/attachment.bin>


More information about the llvm-commits mailing list