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
fe167cba
Commit
fe167cba
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Add IsDisposable instances for ResourceManager and AsyncTask
parent
985158ab
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/Core.hs
+25
-6
25 additions, 6 deletions
src/Quasar/Core.hs
with
25 additions
and
6 deletions
src/Quasar/Core.hs
+
25
−
6
View file @
fe167cba
...
...
@@ -7,11 +7,13 @@ module Quasar.Core (
withDefaultResourceManager
,
withUnlimitedResourceManager
,
newResourceManager
,
disposeResourceManager
,
defaultResourceManagerConfiguration
,
unlimitedResourceManagerConfiguration
,
-- * AsyncTask
AsyncTask
,
cancelTask
,
cancelTaskIO
,
toAsyncTask
,
successfulTask
,
...
...
@@ -30,6 +32,8 @@ module Quasar.Core (
noDisposable
,
disposeEventually
,
boundDisposable
,
attachDisposeAction
,
attachDisposeAction_
,
)
where
import
Control.Concurrent
(
ThreadId
,
forkIOWithUnmask
,
myThreadId
)
...
...
@@ -84,12 +88,14 @@ instance HasResourceManager AsyncIO where
awaitResult
::
IsAwaitable
r
a
=>
AsyncIO
a
->
AsyncIO
r
awaitResult
=
(
await
=<<
)
-- TODO rename to ResourceManager
data
ResourceManager
=
ResourceManager
{
configuration
::
ResourceManagerConfiguraiton
,
threads
::
TVar
(
HashSet
ThreadId
)
}
instance
IsDisposable
ResourceManager
where
dispose
x
=
pure
$
pure
()
-- | A task that is running asynchronously. It has a result and can fail.
-- The result (or exception) can be aquired by using the `Awaitable` class (e.g. by calling `await` or `awaitIO`).
...
...
@@ -100,6 +106,9 @@ newtype AsyncTask r = AsyncTask (Awaitable r)
instance
IsAwaitable
r
(
AsyncTask
r
)
where
toAwaitable
(
AsyncTask
awaitable
)
=
awaitable
instance
IsDisposable
(
AsyncTask
r
)
where
dispose
=
undefined
instance
Functor
AsyncTask
where
fmap
fn
(
AsyncTask
x
)
=
AsyncTask
(
fn
<$>
x
)
...
...
@@ -107,9 +116,11 @@ instance Applicative AsyncTask where
pure
=
AsyncTask
.
pure
liftA2
fn
(
AsyncTask
fx
)
(
AsyncTask
fy
)
=
AsyncTask
$
liftA2
fn
fx
fy
cancelTask
::
AsyncTask
r
->
IO
()
-- TODO resource management
cancelTask
=
const
(
pure
()
)
cancelTask
::
AsyncTask
r
->
IO
(
Awaitable
()
)
cancelTask
=
dispose
cancelTaskIO
::
AsyncTask
r
->
IO
()
cancelTaskIO
=
awaitIO
<=<
dispose
-- | Creates an `AsyncTask` from an `Awaitable`.
-- The resulting task only depends on an external resource, so disposing it has no effect.
...
...
@@ -228,4 +239,12 @@ disposeEventually resourceManager disposable = liftIO $ do
-- | Creates an `Disposable` that is bound to a ResourceManager. It will automatically be disposed when the resource manager is disposed.
boundDisposable
::
HasResourceManager
m
=>
IO
(
Awaitable
()
)
->
m
Disposable
boundDisposable
=
undefined
boundDisposable
action
=
do
resourceManager
<-
askResourceManager
attachDisposeAction
resourceManager
action
attachDisposeAction
::
MonadIO
m
=>
ResourceManager
->
IO
(
Awaitable
()
)
->
m
Disposable
attachDisposeAction
=
undefined
attachDisposeAction_
::
MonadIO
m
=>
ResourceManager
->
IO
(
Awaitable
()
)
->
m
()
attachDisposeAction_
resourceManager
action
=
void
$
attachDisposeAction
resourceManager
action
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