Skip to content
Snippets Groups Projects
Commit d93606fa authored by Régis Blanc's avatar Régis Blanc
Browse files

using Random to model threads

parent a102909c
No related branches found
No related tags found
No related merge requests found
...@@ -29,4 +29,3 @@ object Random { ...@@ -29,4 +29,3 @@ object Random {
} ensuring(res => res >= 0 && res < max) } ensuring(res => res >= 0 && res < max)
} }
import leon.lang._
import leon.lang.xlang._
import leon.util.Random
import leon.collection._
object DataRacing {
case class SharedState(var i: Int)
case class AtomicInstr(instr: (SharedState) => Unit)
implicit def toInstr(instr: (SharedState) => Unit): AtomicInstr = AtomicInstr(instr)
def execute(t1: List[AtomicInstr], t2: List[AtomicInstr], state: SharedState): Unit = (t1, t2) match {
case (x::xs, y::ys) =>
if(Random.nextBoolean) {
x.instr(state)
execute(xs, y::ys, state)
} else {
y.instr(state)
execute(x::xs, ys, state)
}
case (Nil(), y::ys) =>
y.instr(state)
execute(Nil(), ys, state)
case (x::xs, Nil()) =>
x.instr(state)
execute(xs, Nil(), state)
case (Nil(), Nil()) => ()
}
def main(): Unit = {
val state = SharedState(0)
val t1 = List[AtomicInstr]((s: SharedState) => s.i = s.i + 1)
val t2 = List[AtomicInstr]((s: SharedState) => s.i = s.i * 2)
execute(t1, t2, state)
assert(state.i == 2)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment