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