-
Notifications
You must be signed in to change notification settings - Fork 521
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
base: series/3.x
Are you sure you want to change the base?
Add Lock to std #3542
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. :-) |
||
} |
There was a problem hiding this comment.
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 doLock[IO].flatMap(lock => ...)
and wonder why it doesn't work. :-D