1 // RUN: %clangxx_tsan %s -o %t -framework Foundation
2 // RUN: %deflake %run %t 2>&1 | FileCheck %s
6 #import <Foundation/Foundation.h>
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[]) {
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) {
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);
42 barrier_wait(&barrier);
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'
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());
67 xpc_connection_resume(client);
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);
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]);
88 fprintf(stderr, "Done.\n");