Skip to content

Commit

Permalink
depend on int-supply
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Nov 28, 2023
1 parent 5e38598 commit a5da3ba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 72 deletions.
20 changes: 12 additions & 8 deletions ki/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
## [1.0.1.1] - 2023-10-10
## [1.0.1.2] - Unreleased

- Refactor: depend on (rather than inline) `int-supply` package

## [1.0.1.1] - October 10, 2023

- Compat: support GHC 9.8.1

## [1.0.1.0] - 2023-04-03
## [1.0.1.0] - April 3, 2023

- Change [#25](https://github.com/awkward-squad/ki/pull/25): Attempting to fork a thread in a closing scope now acts as
if it were a child being terminated due to the scope closing. Previously, attempting to fork a thread in a closing
scope would throw a runtime exception like `error "ki: scope closed"`.
- Change [#27](https://github.com/awkward-squad/ki/pull/27): Calling `awaitAll` on a closed scope now returns `()`
instead of blocking forever.

## [1.0.0.2] - 2023-01-25
## [1.0.0.2] - January 25, 2023

- Bugfix [#20](https://github.com/awkward-squad/ki/pull/20): previously, a child thread could deadlock when attempting
to propagate an exception to its parent

## [1.0.0.1] - 2022-08-14
## [1.0.0.1] - August 14, 2022

- Compat: support GHC 9.4.1

## [1.0.0] - 2022-06-30
## [1.0.0] - June 30, 2022

- Breaking: Remove `Context` type, `Ki.Implicit` module, and the ability to soft-cancel a `Scope`.
- Breaking: Remove `Duration` type and its associated API, including `waitFor` and `awaitFor`.
Expand All @@ -40,17 +44,17 @@

- Performance: Use atomic fetch-and-add rather than a `TVar` to track internal child thread ids.

## [0.2.0] - 2020-12-17
## [0.2.0] - December 17, 2020

- Breaking: Remove `ThreadFailed` exception wrapper.
- Breaking: Rename `cancelScope` to `cancel`.

## [0.1.0.1] - 2020-11-30
## [0.1.0.1] - November 30, 2020

- Misc: Replace `AtomicCounter` with `Int` to drop the `atomic-primops` dependency.

- Bounds: Lower `cabal-version` from 3.0 to 2.2 because `stack` cannot parse 3.0.

## [0.1.0] - 2020-11-11
## [0.1.0] - November 11, 2020

- Initial release.
2 changes: 1 addition & 1 deletion ki/ki.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ library
import: component
build-depends:
containers ^>= 0.6 || ^>= 0.7,
int-supply ^>= 1.0.0,
exposed-modules:
Ki
hs-source-dirs: src
other-modules:
Ki.Internal.ByteCount
Ki.Internal.Counter
Ki.Internal.IO
Ki.Internal.Scope
Ki.Internal.Thread
Expand Down
56 changes: 0 additions & 56 deletions ki/src/Ki/Internal/Counter.hs

This file was deleted.

15 changes: 8 additions & 7 deletions ki/src/Ki/Internal/Scope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import GHC.Conc
import GHC.Conc.Sync (readTVarIO)
import GHC.IO (unsafeUnmask)
import Ki.Internal.ByteCount
import Ki.Internal.Counter
import IntSupply (IntSupply)
import qualified IntSupply
import Ki.Internal.IO
( IOResult (..),
UnexceptionalIO (..),
Expand Down Expand Up @@ -84,8 +85,8 @@ data Scope = Scope
childExceptionVar :: {-# UNPACK #-} !(MVar SomeException),
-- The set of child threads that are currently running, each keyed by a monotonically increasing int.
childrenVar :: {-# UNPACK #-} !(TVar (IntMap ThreadId)),
-- The counter that holds the (int) key to use for the next child thread.
nextChildIdCounter :: {-# UNPACK #-} !Counter,
-- The supply that holds the (int) key to use for the next child thread.
nextChildIdSupply :: {-# UNPACK #-} !IntSupply,
-- The id of the thread that created the scope, which is considered the parent of all threads created within it.
parentThreadId :: {-# UNPACK #-} !ThreadId,
statusVar :: {-# UNPACK #-} !(TVar ScopeStatus)
Expand Down Expand Up @@ -210,16 +211,16 @@ allocateScope :: IO Scope
allocateScope = do
childExceptionVar <- newEmptyMVar
childrenVar <- newTVarIO IntMap.Lazy.empty
nextChildIdCounter <- newCounter
nextChildIdSupply <- IntSupply.new
parentThreadId <- myThreadId
statusVar <- newTVarIO 0
pure Scope {childExceptionVar, childrenVar, nextChildIdCounter, parentThreadId, statusVar}
pure Scope {childExceptionVar, childrenVar, nextChildIdSupply, parentThreadId, statusVar}

-- Spawn a thread in a scope, providing it its child id and a function that sets the masking state to the requested
-- masking state. The given action is called with async exceptions interruptibly masked.
spawn :: Scope -> ThreadOptions -> (Tid -> (forall x. IO x -> IO x) -> UnexceptionalIO ()) -> IO ThreadId
spawn
Scope {childrenVar, nextChildIdCounter, statusVar}
Scope {childrenVar, nextChildIdSupply, statusVar}
ThreadOptions {affinity, allocationLimit, label, maskingState = requestedChildMaskingState}
action = do
-- Interruptible mask is enough so long as none of the STM operations below block.
Expand All @@ -236,7 +237,7 @@ spawn
Closing -> throwSTM ScopeClosing
Closed -> throwSTM (ErrorCall "ki: scope closed")

childId <- incrCounter nextChildIdCounter
childId <- IntSupply.next nextChildIdSupply

childThreadId <-
forkWithAffinity affinity do
Expand Down

0 comments on commit a5da3ba

Please sign in to comment.