git.iko.soy/rowtype-yoga/purescript-yoga-om-workerbees
on main help sign in
Add makePoolWithData variant and improve SharedMap/SharedState
7bfab93Mark
src/Yoga/Om/WorkerBees.purs+17−8
@@ -3,6 +3,7 @@ module Yoga.Om.WorkerBees
3 , PoolConfig
3 , PoolConfig
4 , defaultPoolConfig
4 , defaultPoolConfig
5 , makePool
5 , makePool
6+ , makePoolWithData
6 , terminatePool
7 , terminatePool
7 , distributeWork
8 , distributeWork
8 , module Exports
9 , module Exports
@@ -41,20 +42,28 @@ defaultPoolConfig workerPath =
41type WorkerPool input output = Pool.WorkerPool input output
42type WorkerPool input output = Pool.WorkerPool input output
42
43
43-- | Create a worker pool. Must be paired with `terminatePool`.
44-- | Create a worker pool. Must be paired with `terminatePool`.
44−-- | Workers receive the current Om context via `ctx.workerData`.
45−-- | If you don't need shared data, use `Unit` as the context type.
46makePool
45makePool
47 :: forall ctx errs input output
46 :: forall ctx errs input output
48 . WB.Sendable input
47 . WB.Sendable input
49 => WB.Sendable output
48 => WB.Sendable output
50− => WB.Sendable ctx
51 => PoolConfig
49 => PoolConfig
52 -> Om ctx errs (WorkerPool input output)
50 -> Om ctx errs (WorkerPool input output)
53−makePool config = do
51+makePool config = Om.fromAff do
54− ctx <- Om.ask
52+ let worker = WB.unsafeWorkerFromPath config.workerPath
53+ Pool.make worker unit config.numWorkers
54+
55+-- | Create a worker pool with shared data accessible to all workers via `workerData`.
56+makePoolWithData
57+ :: forall ctx errs input output a
58+ . WB.Sendable input
59+ => WB.Sendable output
60+ => WB.Sendable a
61+ => PoolConfig
62+ -> a
63+ -> Om ctx errs (WorkerPool input output)
55− Om.fromAff do
64+makePoolWithData config workerData = Om.fromAff do
56− let worker = WB.unsafeWorkerFromPath config.workerPath
65+ let worker = WB.unsafeWorkerFromPath config.workerPath
57− Pool.make worker ctx config.numWorkers
66+ Pool.make worker workerData config.numWorkers
58
67
59-- | Terminate a worker pool and all its threads.
68-- | Terminate a worker pool and all its threads.
60terminatePool
69terminatePool
src/Yoga/Om/WorkerBees/SharedMap.purs−2
@@ -182,8 +182,6 @@ writeSegment (SharedMap { sab, maxBytesPerSegment }) { lenSlot, dataOffset } obj
182 _ <- runEffectFn3 writeSegmentDataImpl sab dataOffset json
182 _ <- runEffectFn3 writeSegmentDataImpl sab dataOffset json
183 void $ Atomics.store lenSlot 0 len
183 void $ Atomics.store lenSlot 0 len
184
184
185−-- FFI
186−
187foreign import hashKeyImpl :: Fn2 String Int Int
185foreign import hashKeyImpl :: Fn2 String Int Int
188foreign import writeSegmentDataImpl :: EffectFn3 SharedArrayBuffer Int String Int
186foreign import writeSegmentDataImpl :: EffectFn3 SharedArrayBuffer Int String Int
189foreign import readSegmentDataImpl :: EffectFn3 SharedArrayBuffer Int Int String
187foreign import readSegmentDataImpl :: EffectFn3 SharedArrayBuffer Int Int String
src/Yoga/Om/WorkerBees/SharedState.purs−2
@@ -118,7 +118,5 @@ readData (SharedState { sab, lenSlot }) = do
118 Just val -> pure val
118 Just val -> pure val
119 Nothing -> throw "SharedState: failed to deserialize"
119 Nothing -> throw "SharedState: failed to deserialize"
120
120
121−-- FFI
122−
123foreign import writeDataImpl :: EffectFn2 SharedArrayBuffer String Int
121foreign import writeDataImpl :: EffectFn2 SharedArrayBuffer String Int
124foreign import readDataImpl :: EffectFn2 SharedArrayBuffer Int String
122foreign import readDataImpl :: EffectFn2 SharedArrayBuffer Int String