[compiler-rt] r358203 - [TSan][libdispatch] Replace CFRunLoop with dispatch_semaphore, pt. 2

Julian Lettner via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 11 11:14:13 PDT 2019


Author: yln
Date: Thu Apr 11 11:14:13 2019
New Revision: 358203

URL: http://llvm.org/viewvc/llvm-project?rev=358203&view=rev
Log:
[TSan][libdispatch] Replace CFRunLoop with dispatch_semaphore, pt. 2

Reviewed By: kubamracek

Differential Revision: https://reviews.llvm.org/D60475

Modified:
    compiler-rt/trunk/test/tsan/Darwin/gcd-groups-norace.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-serial-queue-norace.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel2.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-source-event.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-source-event2.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration2.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-sync-norace.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-sync-race.mm
    compiler-rt/trunk/test/tsan/Darwin/gcd-target-queue-norace.mm

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-groups-norace.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-groups-norace.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-groups-norace.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-groups-norace.mm Thu Apr 11 11:14:13 2019
@@ -1,15 +1,15 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
 
-#import "../test.h"
+#include <stdio.h>
 
 long global;
 
 int main() {
-  NSLog(@"Hello world.");
-  NSLog(@"addr=%p\n", &global);
+  fprintf(stderr, "Hello world.\n");
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
 
   dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   global = 42;
@@ -39,13 +39,11 @@ int main() {
   dispatch_group_notify(g, q, ^{
     global = 48;
 
-    dispatch_sync(dispatch_get_main_queue(), ^{
-      CFRunLoopStop(CFRunLoopGetCurrent());
-    });
+    dispatch_semaphore_signal(done);
   });
 
-  CFRunLoopRun();
-  NSLog(@"Done.");
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
+  fprintf(stderr, "Done.\n");
 }
 
 // CHECK: Hello world.

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-serial-queue-norace.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-serial-queue-norace.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-serial-queue-norace.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-serial-queue-norace.mm Thu Apr 11 11:14:13 2019
@@ -1,15 +1,15 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
 
-#import "../test.h"
+#include <stdio.h>
 
 long global;
 
 int main() {
-  NSLog(@"Hello world.");
-  NSLog(@"addr=%p\n", &global);
+  fprintf(stderr, "Hello world.\n");
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
 
   dispatch_queue_t q1 = dispatch_queue_create("my.queue1", DISPATCH_QUEUE_CONCURRENT);
   dispatch_queue_t q2 = dispatch_queue_create("my.queue2", DISPATCH_QUEUE_SERIAL);
@@ -26,13 +26,11 @@ int main() {
   }
 
   dispatch_barrier_async(q1, ^{
-    dispatch_sync(dispatch_get_main_queue(), ^{
-      CFRunLoopStop(CFRunLoopGetCurrent());
-    });
+    dispatch_semaphore_signal(done);
   });
 
-  CFRunLoopRun();
-  NSLog(@"Done.");
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
+  fprintf(stderr, "Done.\n");
 }
 
 // CHECK: Hello world.

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel.mm Thu Apr 11 11:14:13 2019
@@ -1,11 +1,15 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
 
 int main(int argc, const char *argv[]) {
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
+
   dispatch_queue_t queue =
       dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
 
@@ -19,15 +23,13 @@ int main(int argc, const char *argv[]) {
   dispatch_source_set_cancel_handler(source, ^{
     fprintf(stderr, "global = %ld\n", global);
 
-    dispatch_sync(dispatch_get_main_queue(), ^{
-      CFRunLoopStop(CFRunLoopGetCurrent());
-    });
+    dispatch_semaphore_signal(done);
   });
 
   dispatch_resume(source);
   dispatch_cancel(source);
 
-  CFRunLoopRun();
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel2.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel2.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel2.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-source-cancel2.mm Thu Apr 11 11:14:13 2019
@@ -1,19 +1,22 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
+dispatch_semaphore_t done;
 
 void handler(void *arg) {
   fprintf(stderr, "global = %ld\n", global);
 
-  dispatch_sync(dispatch_get_main_queue(), ^{
-    CFRunLoopStop(CFRunLoopGetCurrent());
-  });
+  dispatch_semaphore_signal(done);
 }
 
 int main(int argc, const char *argv[]) {
+  done = dispatch_semaphore_create(0);
+
   dispatch_queue_t queue =
       dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
 
@@ -29,7 +32,7 @@ int main(int argc, const char *argv[]) {
   dispatch_resume(source);
   dispatch_cancel(source);
 
-  CFRunLoopRun();
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-source-event.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-source-event.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-source-event.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-source-event.mm Thu Apr 11 11:14:13 2019
@@ -1,11 +1,15 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
 
 int main(int argc, const char *argv[]) {
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
+
   dispatch_queue_t queue =
       dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
 
@@ -19,14 +23,12 @@ int main(int argc, const char *argv[]) {
   dispatch_source_set_event_handler(source, ^{
     fprintf(stderr, "global = %ld\n", global);
 
-    dispatch_sync(dispatch_get_main_queue(), ^{
-      CFRunLoopStop(CFRunLoopGetCurrent());
-    });
+    dispatch_semaphore_signal(done);
   });
 
   dispatch_resume(source);
 
-  CFRunLoopRun();
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-source-event2.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-source-event2.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-source-event2.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-source-event2.mm Thu Apr 11 11:14:13 2019
@@ -1,19 +1,22 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
+dispatch_semaphore_t done;
 
 void handler(void *arg) {
   fprintf(stderr, "global = %ld\n", global);
 
-  dispatch_sync(dispatch_get_main_queue(), ^{
-    CFRunLoopStop(CFRunLoopGetCurrent());
-  });
+  dispatch_semaphore_signal(done);
 }
 
 int main(int argc, const char *argv[]) {
+  done = dispatch_semaphore_create(0);
+
   dispatch_queue_t queue =
       dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
 
@@ -28,7 +31,7 @@ int main(int argc, const char *argv[]) {
 
   dispatch_resume(source);
 
-  CFRunLoopRun();
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration.mm Thu Apr 11 11:14:13 2019
@@ -1,11 +1,15 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
 
 int main(int argc, const char *argv[]) {
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
+
   dispatch_queue_t queue =
       dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
 
@@ -17,14 +21,12 @@ int main(int argc, const char *argv[]) {
   dispatch_source_set_registration_handler(source, ^{
     fprintf(stderr, "global = %ld\n", global);
 
-    dispatch_sync(dispatch_get_main_queue(), ^{
-      CFRunLoopStop(CFRunLoopGetCurrent());
-    });
+    dispatch_semaphore_signal(done);
   });
 
   dispatch_resume(source);
 
-  CFRunLoopRun();
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration2.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration2.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration2.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-source-registration2.mm Thu Apr 11 11:14:13 2019
@@ -1,19 +1,22 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
+dispatch_semaphore_t done;
 
 void handler(void *arg) {
   fprintf(stderr, "global = %ld\n", global);
 
-  dispatch_sync(dispatch_get_main_queue(), ^{
-    CFRunLoopStop(CFRunLoopGetCurrent());
-  });
+  dispatch_semaphore_signal(done);
 }
 
 int main(int argc, const char *argv[]) {
+  done = dispatch_semaphore_create(0);
+
   dispatch_queue_t queue =
       dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
 
@@ -26,7 +29,7 @@ int main(int argc, const char *argv[]) {
 
   dispatch_resume(source);
 
-  CFRunLoopRun();
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
 
   return 0;
 }

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-sync-norace.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-sync-norace.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-sync-norace.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-sync-norace.mm Thu Apr 11 11:14:13 2019
@@ -1,30 +1,35 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
 
 static const long nIter = 1000;
 
 int main() {
-  NSLog(@"Hello world.");
+  fprintf(stderr, "Hello world.\n");
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
+
+  dispatch_queue_t serial_q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL);
 
   global = 42;
   for (int i = 0; i < nIter; i++) {
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-      dispatch_sync(dispatch_get_main_queue(), ^{
+      dispatch_sync(serial_q, ^{
         global = i;
 
         if (i == nIter - 1) {
-          CFRunLoopStop(CFRunLoopGetCurrent());
+          dispatch_semaphore_signal(done);
         }
       });
     });
   }
 
-  CFRunLoopRun();
-  NSLog(@"Done.");
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
+  fprintf(stderr, "Done.\n");
 }
 
 // CHECK: Hello world.

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-sync-race.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-sync-race.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-sync-race.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-sync-race.mm Thu Apr 11 11:14:13 2019
@@ -1,15 +1,15 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %deflake %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
-
-#import "../test.h"
+#include "dispatch/dispatch.h"
+#include "../test.h"
 
 long global;
 
 int main() {
-  NSLog(@"Hello world.");
+  fprintf(stderr, "Hello world.\n");
   print_address("addr=", 1, &global);
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
   barrier_init(&barrier, 2);
 
   dispatch_queue_t q1 = dispatch_queue_create("my.queue1", DISPATCH_QUEUE_CONCURRENT);
@@ -27,14 +27,12 @@ int main() {
       barrier_wait(&barrier);
       global = 44;
 
-      dispatch_sync(dispatch_get_main_queue(), ^{
-        CFRunLoopStop(CFRunLoopGetCurrent());
-      });
+      dispatch_semaphore_signal(done);
     });
   });
 
-  CFRunLoopRun();
-  NSLog(@"Done.");
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
+  fprintf(stderr, "Done.\n");
 }
 
 // CHECK: Hello world.

Modified: compiler-rt/trunk/test/tsan/Darwin/gcd-target-queue-norace.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/gcd-target-queue-norace.mm?rev=358203&r1=358202&r2=358203&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/gcd-target-queue-norace.mm (original)
+++ compiler-rt/trunk/test/tsan/Darwin/gcd-target-queue-norace.mm Thu Apr 11 11:14:13 2019
@@ -1,11 +1,14 @@
-// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-#import <Foundation/Foundation.h>
+#include "dispatch/dispatch.h"
+
+#include <stdio.h>
 
 long global;
 
 int main(int argc, const char *argv[]) {
+  dispatch_semaphore_t done = dispatch_semaphore_create(0);
   dispatch_queue_t target_queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
   dispatch_queue_t q1 = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
   dispatch_queue_t q2 = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
@@ -17,25 +20,22 @@ int main(int argc, const char *argv[]) {
       global++;
 
       if (global == 200000) {
-        dispatch_sync(dispatch_get_main_queue(), ^{
-          CFRunLoopStop(CFRunLoopGetCurrent());
-        });
+        dispatch_semaphore_signal(done);
       }
     });
     dispatch_async(q2, ^{
       global++;
 
       if (global == 200000) {
-        dispatch_sync(dispatch_get_main_queue(), ^{
-          CFRunLoopStop(CFRunLoopGetCurrent());
-        });
+        dispatch_semaphore_signal(done);
       }
     });
   }
 
-  CFRunLoopRun();
-  NSLog(@"Done.");
+  dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
+  fprintf(stderr, "Done.\n");
   return 0;
 }
 
 // CHECK-NOT: WARNING: ThreadSanitizer
+// CHECK: Done.




More information about the llvm-commits mailing list