summaryrefslogtreecommitdiffstats
path: root/tests/new/CFlowThreads.java
blob: 3f6220e0aa0f88cd8339ef0cf0322613e2bec6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class CFlowThreads implements Runnable {
    private static final int MAX_VALUE = 100;

    public static void main(String[] argv) {
        // Create a lot of threads
        int x = 0;
        while (x++ < 100) {
            new Thread(new CFlowThreads()).start();
            try {
                Thread.sleep((long)(Math.random()*100));
            } catch (Exception e) {}
        }
    }

    public boolean gotOne(int x) {
//        System.out.println("Succeeded, x = "+x);
        return true;
    }

    public boolean methodA(int x) {
        if (x % 2 == 0) return methodB(x+3);
        else return methodC(x/2);
    }

    public boolean methodB(int x) {
        if (x % 3 == 0) return methodC(x+4);
        else return methodA(x/3);
    }

    public boolean methodC(int x) {
        if (x > 2) return methodB(x-3);
        else return gotOne(x);
    }

    public boolean startIt(int x) {
        return methodA(x);
    }

    public void run() {
        startIt((int)(Math.random()*MAX_VALUE));
    }

    static aspect OriginalValue of eachcflow(OriginalValue.entrypoints(int)) {
        int recursioncount = 0;
        int original;

        pointcut entrypoints(int x):
            instanceof(CFlowThreads) && receptions(boolean startIt(x));

        before(int x): entrypoints(x) {
            original = x;
        }

        pointcut successes(int x):
            instanceof(CFlowThreads) && receptions(boolean gotOne(x));

        pointcut recursiveCalls(int x):
            instanceof(CFlowThreads) &&
            receptions(boolean *(x)) && !receptions(boolean gotOne(x));

        after(int x) returning (boolean s): successes(x) {
//            System.out.println("Started at "+original+", used "+recursioncount+" hops");
        }

        boolean fail(int x) {
//            System.out.println("Never gonna make it from "+original);
            return false;
        }

        around(int x) returns boolean: recursiveCalls(x) {
            if (recursioncount++ > 50) {
                return fail(x);
            } else {
                return(proceed(x));
            }
        }
    }
    static aspect CheckValues {
        static int[] hops = new int[MAX_VALUE];
        static after(int x, OriginalValue a) returning (boolean s):
            OriginalValue.successes(x) && hasaspect(a) {
            synchronized (hops) {
                if (hops[a.original] != 0) {
                    if (hops[a.original] != a.recursioncount) {
                        System.out.println("Error: "+a.original+" takes "+
                            hops[a.original]+", not "+
                            a.recursioncount+" hops.");
                    }
                } else {
                    hops[a.original] = a.recursioncount;
                }
            }
        }
        static after(int x, OriginalValue a) returning (boolean s):
            instanceof(a) && receptions(boolean fail(x)) {
            synchronized (hops) {
                if (hops[a.original] != 0 &&&& hops[a.original] != -1) {
                    System.out.println("Error: "+a.original+" takes "+
                        hops[a.original]+" hops, doesn't fail");
                } else {
                    hops[a.original] = -1;
                }
            }
        }
    }
}