Skip to content

Commit

Permalink
BlockingQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
lemmy committed Oct 3, 2023
1 parent 59f691f commit bf03fe8
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
23 changes: 23 additions & 0 deletions BlockingQueue.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SPECIFICATION BQSpec

INVARIANT
TypeOK

PROPERTIES
DeadlockFree
\* Foo
StarvationFreedom

CONSTANT
bufCap = 4

p1 = p1
p2 = p2
p3 = p3
p4 = p4
P = {p1,p2,p3,p4}

c1 = c1
c2 = c2
c3 = c3
C = {c1,c2,c3}
80 changes: 80 additions & 0 deletions BlockingQueue.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
----- MODULE BlockingQueue -----
EXTENDS Naturals, FiniteSets

CONSTANT bufCap, P, C

ASSUME /\ bufCap \in Nat
/\ IsFiniteSet(P)
/\ P # {}
/\ IsFiniteSet(C)
/\ C # {}

VARIABLE buf, waitSet
vars == <<buf, waitSet>>

TypeOK ==
/\ buf \in 0..bufCap
/\ waitSet \in SUBSET (P \union C)

Init ==
/\ buf = 0
/\ waitSet = {}

Notify ==
IF waitSet = {} THEN UNCHANGED waitSet
ELSE \E t \in waitSet: waitSet' = waitSet \ {t}

NotifyAll ==
waitSet' = {}

NotifyOther(T) ==
IF waitSet \cap T = {} THEN UNCHANGED waitSet
ELSE \E t \in waitSet \cap T : waitSet' = waitSet \ {t}

Wait(self) ==
waitSet' = waitSet \union {self}

Put(self) ==
\/ /\ bufCap > buf
/\ buf' = buf + 1
/\ Notify
\/ /\ bufCap = buf
/\ Wait(self)
/\ UNCHANGED buf

Get(self) ==
\/ /\ buf > 0
/\ buf' = buf - 1
/\ Notify
\/ /\ buf = 0
/\ Wait(self)
/\ UNCHANGED buf

Next ==
\E t \in (P \union C) \ waitSet:
\/ /\ t \in P
/\ Put(t)
\/ /\ t \in C
/\ Get(t)

BQSpec ==
Init /\ [][Next]_vars /\ WF_vars(Next)

DeadlockFree ==
[](waitSet # P \union C)

Foo ==
<>(buf = bufCap)

\* BQ => DeadlockFree
\* BQS => DeadlockFree
\* BQS => BQ

\* <>[]P
\* []<>P

StarvationFreedom ==
/\ \A p \in P: []<> <<Put(p)>>_<<buf,waitSet>>
/\ \A c \in C: []<> <<Get(c)>>_<<buf,waitSet>>

====
22 changes: 22 additions & 0 deletions BlockingQueueSplit.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
INIT Init
NEXT Next

INVARIANT
TypeOK

PROPERTIES
BQSpec

CONSTANT
bufCap = 1

p1 = p1
p2 = p2
p3 = p3
p4 = p4
P = {p1}

c1 = c1
c2 = c2
c3 = c3
C = {c1,c2}
70 changes: 70 additions & 0 deletions BlockingQueueSplit.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
----- MODULE BlockingQueueSplit ------
EXTENDS Naturals, FiniteSets

CONSTANT bufCap, P, C

ASSUME /\ bufCap \in Nat
/\ IsFiniteSet(P)
/\ P # {}
/\ IsFiniteSet(C)
/\ C # {}

VARIABLE buf, waitSetC, waitSetP
vars ==<<buf, waitSetC, waitSetP>>

TypeOK ==
/\ buf \in 0..bufCap
/\ waitSetC \in SUBSET (C)
/\ waitSetP \in SUBSET (P)

Init ==
/\ buf = 0
/\ waitSetC = {}
/\ waitSetP = {}

Notify(ws) ==
IF ws = {} THEN UNCHANGED ws
ELSE \E t \in ws: ws' = ws \ {t}

Wait(ws, self) ==
ws' = ws \union {self}

Put(self) ==
\/ /\ bufCap > buf
/\ buf' = buf + 1
/\ Notify(waitSetC)
/\ UNCHANGED waitSetP
\/ /\ bufCap = buf
/\ Wait(waitSetP, self)
/\ UNCHANGED <<buf, waitSetC>>

Get(self) ==
\/ /\ buf > 0
/\ buf' = buf - 1
/\ Notify(waitSetP)
/\ UNCHANGED waitSetC
\/ /\ buf = 0
/\ Wait(waitSetC, self)
/\ UNCHANGED <<buf, waitSetP>>

Next ==
\E t \in (P \union C) \ (waitSetC \union waitSetP):
\/ /\ t \in P
/\ Put(t)
\/ /\ t \in C
/\ Get(t)

BQSSpec ==
Init /\ [][Next]_vars

\* BQS => BQ

BQ ==
INSTANCE BlockingQueue WITH waitSet <- waitSetP \union waitSetC

BQSpec ==
BQ!BQSpec

THEOREM Implements == BQSSpec => BQSpec
THEOREM Types == BQSSpec => TypeOK
=====

0 comments on commit bf03fe8

Please sign in to comment.