AtomicCell

cats.effect.std.AtomicCell
See theAtomicCell companion object
abstract class AtomicCell[F[_], A]

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 calling fiber.

 final class ParkingLot(data: AtomicCell[IO, ArraySeq[Boolean]], rnd: Random[IO]) {
   def getSpot: IO[Option[Int]] =
     data.evalModify { spots =>
       val availableSpots = spots.zipWithIndex.collect { case (true, idx) => idx }
       rnd.shuffleList(availableSpots).map { shuffled =>
         val acquired = shuffled.headOption
         val next = acquired.fold(spots)(a => spots.updated(a, false)) // mark the chosen spot as taken
         (next, shuffled.headOption)
       }
     }
 }

Attributes

See also:

cats.effect.kernel.Ref for a non-blocking alternative.

Companion:
object
Source:
AtomicCell.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Concise view

Value members

Abstract methods

def evalGetAndUpdate(f: A => F[A]): F[A]

Updates the current value using the provided effectual function, and returns the previous value.

Updates the current value using the provided effectual function, and returns the previous value.

Attributes

Source:
AtomicCell.scala
def evalModify[B](f: A => F[(A, B)]): F[B]

Like evalUpdate but allows the update function to return an output value.

Like evalUpdate but allows the update function to return an output value.

Attributes

Source:
AtomicCell.scala
def evalUpdate(f: A => F[A]): F[Unit]

Like update but using an effectual function; which is guaranteed to run only once.

Like update but using an effectual function; which is guaranteed to run only once.

Attributes

Source:
AtomicCell.scala
def evalUpdateAndGet(f: A => F[A]): F[A]

Updates the current value using the provided effectual function, and returns the updated value.

Updates the current value using the provided effectual function, and returns the updated value.

Attributes

Source:
AtomicCell.scala
def get: F[A]

Obtains the current value.

Obtains the current value.

Attributes

Source:
AtomicCell.scala
def modify[B](f: A => (A, B)): F[B]

Like update but allows the update function to return an output value.

Like update but allows the update function to return an output value.

Attributes

Source:
AtomicCell.scala
def set(a: A): F[Unit]

Sets the current value to a.

Sets the current value to a.

Attributes

Source:
AtomicCell.scala

Concrete methods

def getAndSet(a: A): F[A]

Replaces the current value with a, returning the previous value.

Replaces the current value with a, returning the previous value.

Attributes

Source:
AtomicCell.scala
def getAndUpdate(f: A => A): F[A]

Updates the current value using the provided function, and returns the previous value.

Updates the current value using the provided function, and returns the previous value.

Attributes

Source:
AtomicCell.scala
def update(f: A => A): F[Unit]

Modifies the current value using the supplied update function.

Modifies the current value using the supplied update function.

Attributes

Source:
AtomicCell.scala
def updateAndGet(f: A => A): F[A]

Updates the current value using the provided function, and returns the updated value.

Updates the current value using the provided function, and returns the updated value.

Attributes

Source:
AtomicCell.scala