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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/Lock.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2020-2023 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.std

import cats.effect.kernel.{Concurrent, Poll, Resource}
import cats.syntax.functor._

abstract class Lock[F[_]] {
def shared: Resource[F, Unit]
def exclusive: Resource[F, Unit]
}
Comment on lines +22 to +25
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

Comment on lines +22 to +25
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 !


object Lock {
def apply[F[_]: Concurrent]: F[Lock[F]] = apply(Long.MaxValue)

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))
}
}
Comment on lines +30 to +38
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. :-)

}