cats-effect
cats-effect copied to clipboard
"Single-Resourced"
Prompted by https://github.com/typelevel/cats-effect/issues/3376 I was thinking again about reference-counting resources.
In https://github.com/typelevel/cats-effect/issues/3376 we consider ResourceSupervisor as the Resource analog of Fiber's Supervisor.
A related question: is there a Resource analog of Chris' Single-Fibered? I think it may look very much like a reference-counted Resource.
I'm imagining a signature like this:
def prepare[F[_], A](resource: Resource[F, A]): F[Resource[F, A]]
I realize this is a suspicious looking signature so bear with me!
The semantics of the returned Resource would be:
- acquire:
- if the resource is not presently acquired, it will acquire it
- if the resource is currently being acquired, it will await that
- if the resource is already acquired, it will grab it and increment the counter
- release:
- if this is the sole owner of the resource, it will release it
- otherwise simply decrement the counter
Note that under this scheme the resource may be acquired more than once, if the count goes to zero and then starts increasing again.
This would be implemented by allocating a Ref to hold the state, hence the return signature in F[_].
In single-fibered, there is at most one fiber running the task at any given time. In single-resourced, there is at most one acquisition of the resource at any given time.