-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some more atomic-branch examples.
- Loading branch information
1 parent
0c923ad
commit 8be8082
Showing
5 changed files
with
148 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Herlihy/Shavit-style compare and swap lock, | ||
* with an intentionally broken compare and swap. | ||
*/ | ||
|
||
shared bool lock; // True iff the lock is taken. | ||
thread bool test; // Used when trying to take the lock. | ||
|
||
/* | ||
* Locks the CAS lock. | ||
*/ | ||
method lock() { | ||
{| emp |} | ||
do { | ||
{| emp |} | ||
test = false; | ||
{| if test == false then emp else False() |} | ||
<| if (lock == test) { lock = true; } /* test = lock; */ |> | ||
{| if test == false then holdLock() else emp |} | ||
} while (test == true); | ||
{| holdLock() |} | ||
} | ||
|
||
/* | ||
* Unlocks the CAS lock. | ||
*/ | ||
method unlock() { | ||
{| holdLock() |} | ||
<| lock = false; |> | ||
{| emp |} | ||
} | ||
|
||
|
||
// False is a hack to implement local variable reasoning. | ||
view False(); | ||
constraint False() -> false; | ||
|
||
view holdLock(); | ||
constraint holdLock() -> lock == true; // Identity of lock. | ||
constraint holdLock() * holdLock() -> false; // Mutual exclusion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Allocatable version of Examples/Pass/spinLock.cvf. | ||
|
||
typedef int Lock; | ||
|
||
pragma grasshopper_include {./grasshopper/spinlock-module.spl}; | ||
pragma grasshopper_footprint {LockFoot}; | ||
pragma grasshopper_footprint_sort {Set<Lock>}; | ||
|
||
thread Lock x, ret; | ||
thread bool test; // Used when trying to take the lock. | ||
|
||
view newLock(Lock x), holdLock(Lock x), isLock(Lock x); | ||
|
||
method newLock() { | ||
{| emp |} | ||
<| ret = %{new Lock}; |> | ||
{| newLock(ret) |} | ||
<| %{[|ret|].lock := false}; |> | ||
{| isLock(ret) |} | ||
} | ||
|
||
method lock(Lock x) { | ||
{| isLock(x) |} | ||
do { | ||
{| isLock(x) |} | ||
test = false; | ||
{| if test == false then isLock(x) else False() |} | ||
<| test = %{ [|x|].lock }; | ||
if (%{ [|x|].lock } == false) { | ||
%{ [|x|].lock := true }; | ||
} |> | ||
test = !test; | ||
{| if test == false then isLock(x) else holdLock(x) |} | ||
} while (test == false); | ||
{| holdLock(x) |} | ||
} | ||
|
||
method unlock(Lock x) { | ||
{| holdLock(x) |} | ||
<| %{[|x|].lock := false}; |> | ||
{| isLock(x) |} | ||
} | ||
|
||
method split(Lock x) { | ||
{| isLock(x) |} | ||
; | ||
{| isLock(x) * isLock(x) |} | ||
} | ||
|
||
// False is a hack to implement local variable reasoning. | ||
view False(); | ||
constraint False() -> false; | ||
|
||
constraint holdLock(x) -> %{ [|x|] in LockFoot && [|x|].lock }; | ||
constraint isLock(x) -> %{ [|x|] in LockFoot }; | ||
constraint newLock(x) -> %{ [|x|] in LockFoot }; | ||
|
||
constraint isLock(x) * newLock(y) -> x != y; | ||
constraint holdLock(x) * newLock(y) -> x != y; | ||
constraint newLock(x) * newLock(y) -> x != y; | ||
|
||
constraint holdLock(x) * holdLock(y) -> x != y; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters