| 1 | // RUN: %clang_tsan %s -o %t |
| 2 | // RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer' |
| 3 | |
| 4 | #include <dispatch/dispatch.h> |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | |
| 8 | long global; |
| 9 | |
| 10 | int main(int argc, const char *argv[]) { |
| 11 | fprintf(stderr, format: "Hello world.\n" ); |
| 12 | |
| 13 | dispatch_queue_t q = dispatch_queue_create("my.queue" , DISPATCH_QUEUE_SERIAL); |
| 14 | dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q); |
| 15 | long long interval_ms = 10; |
| 16 | dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval_ms * NSEC_PER_MSEC, 0); |
| 17 | |
| 18 | dispatch_semaphore_t sem = dispatch_semaphore_create(0); |
| 19 | dispatch_source_set_event_handler(timer, ^{ |
| 20 | fprintf(stderr, format: "timer\n" ); |
| 21 | global++; |
| 22 | |
| 23 | if (global > 50) { |
| 24 | dispatch_semaphore_signal(sem); |
| 25 | dispatch_suspend(timer); |
| 26 | } |
| 27 | }); |
| 28 | dispatch_resume(timer); |
| 29 | |
| 30 | dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); |
| 31 | |
| 32 | fprintf(stderr, format: "Done.\n" ); |
| 33 | } |
| 34 | |
| 35 | // CHECK: Hello world. |
| 36 | // CHECK: timer |
| 37 | // CHECK: Done. |
| 38 | // CHECK-NOT: timer |
| 39 | |