-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ new ] atomic modification of arrays using CAS loops (#51)
* [ new ] atomic modification of arrays using CAS loops * [ test ] preparing CAS tests * [ test ] single threaded CAS updates * [ test ] CAS updates under contention
- Loading branch information
1 parent
8a5346c
commit 3f14963
Showing
6 changed files
with
232 additions
and
10 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
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
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,50 @@ | ||
module Concurrent | ||
|
||
import Data.Vect as V | ||
import Data.Array | ||
import Data.Array.Mutable | ||
import System.Concurrency | ||
import System | ||
|
||
%default total | ||
|
||
public export | ||
ITER : Nat | ||
ITER = 1_000_000 | ||
|
||
data Prog = Unsafe | CAS | Mut | ||
|
||
inc : (r : IOArray 1 Nat) -> F1' [World] | ||
inc r = modify r 0 S | ||
|
||
casinc : (r : IOArray 1 Nat) -> F1' [World] | ||
casinc r = casmodify r 0 S | ||
|
||
mutinc : Mutex -> IOArray 1 Nat -> Nat -> IO () | ||
mutinc m r 0 = pure () | ||
mutinc m r (S k) = do | ||
mutexAcquire m | ||
runIO (inc r) | ||
mutexRelease m | ||
mutinc m r k | ||
|
||
prog : Prog -> Mutex -> IOArray 1 Nat -> IO () | ||
prog Unsafe m ref = runIO (forN ITER $ inc ref) | ||
prog CAS m ref = runIO (forN ITER $ casinc ref) | ||
prog Mut m ref = mutinc m ref ITER | ||
|
||
runProg : Prog -> Nat -> IO Nat | ||
runProg prg n = do | ||
mut <- makeMutex | ||
ref <- newIOArray 1 Z | ||
ts <- sequence $ V.replicate n (fork $ prog prg mut ref) | ||
traverse_ (\t => threadWait t) ts | ||
runIO (get ref 0) | ||
|
||
main : IO () | ||
main = do | ||
u <- runProg Unsafe 4 | ||
c <- runProg CAS 4 | ||
when (u >= c) (die "no race condition") | ||
when (c /= 4 * ITER) (die "CAS synchronization failed") | ||
putStrLn "Concurrent counter succeeded!" |
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,64 @@ | ||
module ConcurrentQueue | ||
|
||
import Data.Queue | ||
import Data.Vect as V | ||
import Data.Array | ||
import Data.Array.Mutable | ||
import System.Concurrency | ||
import System | ||
|
||
%default total | ||
|
||
record State where | ||
constructor ST | ||
cur : Nat | ||
queue : Queue Nat | ||
|
||
next : State -> State | ||
next (ST n q) = ST (S n) (enqueue q n) | ||
|
||
ITER : Nat | ||
ITER = 10_000 | ||
|
||
DELAY : Nat | ||
DELAY = 100_000 | ||
|
||
data Prog = Unsafe | CAS | Mut | ||
|
||
inc : (r : IOArray 1 State) -> Nat -> F1' [World] | ||
inc r 0 = modify r 0 next | ||
inc r (S k) = inc r k | ||
|
||
casinc : (r : IOArray 1 State) -> Nat -> F1' [World] | ||
casinc r 0 = casmodify r 0 next | ||
casinc r (S k) = casinc r k | ||
|
||
mutinc : Mutex -> IOArray 1 State -> Nat -> Nat -> IO () | ||
mutinc m r n (S k) = mutinc m r n k | ||
mutinc m r 0 0 = pure () | ||
mutinc m r (S k) 0 = do | ||
mutexAcquire m | ||
runIO (inc r 0) | ||
mutexRelease m | ||
mutinc m r k DELAY | ||
|
||
prog : Prog -> Mutex -> IOArray 1 State -> IO () | ||
prog Unsafe m ref = runIO (forN ITER $ inc ref DELAY) | ||
prog CAS m ref = runIO (forN ITER $ casinc ref DELAY) | ||
prog Mut m ref = mutinc m ref ITER DELAY | ||
|
||
runProg : Prog -> Nat -> IO (List Nat) | ||
runProg prg n = do | ||
mut <- makeMutex | ||
ref <- newIOArray 1 (ST 0 empty) | ||
ts <- sequence $ V.replicate n (fork $ prog prg mut ref) | ||
traverse_ (\t => threadWait t) ts | ||
toList . queue <$> runIO (get ref 0) | ||
|
||
main : IO () | ||
main = do | ||
us <- runProg Unsafe 4 | ||
cs <- runProg CAS 4 | ||
when (length us >= length cs) (die "no race condition") | ||
when (cs /= [0 .. pred (4 * ITER)]) (die "CAS synchronization failed") | ||
putStrLn "Concurrent queue succeeded!" |
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 |
---|---|---|
|
@@ -11,4 +11,5 @@ executable = "array-test" | |
sourcedir = "src" | ||
|
||
depends = array | ||
, containers | ||
, hedgehog |