Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / Darwin / xpc.mm
blob036841ed7121152f714b284226ae20674349d356
1 // RUN: %clang_tsan %s -o %t -framework Foundation
2 // RUN: %run %t 2>&1 | FileCheck %s
4 // UNSUPPORTED: ios
6 #import <Foundation/Foundation.h>
7 #import <xpc/xpc.h>
9 long global;
11 int main(int argc, const char *argv[]) {
12   @autoreleasepool {
13     NSLog(@"Hello world.");
15     dispatch_queue_t server_q = dispatch_queue_create("server.queue", DISPATCH_QUEUE_CONCURRENT);
16     dispatch_queue_t client_q = dispatch_queue_create("client.queue", DISPATCH_QUEUE_CONCURRENT);
18     xpc_connection_t server_conn = xpc_connection_create(NULL, server_q);
20     global = 42;
22     xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) {
23       NSLog(@"global = %ld", global);
24       NSLog(@"server event handler, client = %@", client);
26       if (client == XPC_ERROR_CONNECTION_INTERRUPTED || client == XPC_ERROR_CONNECTION_INVALID) {
27         return;
28       }
29       xpc_connection_set_event_handler(client, ^(xpc_object_t object) {
30         NSLog(@"received message: %@", object);
32         xpc_object_t reply = xpc_dictionary_create_reply(object);
33         if (!reply)
34           return;
35         xpc_dictionary_set_string(reply, "reply", "value");
37         xpc_connection_t remote = xpc_dictionary_get_remote_connection(object);
38         xpc_connection_send_message(remote, reply);
39       });
41       xpc_connection_resume(client);
42     });
43     xpc_connection_resume(server_conn);
44     xpc_endpoint_t endpoint = xpc_endpoint_create(server_conn);
46     xpc_connection_t client_conn = xpc_connection_create_from_endpoint(endpoint);
47     xpc_connection_set_event_handler(client_conn, ^(xpc_object_t event) {
48       NSLog(@"client event handler, event = %@", event);
49     });
51     xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
52     xpc_dictionary_set_string(msg, "hello", "world");
53     NSLog(@"sending message: %@", msg);
55     xpc_connection_send_message_with_reply(
56         client_conn, msg, client_q, ^(xpc_object_t object) {
57           NSLog(@"received reply: %@", object);
59           xpc_connection_cancel(client_conn);
60           xpc_connection_cancel(server_conn);
62           dispatch_sync(dispatch_get_main_queue(), ^{
63             CFRunLoopStop(CFRunLoopGetCurrent());
64           });
65         });
66     xpc_connection_resume(client_conn);
68     CFRunLoopRun();
70     NSLog(@"Done.");
71   }
72   return 0;
75 // CHECK: Done.
76 // CHECK-NOT: WARNING: ThreadSanitizer