xref: /llvm-project/compiler-rt/test/tsan/libdispatch/io-cleanup.c (revision a59dad920ffb7a01f8e275ac4b6596048621fd20)
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 #include <stdlib.h>
8 
9 long my_global = 0;
10 
main(int argc,const char * argv[])11 int main(int argc, const char *argv[]) {
12   fprintf(stderr, "Hello world.\n");
13 
14   dispatch_queue_t queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
15   dispatch_semaphore_t sem = dispatch_semaphore_create(0);
16   const char *path = tempnam(NULL, "libdispatch-io-cleanup-");
17   dispatch_io_t channel;
18 
19   dispatch_fd_t fd = open(path, O_CREAT | O_WRONLY, 0666);
20   my_global++;
21   channel = dispatch_io_create(DISPATCH_IO_STREAM, fd, queue, ^(int error) {
22     my_global++;
23     dispatch_semaphore_signal(sem);
24   });
25   if (! channel) abort();
26   my_global++;
27   dispatch_io_close(channel, 0);
28   dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
29 
30   my_global++;
31   channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) {
32     my_global++;
33     dispatch_semaphore_signal(sem);
34   });
35   if (! channel) abort();
36   my_global++;
37   dispatch_io_close(channel, 0);
38   dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
39 
40   my_global++;
41   dispatch_io_t other_channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) { });
42   channel = dispatch_io_create_with_io(DISPATCH_IO_STREAM, other_channel, queue, ^(int error) {
43     my_global++;
44     dispatch_semaphore_signal(sem);
45   });
46   if (! channel) abort();
47   my_global++;
48   dispatch_io_close(channel, 0);
49   dispatch_io_close(other_channel, 0);
50   dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
51 
52   fprintf(stderr, "Done.\n");
53   return 0;
54 }
55 
56 // CHECK: Hello world.
57 // CHECK: Done.
58