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
f643c2ab
Commit
f643c2ab
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Add async disposable
Co-authored-by:
Jan Beinke
<
git@janbeinke.com
>
parent
aa7ef1f9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2556
canceled
3 years ago
Stage: build
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Quasar/Disposable.hs
+32
-0
32 additions, 0 deletions
src/Quasar/Disposable.hs
with
32 additions
and
0 deletions
src/Quasar/Disposable.hs
+
32
−
0
View file @
f643c2ab
...
...
@@ -9,6 +9,9 @@ module Quasar.Disposable (
newDisposable
,
noDisposable
,
-- ** Async Disposable
newAsyncDisposable
,
-- ** STM disposable
STMDisposable
,
newSTMDisposable
,
...
...
@@ -25,6 +28,7 @@ module Quasar.Disposable (
awaitResourceManagerResult
,
)
where
import
Control.Concurrent
(
forkIO
)
import
Control.Concurrent.STM
import
Control.Monad.Catch
import
Control.Monad.Reader
...
...
@@ -149,6 +153,34 @@ newDisposable disposeAction = liftIO do
fmap
toDisposable
$
IODisposable
key
<$>
newTMVarIO
disposeAction
<*>
newDisposableFinalizers
<*>
newAsyncVar
data
AsyncDisposable
=
AsyncDisposable
Unique
(
TMVar
(
IO
()
))
DisposableFinalizers
(
AsyncVar
()
)
instance
IsDisposable
AsyncDisposable
where
beginDispose
(
AsyncDisposable
key
actionVar
finalizers
resultVar
)
=
do
-- This is only safe when run in masked state
atomically
(
tryTakeTMVar
actionVar
)
>>=
mapM_
\
action
->
do
void
$
forkIO
do
result
<-
try
action
atomically
do
putAsyncVarEitherSTM_
resultVar
result
defaultRunFinalizers
finalizers
pure
$
DisposeResultAwait
$
await
resultVar
isDisposed
(
AsyncDisposable
_
_
_
resultVar
)
=
toAwaitable
resultVar
`
catchAll
`
\
_
->
pure
()
registerFinalizer
(
AsyncDisposable
_
_
finalizers
_
)
=
defaultRegisterFinalizer
finalizers
-- | Create a new disposable from an IO action. The action will be run asynchrously. Is is guaranteed, that the IO
-- action will only be called once (even when `dispose` is called multiple times).
--
-- The action must not block for an unbound time.
newAsyncDisposable
::
MonadIO
m
=>
IO
()
->
m
Disposable
newAsyncDisposable
disposeAction
=
liftIO
do
key
<-
newUnique
fmap
toDisposable
$
AsyncDisposable
key
<$>
newTMVarIO
disposeAction
<*>
newDisposableFinalizers
<*>
newAsyncVar
data
STMDisposable
=
STMDisposable
Unique
(
TMVar
(
STM
()
))
DisposableFinalizers
(
AsyncVar
()
)
instance
IsDisposable
STMDisposable
where
...
...
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