1 // RUN: %clang_tsan %s -o %t -framework Foundation
2 // RUN: %run %t 2>&1 | FileCheck %s
6 #import <Foundation/Foundation.h>
11 int main(int argc, const char *argv[]) {
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);
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) {
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);
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);
41 xpc_connection_resume(client);
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);
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());
66 xpc_connection_resume(client_conn);
76 // CHECK-NOT: WARNING: ThreadSanitizer