Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lock to std #3542

Draft
wants to merge 3 commits into
base: series/3.x
Choose a base branch
from
Draft

Conversation

kevchuang
Copy link

Resolves #3535

Comment on lines +21 to +24
abstract class Lock[F[_]] {
def shared: Resource[F, Unit]
def exclusive: Resource[F, Unit]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we should have an implementation here as well, apart from just HotSwap. People are almost certainly going to try to do Lock[IO].flatMap(lock => ...) and wonder why it doesn't work. :-D

Copy link
Member

@djspiewak djspiewak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to note the requested changes in the PR state :-)

@@ -51,7 +51,7 @@ import cats.syntax.all._
*
* Ported from https://github.com/typelevel/fs2.
*/
sealed trait Hotswap[F[_], R] {
sealed trait Hotswap[F[_], R] extends Lock[F] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure Hotswap should extend Lock – it seems more like an implementation detail of Hotswap.

Copy link
Member

@djspiewak djspiewak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines +22 to +25
abstract class Lock[F[_]] {
def shared: Resource[F, Unit]
def exclusive: Resource[F, Unit]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to consider adding some other APIs here. For example:

  def tryShared: Resource[F, Boolean]
  def tryExclusive: Resource[F, Boolean]

Also should check Chris's lib for inspiration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood ! I'm working on that !

Comment on lines +30 to +38
def apply[F[_]: Concurrent](maxShared: Long): F[Lock[F]] =
Semaphore[F](maxShared).map { semaphore =>
new Lock[F] {
override def shared: Resource[F, Unit] = semaphore.permit
override def exclusive: Resource[F, Unit] =
Resource.makeFull((poll: Poll[F]) => poll(semaphore.acquireN(maxShared)))(_ =>
semaphore.releaseN(maxShared))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for this implementation. For example to verify that the exclusive lock is indeed exclusive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely should be tested. It occurred to me the other day that I just didn't catch that; thanks for being on it, Arman. :-)

@ChristopherDavenport
Copy link
Member

ChristopherDavenport commented Apr 26, 2023

Not sure this is enough for folks that want traditional locking.

Like its an approach, but locks are decently diverse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Lock to std
5 participants