[PATCH] D60327: [TSan][libdispatch] Add RunLoop helper to ease porting tests

Julian Lettner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 5 11:26:45 PDT 2019


yln created this revision.
Herald added subscribers: llvm-commits, Sanitizers, kubamracek.
Herald added projects: Sanitizers, LLVM.
yln edited the summary of this revision.
yln added reviewers: kubamracek, dcoughlin, delcypher.

Many existing libdispatch tests use CFRunLoop{Run,Stop} to setup the
test harness. Unfortunately, libdispatch only offers `dispatch_main`
which has no built-in functionality for stopping the run loop.

To emulate the Foundation behavior we schedule an "exit block" to the
main queue that exists when a flag is set. If the flag hasn't been set
yet, the block resubmits itself to to the queue.

I included one adapted example test to show how this would be used.
Let me know if you think this is a good approach and if it sufficiently retains
the original test semantics.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60327

Files:
  compiler-rt/test/tsan/Darwin/gcd-blocks.mm
  compiler-rt/test/tsan/Darwin/runloop.h


Index: compiler-rt/test/tsan/Darwin/runloop.h
===================================================================
--- /dev/null
+++ compiler-rt/test/tsan/Darwin/runloop.h
@@ -0,0 +1,30 @@
+// Emulation of Foundation RunLoop functionality.
+// Supports porting of libdispatch tests to platforms without Foundation.
+
+#include <dispatch/dispatch.h>
+#include <stdio.h>  // fprintf
+#include <stdlib.h> // exit
+
+static bool runloop_exit = false;
+
+static inline void RunLoopStop() {
+  runloop_exit = true;
+}
+
+// Start executing blocks submitted to the main queue.
+// Never returns (should be the last statement in a test's main function). Loops
+// indefinitely until a call to RunLoopStop is scheduled on the main queue.
+// Prints "Done." to stderr before exiting.
+static inline void RunLoopRun() {
+  __block dispatch_block_t exitBlock = ^{
+    if (runloop_exit) {
+      fprintf(stderr, "Done.\n");
+      exit(0);
+    } else {
+      dispatch_async(dispatch_get_main_queue(), exitBlock);
+    }
+  };
+
+  dispatch_async(dispatch_get_main_queue(), exitBlock);
+  dispatch_main(); // Never returns
+}
Index: compiler-rt/test/tsan/Darwin/gcd-blocks.mm
===================================================================
--- compiler-rt/test/tsan/Darwin/gcd-blocks.mm
+++ compiler-rt/test/tsan/Darwin/gcd-blocks.mm
@@ -1,7 +1,7 @@
-// RUN: %clangxx_tsan %s -o %t -framework Foundation
-// RUN: %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_tsan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
 
-#import <Foundation/Foundation.h>
+#include "runloop.h"
 
 int main() {
   fprintf(stderr, "start\n");
@@ -19,16 +19,13 @@
     fprintf(stderr, "block_var = %ld\n", block_var);
 
     dispatch_sync(dispatch_get_main_queue(), ^{
-      CFRunLoopStop(CFRunLoopGetCurrent());
+      RunLoopStop();
     });
   });
-  
-  CFRunLoopRun();
-  fprintf(stderr, "done\n");
+
+  RunLoopRun();
 }
 
 // CHECK: start
 // CHECK: block_var = 42
-// CHECK: done
-// CHECK-NOT: WARNING: ThreadSanitizer
-// CHECK-NOT: CHECK failed
+// CHECK: Done.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60327.193918.patch
Type: text/x-patch
Size: 2076 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190405/59eedfff/attachment.bin>


More information about the llvm-commits mailing list