1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 
6 #include <CoreFoundation/CoreFoundation.h>
7 
8 #include <dispatch/dispatch.h>
9 #include <unistd.h>
10 //#import <Foundation/Foundation.h>
11 #include <Block.h>
12 
13 // CONFIG rdar://problem/6371811
14 
15 const char *whoami = "nobody";
16 
EnqueueStuff(dispatch_queue_t q)17 void EnqueueStuff(dispatch_queue_t q)
18 {
19     __block CFIndex counter;
20 
21     // above call has a side effect: it works around:
22     // <rdar://problem/6225809> __block variables not implicitly imported into intermediate scopes
23     dispatch_async(q, ^{
24         counter = 0;
25     });
26 
27 
28     dispatch_async(q, ^{
29         //printf("outer block.\n");
30         counter++;
31         dispatch_async(q, ^{
32             //printf("inner block.\n");
33             counter--;
34             if(counter == 0) {
35                 printf("%s: success\n", whoami);
36                 exit(0);
37             }
38         });
39         if(counter == 0) {
40             printf("already done? inconceivable!\n");
41             exit(1);
42         }
43     });
44 }
45 
main(int argc,const char * argv[])46 int main (int argc, const char * argv[]) {
47     dispatch_queue_t q = dispatch_queue_create("queue", NULL);
48 
49     whoami = argv[0];
50 
51     EnqueueStuff(q);
52 
53     dispatch_main();
54     printf("shouldn't get here\n");
55     return 1;
56 }
57