Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
quasar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jens Nolte
quasar
Commits
be85193c
Commit
be85193c
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Implement a helper to abort a computation when disposing
parent
951fe87f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Quasar/ResourceManager.hs
+35
-5
35 additions, 5 deletions
src/Quasar/ResourceManager.hs
with
35 additions
and
5 deletions
src/Quasar/ResourceManager.hs
+
35
−
5
View file @
be85193c
...
@@ -24,7 +24,9 @@ module Quasar.ResourceManager (
...
@@ -24,7 +24,9 @@ module Quasar.ResourceManager (
CombinedException
,
CombinedException
,
withRootResourceManager
,
withRootResourceManager
,
CancelLinkedThread
,
-- ** Linking computations to a resource manager
linkExecution
,
CancelLinkedExecution
,
-- ** Resource manager implementations
-- ** Resource manager implementations
newUnmanagedRootResourceManager
,
newUnmanagedRootResourceManager
,
...
@@ -184,15 +186,43 @@ captureTask action = do
...
@@ -184,15 +186,43 @@ captureTask action = do
-- | A computation bound to a resource manager with 'linkThread' should be canceled.
-- | A computation bound to a resource manager with 'linkThread' should be canceled.
data
CancelLinked
Thread
=
CancelLinked
Thread
Unique
data
CancelLinked
Execution
=
CancelLinked
Execution
Unique
deriving
anyclass
Exception
deriving
anyclass
Exception
instance
Show
CancelLinked
Thread
where
instance
Show
CancelLinked
Execution
where
show
_
=
"CancelLinked
Thread
"
show
_
=
"CancelLinked
Execution
"
data
LinkState
=
LinkStateLinked
ThreadId
|
LinkStateThrowing
|
LinkStateCompleted
data
LinkState
=
LinkStateLinked
ThreadId
|
LinkStateThrowing
|
LinkStateCompleted
deriving
Eq
deriving
stock
Eq
-- | Links the execution of a computation to a resource manager.
--
-- The computation is executed on the current thread. When the resource manager is disposed before the computation
-- is completed, a `CancelLinkedExecution`-exception is thrown to the current thread.
linkExecution
::
MonadResourceManager
m
=>
m
a
->
m
(
Maybe
a
)
linkExecution
action
=
do
key
<-
liftIO
$
newUnique
var
<-
liftIO
$
newTVarIO
=<<
LinkStateLinked
<$>
myThreadId
registerSimpleDisposeAction
$
do
atomically
(
swapTVar
var
LinkStateThrowing
)
>>=
\
case
LinkStateLinked
threadId
->
throwTo
threadId
$
CancelLinkedExecution
key
LinkStateThrowing
->
pure
()
-- Dispose called twice
LinkStateCompleted
->
pure
()
-- Thread has already left link
catch
do
result
<-
action
state
<-
liftIO
$
atomically
$
swapTVar
var
LinkStateCompleted
when
(
state
==
LinkStateThrowing
)
$
sleepForever
-- Wait for exception to arrive
pure
$
Just
result
\
ex
@
(
CancelLinkedExecution
exceptionKey
)
->
if
key
==
exceptionKey
then
return
Nothing
else
throwM
ex
-- * Resource manager implementations
-- * Resource manager implementations
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment