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