brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.3 KiB · a6f9c1f Raw
57 lines · c
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-exception5 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/637181114 15const char *whoami = "nobody";16 17void 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 scopes23    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 46int 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