1 | // RUN: %clang_tsan %s -o %t -framework Foundation |
2 | // RUN: %run %t 2>&1 | FileCheck %s |
3 | |
4 | // UNSUPPORTED: ios |
5 | |
6 | #import <Foundation/Foundation.h> |
7 | #import <xpc/xpc.h> |
8 | |
9 | long global; |
10 | |
11 | int main(int argc, const char *argv[]) { |
12 | @autoreleasepool { |
13 | NSLog(@"Hello world." ); |
14 | |
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); |
17 | |
18 | xpc_connection_t server_conn = xpc_connection_create(NULL, server_q); |
19 | |
20 | global = 42; |
21 | |
22 | xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) { |
23 | NSLog(@"global = %ld" , global); |
24 | NSLog(@"server event handler, client = %@" , client); |
25 | |
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); |
31 | |
32 | xpc_object_t reply = xpc_dictionary_create_reply(object); |
33 | if (!reply) |
34 | return; |
35 | xpc_dictionary_set_string(reply, "reply" , "value" ); |
36 | |
37 | xpc_connection_t remote = xpc_dictionary_get_remote_connection(object); |
38 | xpc_connection_send_message(remote, reply); |
39 | }); |
40 | |
41 | xpc_connection_resume(client); |
42 | }); |
43 | xpc_connection_resume(server_conn); |
44 | xpc_endpoint_t endpoint = xpc_endpoint_create(server_conn); |
45 | |
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 | }); |
50 | |
51 | xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0); |
52 | xpc_dictionary_set_string(msg, "hello" , "world" ); |
53 | NSLog(@"sending message: %@" , msg); |
54 | |
55 | xpc_connection_send_message_with_reply( |
56 | client_conn, msg, client_q, ^(xpc_object_t object) { |
57 | NSLog(@"received reply: %@" , object); |
58 | |
59 | xpc_connection_cancel(client_conn); |
60 | xpc_connection_cancel(server_conn); |
61 | |
62 | dispatch_sync(dispatch_get_main_queue(), ^{ |
63 | CFRunLoopStop(CFRunLoopGetCurrent()); |
64 | }); |
65 | }); |
66 | xpc_connection_resume(client_conn); |
67 | |
68 | CFRunLoopRun(); |
69 | |
70 | NSLog(@"Done." ); |
71 | } |
72 | return 0; |
73 | } |
74 | |
75 | // CHECK: Done. |
76 | // CHECK-NOT: WARNING: ThreadSanitizer |
77 | |