blob: ed24a710bc6b772b97f6e01a1ac05601a3fac0ec (
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
|
procedure LockingExample();
implementation LockingExample()
{
var x: int;
var y: int;
var held: int;
start:
held := 0;
x := 0;
goto LoopHead;
LoopHead:
// Lock
assert held == 0;
held := 1;
y := x;
goto UnlockNow, LoopEnd;
UnlockNow:
// Unlock
assert held == 1;
held := 0;
x := x + 1;
goto LoopEnd;
LoopEnd:
goto ContinueIteration, EndIteration;
ContinueIteration:
assume x != y;
goto LoopHead;
EndIteration:
assume x == y;
goto AfterLoop;
AfterLoop:
// Unlock
assert held == 1;
held := 0;
return;
}
procedure StructuredLockingExample()
{
var x: int;
var y: int;
var held: bool;
held := false;
x := 0;
while (true)
invariant !held;
{
// Lock
assert !held;
held := true;
y := x;
if (*) {
// Unlock
assert held;
held := false;
x := x + 1;
}
if (x == y) { break; }
}
// Unlock
assert held;
held := false;
}
procedure StructuredLockingExampleWithCalls()
{
var x: int;
var y: int;
var mutex: Mutex;
call mutex := Initialize();
x := 0;
while (true)
invariant !IsHeld(mutex);
{
call mutex := Acquire(mutex);
y := x;
if (*) {
call mutex := Release(mutex);
x := x + 1;
}
if (x == y) { break; }
}
call mutex := Release(mutex);
}
type Mutex;
function IsHeld(Mutex) returns (bool);
procedure Initialize() returns (post: Mutex);
ensures !IsHeld(post);
procedure Acquire(pre: Mutex) returns (post: Mutex);
requires !IsHeld(pre);
ensures IsHeld(post);
procedure Release(pre: Mutex) returns (post: Mutex);
requires IsHeld(pre);
ensures !IsHeld(post);
|