Skip to content

Commit

Permalink
move forkWithAffinity
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Nov 28, 2023
1 parent bb0115d commit 59312dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ki/src/Ki/Internal/Scope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ import Ki.Internal.IO
)
import Ki.Internal.NonblockingSTM
import Ki.Internal.Propagating (Tid, peelOffPropagating, propagate, pattern PropagatingFrom)
import Ki.Internal.Thread (Thread, forkWithAffinity, makeThread)
import Ki.Internal.Thread (Thread, makeThread)
import Ki.Internal.ThreadAffinity (forkWithAffinity)
import Ki.Internal.ThreadOptions (ThreadOptions (..), defaultThreadOptions)

-- | A scope.
Expand Down
13 changes: 2 additions & 11 deletions ki/src/Ki/Internal/Thread.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ module Ki.Internal.Thread
( Thread,
makeThread,
await,
forkWithAffinity,
)
where

import Control.Concurrent (ThreadId, forkOS)
import Control.Concurrent (ThreadId)
import Control.Exception (BlockedIndefinitelyOnSTM (..))
import GHC.Conc (STM)
import Ki.Internal.IO (forkIO, forkOn, tryEitherSTM)
import Ki.Internal.ThreadAffinity (ThreadAffinity (..))
import Ki.Internal.IO (tryEitherSTM)

-- | A thread.
--
Expand Down Expand Up @@ -48,13 +46,6 @@ makeThread threadId action =
await_ = tryEitherSTM (\BlockedIndefinitelyOnSTM -> action) pure action
}

-- forkIO/forkOn/forkOS, switching on affinity
forkWithAffinity :: ThreadAffinity -> IO () -> IO ThreadId
forkWithAffinity = \case
Unbound -> forkIO
Capability n -> forkOn n
OsThread -> Control.Concurrent.forkOS

-- | Wait for a thread to terminate.
await :: Thread a -> STM a
await =
Expand Down
11 changes: 11 additions & 0 deletions ki/src/Ki/Internal/ThreadAffinity.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module Ki.Internal.ThreadAffinity
( ThreadAffinity (..),
forkWithAffinity,
)
where

import Control.Concurrent (ThreadId, forkOS)
import Ki.Internal.IO (forkIO, forkOn)

-- | What, if anything, a thread is bound to.
data ThreadAffinity
= -- | Unbound.
Expand All @@ -12,3 +16,10 @@ data ThreadAffinity
| -- | Bound to an OS thread.
OsThread
deriving stock (Eq, Show)

-- forkIO/forkOn/forkOS, switching on affinity
forkWithAffinity :: ThreadAffinity -> IO () -> IO ThreadId
forkWithAffinity = \case
Unbound -> forkIO
Capability n -> forkOn n
OsThread -> forkOS

0 comments on commit 59312dc

Please sign in to comment.