@@ -14,52 +14,53 @@ module Concurrent.BoundedQueue
1414
1515import Prelude
1616
17- import Control.Monad.Aff (Aff )
18- import Control.Monad.Aff.AVar as AV
1917import Data.Array (unsafeIndex )
2018import Data.Maybe (Maybe (..))
2119import Data.Unfoldable (replicateA )
20+ import Effect.Aff (Aff )
21+ import Effect.Aff.AVar (AVar )
22+ import Effect.Aff.AVar as AVar
2223import Partial.Unsafe (unsafePartial )
2324
2425newtype BoundedQueue a =
2526 BoundedQueue
2627 { size ∷ Int
27- , contents ∷ Array (AV. AVar a )
28- , readPos ∷ AV. AVar Int
29- , writePos ∷ AV. AVar Int
28+ , contents ∷ Array (AVar a )
29+ , readPos ∷ AVar Int
30+ , writePos ∷ AVar Int
3031 }
3132
3233-- | Creates a new `BoundedQueue` with the given capacity,
33- new ∷ ∀ a eff . Int → Aff ( avar ∷ AV.AVAR | eff ) (BoundedQueue a )
34+ new ∷ ∀ a . Int → Aff (BoundedQueue a )
3435new size = do
35- contents ← replicateA size AV .makeEmptyVar
36- readPos ← AV .makeVar 0
37- writePos ← AV .makeVar 0
36+ contents ← replicateA size AVar .empty
37+ readPos ← AVar .new 0
38+ writePos ← AVar .new 0
3839 pure (BoundedQueue { size, contents, readPos, writePos })
3940
4041-- | Writes an element to the given queue. Will block if the queue is full until
4142-- | someone reads from it.
42- write ∷ ∀ a eff . BoundedQueue a → a → Aff ( avar ∷ AV.AVAR | eff ) Unit
43+ write ∷ ∀ a . BoundedQueue a → a → Aff Unit
4344write (BoundedQueue q) a = do
44- w ← AV .takeVar q.writePos
45- AV .putVar a (unsafePartial unsafeIndex q.contents w)
46- AV .putVar ((w + 1 ) `mod` q.size) q.writePos
45+ w ← AVar .take q.writePos
46+ AVar .put a (unsafePartial unsafeIndex q.contents w)
47+ AVar .put ((w + 1 ) `mod` q.size) q.writePos
4748
4849-- | Reads an element from the given queue, will block if the queue is empty,
4950-- | until someone writes to it.
50- read ∷ ∀ a eff . BoundedQueue a → Aff ( avar ∷ AV.AVAR | eff ) a
51+ read ∷ ∀ a . BoundedQueue a → Aff a
5152read (BoundedQueue q) = do
52- r ← AV .takeVar q.readPos
53- v ← AV .takeVar (unsafePartial unsafeIndex q.contents r)
54- AV .putVar ((r + 1 ) `mod` q.size) q.readPos
53+ r ← AVar .take q.readPos
54+ v ← AVar .take (unsafePartial unsafeIndex q.contents r)
55+ AVar .put ((r + 1 ) `mod` q.size) q.readPos
5556 pure v
5657
5758-- | Checks whether the given queue is empty. Never blocks.
58- isEmpty ∷ ∀ a eff . BoundedQueue a → Aff ( avar ∷ AV.AVAR | eff ) Boolean
59+ isEmpty ∷ ∀ a . BoundedQueue a → Aff Boolean
5960isEmpty (BoundedQueue q) = do
60- AV .tryReadVar q.readPos >>= case _ of
61+ AVar .tryRead q.readPos >>= case _ of
6162 Nothing → pure true
62- Just r → AV .tryReadVar (unsafePartial unsafeIndex q.contents r) <#> case _ of
63+ Just r → AVar .tryRead (unsafePartial unsafeIndex q.contents r) <#> case _ of
6364 Nothing → true
6465 Just _ → false
6566
@@ -68,29 +69,29 @@ isEmpty (BoundedQueue q) = do
6869-- |
6970-- | *Careful!* If other readers are blocked on the queue `tryRead` will also
7071-- | block.
71- tryRead ∷ ∀ a eff . BoundedQueue a → Aff ( avar ∷ AV.AVAR | eff ) (Maybe a )
72+ tryRead ∷ ∀ a . BoundedQueue a → Aff (Maybe a )
7273tryRead (BoundedQueue q) = do
73- r ← AV .takeVar q.readPos
74- AV .tryTakeVar (unsafePartial unsafeIndex q.contents r) >>= case _ of
74+ r ← AVar .take q.readPos
75+ AVar .tryTake (unsafePartial unsafeIndex q.contents r) >>= case _ of
7576 Just v → do
76- AV .putVar ((r + 1 ) `mod` q.size) q.readPos
77+ AVar .put ((r + 1 ) `mod` q.size) q.readPos
7778 pure (Just v)
7879 Nothing → do
79- AV .putVar r q.readPos
80+ AVar .put r q.readPos
8081 pure Nothing
8182
8283-- | Attempts to write an element into the given queue. If the queue is full,
8384-- | returns `false` otherwise `true`.
8485-- |
8586-- | *Careful!* If other writers are blocked on the queue `tryWrite` will also
8687-- | block.
87- tryWrite ∷ ∀ a eff . BoundedQueue a → a → Aff ( avar ∷ AV.AVAR | eff ) Boolean
88+ tryWrite ∷ ∀ a . BoundedQueue a → a → Aff Boolean
8889tryWrite (BoundedQueue q) a = do
89- w ← AV .takeVar q.writePos
90- AV .tryPutVar a (unsafePartial unsafeIndex q.contents w) >>= if _
90+ w ← AVar .take q.writePos
91+ AVar .tryPut a (unsafePartial unsafeIndex q.contents w) >>= if _
9192 then do
92- AV .putVar ((w + 1 ) `mod` q.size) q.writePos
93+ AVar .put ((w + 1 ) `mod` q.size) q.writePos
9394 pure true
9495 else do
95- AV .putVar w q.writePos
96+ AVar .put w q.writePos
9697 pure false
0 commit comments