1 // RUN: %clang_tsan %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
4 #include <dispatch/dispatch.h>
9 dispatch_queue_t queue
;
11 dispatch_semaphore_t sem
;
16 void test_dispatch_io_write() {
17 dispatch_io_t channel
= dispatch_io_create_with_path(DISPATCH_IO_STREAM
, path
, O_CREAT
| O_WRONLY
, 0666, queue
, ^(int error
) { });
18 if (! channel
) abort();
19 dispatch_io_set_high_water(channel
, 1);
22 dispatch_io_write(channel
, 0, data
, queue
, ^(bool done
, dispatch_data_t remainingData
, int error
) {
25 dispatch_async(queue
, ^{
28 dispatch_semaphore_signal(sem
);
33 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
35 dispatch_io_close(channel
, 0);
38 void test_dispatch_write() {
39 dispatch_fd_t fd
= open(path
, O_CREAT
| O_WRONLY
, 0666);
40 if (fd
== -1) abort();
43 dispatch_write(fd
, data
, queue
, ^(dispatch_data_t data
, int error
) {
46 dispatch_async(queue
, ^{
49 dispatch_semaphore_signal(sem
);
53 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
58 void test_dispatch_io_read() {
59 dispatch_io_t channel
= dispatch_io_create_with_path(DISPATCH_IO_STREAM
, path
, O_RDONLY
,
60 0, queue
, ^(int error
) { });
61 dispatch_io_set_high_water(channel
, 1);
64 dispatch_io_read(channel
, 0, SIZE_MAX
, queue
, ^(bool done
, dispatch_data_t remainingData
, int error
) {
67 dispatch_async(queue
, ^{
70 dispatch_semaphore_signal(sem
);
75 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
77 dispatch_io_close(channel
, 0);
80 void test_dispatch_read() {
81 dispatch_fd_t fd
= open(path
, O_RDONLY
, 0);
82 if (fd
== -1) abort();
85 dispatch_read(fd
, SIZE_MAX
, queue
, ^(dispatch_data_t data
, int error
) {
88 dispatch_async(queue
, ^{
90 dispatch_semaphore_signal(sem
);
94 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
99 int main(int argc
, const char *argv
[]) {
100 fprintf(stderr
, "Hello world.\n");
102 queue
= dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL
);
103 sem
= dispatch_semaphore_create(0);
104 path
= tempnam(NULL
, "libdispatch-io-");
106 data
= dispatch_data_create(buf
, sizeof(buf
), NULL
, DISPATCH_DATA_DESTRUCTOR_DEFAULT
);
108 test_dispatch_io_write();
109 test_dispatch_write();
110 test_dispatch_io_read();
111 test_dispatch_read();
113 fprintf(stderr
, "Done.\n");
117 // CHECK: Hello world.