summaryrefslogtreecommitdiff
path: root/Test/civl/ticket.bpl
blob: 7db23dca8cd4984e08e6b18e43b706ba9357686a (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// RUN: %boogie -noinfer -typeEncoding:m -useArrayTheory "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
function RightOpen(n: int) : [int]bool;
function RightClosed(n: int) : [int]bool;
axiom (forall x: int, y: int :: RightOpen(x)[y] <==> y < x);
axiom (forall x: int, y: int :: RightClosed(x)[y] <==> y <= x);

type X;
const nil: X;
var {:layer 0,1} t: int;
var {:layer 0,2} s: int;
var {:layer 0,2} cs: X;
var {:layer 0,2} T: [int]bool;

function {:builtin "MapConst"} MapConstBool(bool) : [X]bool;
function {:inline} {:linear "tid"} TidCollector(x: X) : [X]bool
{
  MapConstBool(false)[x := true]
}
function {:inline} {:linear "tid"} TidSetCollector(x: [X]bool) : [X]bool
{
  x
}

function {:inline} Inv1(tickets: [int]bool, ticket: int): (bool)
{
    tickets == RightOpen(ticket)
}

function {:inline} Inv2(tickets: [int]bool, ticket: int, lock: X): (bool)
{
    if (lock == nil) then tickets == RightOpen(ticket) else tickets == RightClosed(ticket)
}

procedure {:yields} {:layer 2} Allocate({:linear_in "tid"} xls':[X]bool) returns ({:linear "tid"} xls: [X]bool, {:linear "tid"} xl: X)
ensures {:layer 1} {:layer 2} xl != nil;
{
    yield;
    call xls, xl := AllocateLow(xls');
    yield;
}

procedure {:yields} {:layer 2} main({:linear_in "tid"} xls':[X]bool)
requires {:layer 1} Inv1(T, t);
requires {:layer 2} xls' == MapConstBool(true) && Inv2(T, s, cs);
{
    var {:linear "tid"} tid: X;
    var {:linear "tid"} xls: [X]bool;

    par Yield1() | Yield2();

    call InitAbstract(xls');
    xls := xls';

    par Yield1() | Yield2();

    while (*)
    invariant {:layer 1} Inv1(T, t);
    invariant {:layer 2} Inv2(T, s, cs);
    {
        par xls, tid := Allocate(xls) | Yield1() | Yield2();
        async call Customer(tid);
    	par Yield1() | Yield2();
    }
    par Yield1() | Yield2();
}

procedure {:yields} {:layer 2} Customer({:linear_in "tid"} tid: X)
requires {:layer 1} Inv1(T, t);
requires {:layer 2} tid != nil && Inv2(T, s, cs);
{
    par Yield1() | Yield2();    
    while (*) 
    invariant {:layer 1} Inv1(T, t);
    invariant {:layer 2} Inv2(T, s, cs);
    {
        call Enter(tid);
        par Yield1() | Yield2() | YieldSpec(tid);
    	call Leave(tid);
        par Yield1() | Yield2();
    }
    par Yield1() | Yield2();
}

procedure {:yields} {:layer 2} Enter({:linear "tid"} tid: X)
requires {:layer 1} Inv1(T, t);
ensures {:layer 1} Inv1(T,t);
requires {:layer 2} tid != nil && Inv2(T, s, cs);
ensures {:layer 2} Inv2(T, s, cs) && cs == tid;
{
    var m: int;

    par Yield1() | Yield2();
    call m := GetTicketAbstract(tid);
    par Yield1();
    call WaitAndEnter(tid, m);
    par Yield1() | Yield2() | YieldSpec(tid);
}

procedure {:yields} {:layer 1,2} InitAbstract({:linear "tid"} xls:[X]bool)
requires {:layer 1} Inv1(T, t);
ensures {:layer 1} Inv1(T, t);
ensures {:atomic} |{ A: assert xls == MapConstBool(true); cs := nil; s := 0; T := RightOpen(0); return true; }|;
{
    par Yield1();
    call Init(xls);
    par Yield1();
}

procedure {:yields} {:layer 1,2} GetTicketAbstract({:linear "tid"} tid: X) returns (m: int)
requires {:layer 1} Inv1(T, t);
ensures {:layer 1} Inv1(T, t);
ensures {:right} |{ A: havoc m; assume !T[m]; T[m] := true; return true; }|;
{
    par Yield1();
    call m := GetTicket(tid);
    par Yield1();
}

procedure {:yields} {:layer 2} YieldSpec({:linear "tid"} tid: X)
requires {:layer 2} tid != nil && cs == tid;
ensures {:layer 2} cs == tid;
{
    yield;
    assert {:layer 2} tid != nil && cs == tid;
}

procedure {:yields} {:layer 2} Yield2()
requires {:layer 2} Inv2(T, s, cs);
ensures {:layer 2} Inv2(T, s, cs);
{
    yield;
    assert {:layer 2} Inv2(T, s, cs);
}

procedure {:yields} {:layer 1} Yield1()
requires {:layer 1} Inv1(T, t);
ensures {:layer 1} Inv1(T,t);
{
    yield;
    assert {:layer 1} Inv1(T,t);
}

procedure {:yields} {:layer 0,1} Init({:linear "tid"} xls:[X]bool);
ensures {:atomic} |{ A: assert xls == MapConstBool(true); cs := nil; t := 0; s := 0; T := RightOpen(0); return true; }|;

procedure {:yields} {:layer 0,1} GetTicket({:linear "tid"} tid: X) returns (m: int);
ensures {:atomic} |{ A: m := t; t := t + 1; T[m] := true; return true; }|;

procedure {:yields} {:layer 0,2} WaitAndEnter({:linear "tid"} tid: X, m:int);
ensures {:atomic} |{ A: assume m <= s; cs := tid; return true; }|;

procedure {:yields} {:layer 0,2} Leave({:linear "tid"} tid: X);
ensures {:atomic} |{ A: s := s + 1; cs := nil; return true; }|;

procedure {:yields} {:layer 0,2} AllocateLow({:linear_in "tid"} xls':[X]bool) returns ({:linear "tid"} xls: [X]bool, {:linear "tid"} xl: X);
ensures {:atomic} |{ A: assume xl != nil; return true; }|;