50 lines · c
1// RUN: %clang_tsan %s -o %t2// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'3 4#include <dispatch/dispatch.h>5 6#include <stdio.h>7#include <stdlib.h>8 9dispatch_queue_t queue;10dispatch_data_t data;11dispatch_semaphore_t sem;12const char *path;13 14long my_global = 0;15 16int main(int argc, const char *argv[]) {17 fprintf(stderr, "Hello world.\n");18 19 queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);20 sem = dispatch_semaphore_create(0);21 path = tempnam(NULL, "libdispatch-io-barrier");22 char buf[1000];23 data = dispatch_data_create(buf, sizeof(buf), NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);24 25 dispatch_io_t channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) { });26 if (! channel) abort();27 dispatch_io_set_high_water(channel, 1);28 29 for (int i = 0; i < 1000; i++) {30 dispatch_io_barrier(channel, ^{31 my_global = 42;32 });33 }34 35 dispatch_io_barrier(channel, ^{36 my_global = 43;37 38 dispatch_semaphore_signal(sem);39 });40 41 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);42 dispatch_io_close(channel, 0);43 44 fprintf(stderr, "Done.\n");45 return 0;46}47 48// CHECK: Hello world.49// CHECK: Done.50