1 // RUN: %clang_tsan %s -o %t -framework Foundation
2 // RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s
4 #import <Foundation/Foundation.h>
9 int main(int argc, const char *argv[]) {
11 NSLog(@"Hello world.");
13 dispatch_queue_t server_q = dispatch_queue_create("server.queue", DISPATCH_QUEUE_CONCURRENT);
14 dispatch_queue_t client_q = dispatch_queue_create("client.queue", DISPATCH_QUEUE_CONCURRENT);
16 xpc_connection_t server_conn = xpc_connection_create(NULL, server_q);
20 xpc_connection_set_event_handler(server_conn, ^(xpc_object_t client) {
21 NSLog(@"global = %ld", global);
22 NSLog(@"server event handler, client = %@", client);
24 if (client == XPC_ERROR_CONNECTION_INTERRUPTED || client == XPC_ERROR_CONNECTION_INVALID) {
27 xpc_connection_set_event_handler(client, ^(xpc_object_t object) {
28 NSLog(@"received message: %@", object);
30 xpc_object_t reply = xpc_dictionary_create_reply(object);
33 xpc_dictionary_set_string(reply, "reply", "value");
35 xpc_connection_t remote = xpc_dictionary_get_remote_connection(object);
36 xpc_connection_send_message(remote, reply);
39 xpc_connection_resume(client);
41 xpc_connection_resume(server_conn);
42 xpc_endpoint_t endpoint = xpc_endpoint_create(server_conn);
44 xpc_connection_t client_conn = xpc_connection_create_from_endpoint(endpoint);
45 xpc_connection_set_event_handler(client_conn, ^(xpc_object_t event) {
46 NSLog(@"client event handler, event = %@", event);
49 xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
50 xpc_dictionary_set_string(msg, "hello", "world");
51 NSLog(@"sending message: %@", msg);
53 xpc_connection_send_message_with_reply(
54 client_conn, msg, client_q, ^(xpc_object_t object) {
55 NSLog(@"received reply: %@", object);
57 xpc_connection_cancel(client_conn);
58 xpc_connection_cancel(server_conn);
60 dispatch_sync(dispatch_get_main_queue(), ^{
61 CFRunLoopStop(CFRunLoopGetCurrent());
64 xpc_connection_resume(client_conn);
74 // CHECK-NOT: WARNING: ThreadSanitizer