Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / Darwin / xpc-race.mm
blob872c9ee73e138790434125c4ba669b18a447d42b
1 // RUN: %clangxx_tsan %s -o %t -framework Foundation
2 // RUN: %deflake %run %t 2>&1 | FileCheck %s
4 // UNSUPPORTED: ios
6 #import <Foundation/Foundation.h>
7 #import <xpc/xpc.h>
8 #import <stdatomic.h>
10 #import "../test.h"
12 long global;
14 _Atomic(long) msg_counter;
15 _Atomic(long) processed_msgs;
16 xpc_connection_t server_conn;
17 xpc_connection_t client_conns[2];
19 int main(int argc, const char *argv[]) {
20   @autoreleasepool {
21     fprintf(stderr, "Hello world.\n");
22     // CHECK: Hello world.
24     barrier_init(&barrier, 2);
26     dispatch_queue_t server_q = dispatch_queue_create("server.queue", DISPATCH_QUEUE_CONCURRENT);
28     server_conn = xpc_connection_create(NULL, server_q);
30     xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) {
31       fprintf(stderr, "server event handler, client = %p\n", client);
33       if (client == XPC_ERROR_CONNECTION_INTERRUPTED || client == XPC_ERROR_CONNECTION_INVALID) {
34         return;
35       }
36       xpc_connection_set_event_handler(client, ^(xpc_object_t object) {
37         fprintf(stderr, "received message: %p\n", object);
39         long msg_number = atomic_fetch_add_explicit(&msg_counter, 1, memory_order_relaxed);
41         if (msg_number == 0)
42           barrier_wait(&barrier);
44         global++;
45         // CHECK: WARNING: ThreadSanitizer: data race
46         // CHECK:   Write of size 8
47         // CHECK:     #0 {{.*}}xpc-race.mm:[[@LINE-3]]
48         // CHECK:   Previous write of size 8
49         // CHECK:     #0 {{.*}}xpc-race.mm:[[@LINE-5]]
50         // CHECK: Location is global 'global'
52         if (msg_number == 1)
53           barrier_wait(&barrier);
55         atomic_fetch_add(&processed_msgs, 1);
57         dispatch_sync(dispatch_get_main_queue(), ^{
58           if (processed_msgs >= 2) {
59             xpc_connection_cancel(client_conns[0]);
60             xpc_connection_cancel(client_conns[1]);
61             xpc_connection_cancel(server_conn);
62             CFRunLoopStop(CFRunLoopGetCurrent());
63           }
64         });
65       });
67       xpc_connection_resume(client);
68     });
69     xpc_connection_resume(server_conn);
70     xpc_endpoint_t endpoint = xpc_endpoint_create(server_conn);
72     for (int i = 0; i < 2; i++) {
73       client_conns[i] = xpc_connection_create_from_endpoint(endpoint);
74       xpc_connection_set_event_handler(client_conns[i], ^(xpc_object_t event) {
75         fprintf(stderr, "client event handler, event = %p\n", event);
76       });
78       xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
79       xpc_dictionary_set_string(msg, "hello", "world");
80       fprintf(stderr, "sending message: %p\n", msg);
82       xpc_connection_send_message(client_conns[i], msg);
83       xpc_connection_resume(client_conns[i]);
84     }
86     CFRunLoopRun();
88     fprintf(stderr, "Done.\n");
89     // CHECK: Done.
90   }
91   return 0;