1 // RUN: %clang_tsan %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
4 #include <dispatch/dispatch.h>
10 int main(int argc
, const char *argv
[]) {
11 fprintf(stderr
, "Hello world.\n");
13 dispatch_queue_t queue
= dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL
);
14 dispatch_semaphore_t sem
= dispatch_semaphore_create(0);
16 const char *path
= tempnam(NULL
, "libdispatch-fd-");
18 dispatch_io_t channel
= dispatch_io_create_with_path(DISPATCH_IO_STREAM
, path
, O_CREAT
| O_WRONLY
,
19 0666, queue
, ^(int error
) { });
20 dispatch_io_set_high_water(channel
, 1);
23 dispatch_data_t data
= dispatch_data_create(buf
, sizeof(buf
), NULL
, DISPATCH_DATA_DESTRUCTOR_DEFAULT
);
26 dispatch_io_write(channel
, 0, data
, queue
, ^(bool done
, dispatch_data_t remainingData
, int error
) {
28 dispatch_async(queue
, ^{
31 dispatch_semaphore_signal(sem
);
36 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
38 dispatch_io_close(channel
, 0);
39 channel
= dispatch_io_create_with_path(DISPATCH_IO_STREAM
, path
, O_RDONLY
,
40 0, queue
, ^(int error
) { });
41 dispatch_io_set_high_water(channel
, 1);
44 dispatch_io_read(channel
, 0, SIZE_MAX
, queue
, ^(bool done
, dispatch_data_t remainingData
, int error
) {
46 dispatch_async(queue
, ^{
49 dispatch_semaphore_signal(sem
);
54 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
56 fprintf(stderr
, "Done.\n");
60 // CHECK: Hello world.