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

AtomicCell#get should not semantically block #3518

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions docs/std/atomic-cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: Atomic Cell
A synchronized, concurrent, mutable reference.

Provides safe concurrent access and modification of its contents, by ensuring only one fiber
can operate on them at the time. Thus, **all** operations may semantically block the
can operate on them at the time. Thus, all operations except `get` may semantically block the
calling fiber.

```scala mdoc:silent
Expand All @@ -22,15 +22,15 @@ abstract class AtomicCell[F[_], A] {

## Using `AtomicCell`

The `AtomicCell` can be treated as a combination of `Semaphore` and `Ref`:
The `AtomicCell` can be treated as a combination of `Mutex` and `Ref`:
```scala mdoc:reset:silent
import cats.effect.{IO, Ref}
import cats.effect.std.Semaphore
import cats.effect.std.Mutex

trait State
class Service(sem: Semaphore[IO], ref: Ref[IO, State]) {
class Service(mtx: Mutex[IO], ref: Ref[IO, State]) {
def modify(f: State => IO[State]): IO[Unit] =
sem.permit.surround {
mtx.lock.surround {
for {
current <- ref.get
next <- f(current)
Expand Down
13 changes: 5 additions & 8 deletions std/shared/src/main/scala/cats/effect/std/AtomicCell.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import cats.syntax.all._
* A synchronized, concurrent, mutable reference.
*
* Provides safe concurrent access and modification of its contents, by ensuring only one fiber
* can operate on them at the time. Thus, '''all''' operations may semantically block the
* can operate on them at the time. Thus, all operations except `get` may semantically block the
* calling fiber.
*
* {{{
Expand Down Expand Up @@ -166,8 +166,7 @@ object AtomicCell {
)(
implicit F: Concurrent[F]
) extends AtomicCell[F, A] {
override def get: F[A] =
mutex.lock.surround(ref.get)
override def get: F[A] = ref.get

override def set(a: A): F[Unit] =
mutex.lock.surround(ref.set(a))
Expand Down Expand Up @@ -199,13 +198,11 @@ object AtomicCell {
)(
implicit F: Async[F]
) extends AtomicCell[F, A] {
private var cell: A = init
@volatile private var cell: A = init

override def get: F[A] =
mutex.lock.surround {
F.delay {
cell
}
F.delay {
cell
}

override def set(a: A): F[Unit] =
Expand Down
12 changes: 12 additions & 0 deletions tests/shared/src/test/scala/cats/effect/std/AtomicCellSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ final class AtomicCellSpec extends BaseSpec {

op must completeAs(true)
}

"get should not block during concurrent modification" in ticked { implicit ticker =>
val op = for {
cell <- factory(0)
gate <- IO.deferred[Unit]
_ <- cell.evalModify(_ => gate.complete(()) *> IO.never).start
_ <- gate.get
r <- cell.get
} yield r == 0

op must completeAs(true)
}
}
}
}