This PR migrates usages of `Std.Range` to the new polymorphic ranges. This PR unfortunately increases the transitive imports for frequently-used parts of `Init` because the ranges now rely on iterators in order to provide their functionality for types other than `Nat`. However, iteration over ranges in compiled code is as efficient as before in the examples I checked. This is because of a special `IteratorLoop` implementation provided in the PR for this purpose. There were two issues that were uncovered during migration: * In `IndPredBelow.lean`, migrating the last remaining range causes `compilerTest1.lean` to break. I have minimized the issue and came to the conclusion it's a compiler bug. Therefore, I have not replaced said old range usage yet (see #9186). * In `BRecOn.lean`, we are publicly importing the ranges. Making this import private should theoretically work, but there seems to be a problem with the module system, causing the build to panic later in `Init.Data.Grind.Poly` (see #9185). * In `FuzzyMatching.lean`, inlining fails with the new ranges, which would have led to significant slowdown. Therefore, I have not migrated this file either.
70 lines
2.1 KiB
Text
70 lines
2.1 KiB
Text
import Std.Sync.RecursiveMutex
|
|
|
|
def countIt (mutex : Std.RecursiveMutex Nat) : IO Unit := do
|
|
for _ in *...(1000 : Nat) do
|
|
mutex.atomically do
|
|
modify fun s => s + 1
|
|
|
|
def atomically : IO Unit := do
|
|
let mutex ← Std.RecursiveMutex.new 0
|
|
let t1 ← IO.asTask (prio := .dedicated) (countIt mutex)
|
|
let t2 ← IO.asTask (prio := .dedicated) (countIt mutex)
|
|
let t3 ← IO.asTask (prio := .dedicated) (countIt mutex)
|
|
let t4 ← IO.asTask (prio := .dedicated) (countIt mutex)
|
|
IO.ofExcept t1.get
|
|
IO.ofExcept t2.get
|
|
IO.ofExcept t3.get
|
|
IO.ofExcept t4.get
|
|
mutex.atomically do
|
|
let val ← get
|
|
if val != 4000 then
|
|
throw <| .userError s!"Should be 4000 but was {val}"
|
|
|
|
def holdIt (mutex : Std.RecursiveMutex Nat) (ref : IO.Ref Nat) : IO Unit := do
|
|
mutex.atomically do
|
|
ref.set 1
|
|
modify fun s => s + 1
|
|
while (← ref.get) == 1 do
|
|
IO.sleep 1
|
|
|
|
def tryIt (mutex : Std.RecursiveMutex Nat) (ref : IO.Ref Nat) : IO Unit := do
|
|
while (← ref.get) == 0 do
|
|
IO.sleep 1
|
|
let success ← mutex.tryAtomically (modify fun s => s + 1)
|
|
if success.isSome then throw <| .userError s!"lock succeeded but shouldn't"
|
|
ref.set 2
|
|
mutex.atomically (modify fun s => s + 1)
|
|
let success ← mutex.tryAtomically (modify fun s => s + 1)
|
|
if success.isNone then throw <| .userError s!"lock didn't succeed but should"
|
|
|
|
|
|
def tryAtomically : IO Unit := do
|
|
let mutex ← Std.RecursiveMutex.new 0
|
|
let ref ← IO.mkRef 0
|
|
let t1 ← IO.asTask (prio := .dedicated) (holdIt mutex ref)
|
|
let t2 ← IO.asTask (prio := .dedicated) (tryIt mutex ref)
|
|
IO.ofExcept t1.get
|
|
IO.ofExcept t2.get
|
|
mutex.atomically do
|
|
let val ← get
|
|
if val != 3 then
|
|
throw <| .userError s!"Should be 3 but was {val}"
|
|
|
|
def recursive : IO Unit := do
|
|
let mutex ← Std.RecursiveMutex.new 0
|
|
mutex.atomically do
|
|
mutex.atomically do
|
|
modify fun s => s + 1
|
|
|
|
mutex.atomically do
|
|
discard <| mutex.tryAtomically do
|
|
modify fun s => s + 1
|
|
|
|
mutex.atomically do
|
|
let val ← get
|
|
if val != 2 then
|
|
throw <| .userError s!"Should be 2 but was {val}"
|
|
|
|
#eval atomically
|
|
#eval tryAtomically
|
|
#eval recursive
|