Packages

  • package root
    Definition Classes
    root
  • package monix
    Definition Classes
    root
  • package execution
    Definition Classes
    monix
  • package annotations
    Definition Classes
    execution
  • package atomic

    A small toolkit of classes that support compare-and-swap semantics for safe mutation of variables.

    A small toolkit of classes that support compare-and-swap semantics for safe mutation of variables.

    On top of the JVM, this means dealing with lock-free thread-safe programming. Also works on top of Javascript, with Scala.js, for API compatibility purposes and because it's a useful way to box a value.

    The backbone of Atomic references is this method:

    def compareAndSet(expect: T, update: T): Boolean

    This method atomically sets a variable to the update value if it currently holds the expect value, reporting true on success or false on failure. The classes in this package also contain methods to get and unconditionally set values.

    Building a reference is easy with the provided constructor, which will automatically return the most specific type needed (in the following sample, that's an AtomicDouble, inheriting from AtomicNumber[A]):

    val atomicNumber = Atomic(12.2)
    
    atomicNumber.incrementAndGet()
    // => 13.2

    These also provide useful helpers for atomically mutating of values (i.e. transform, transformAndGet, getAndTransform, etc...) or of numbers of any kind (incrementAndGet, getAndAdd, etc...).

    Definition Classes
    execution
  • package cancelables

    Cancelables represent asynchronous units of work or other things scheduled for execution and whose execution can be canceled.

    Cancelables represent asynchronous units of work or other things scheduled for execution and whose execution can be canceled.

    One use-case is the scheduling done by monix.execution.Scheduler, in which the scheduling methods return a Cancelable, allowing the canceling of the scheduling.

    Example:

    val s = ConcurrentScheduler()
    val task = s.scheduleRepeated(10.seconds, 50.seconds, {
      doSomething()
    })
    
    // later, cancels the scheduling ...
    task.cancel()
    Definition Classes
    execution
  • package exceptions
    Definition Classes
    execution
  • package internal
    Definition Classes
    execution
  • package misc
    Definition Classes
    execution
  • package rstreams

    Package exposing utilities for working with the Reactive Streams specification.

    Package exposing utilities for working with the Reactive Streams specification.

    Definition Classes
    execution
  • package schedulers
    Definition Classes
    execution
  • Ack
  • AsyncQueue
  • AsyncSemaphore
  • AsyncVar
  • BufferCapacity
  • Callback
  • Cancelable
  • CancelableFuture
  • CancelablePromise
  • ChannelType
  • ExecutionModel
  • FutureUtils
  • Scheduler
  • UncaughtExceptionReporter
  • compat

final class AsyncSemaphore extends GenericSemaphore[Cancelable]

The AsyncSemaphore is an asynchronous semaphore implementation that limits the parallelism on Future execution.

The following example instantiates a semaphore with a maximum parallelism of 10:

val semaphore = AsyncSemaphore(maxParallelism = 10)

def makeRequest(r: HttpRequest): Future[HttpResponse] = ???

// For such a task no more than 10 requests
// are allowed to be executed in parallel.
val future = semaphore.greenLight(() => makeRequest(???))
Linear Supertypes
GenericSemaphore[Cancelable], Serializable, Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. AsyncSemaphore
  2. GenericSemaphore
  3. Serializable
  4. Serializable
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def acquire(): CancelableFuture[Unit]

    Acquires a single permit.

    Acquires a single permit. Alias for acquireN(1).

    Annotations
    @UnsafeBecauseImpure()
    See also

    withPermit, the preferred way to acquire and release

    acquireN for a version that can acquire multiple permits

  5. def acquireN(n: Long): CancelableFuture[Unit]

    Acquires n permits.

    Acquires n permits.

    The returned effect semantically blocks until all requested permits are available. Note that acquires are satisfied in strict FIFO order, so given an AsyncSemaphore with 2 permits available, an acquireN(3) will always be satisfied before a later call to acquireN(1).

    n

    number of permits to acquire - must be >= 0

    returns

    a future that will complete when the acquisition has succeeded or that can be cancelled, removing the listener from the queue (to prevent memory leaks in race conditions)

    Annotations
    @UnsafeBecauseImpure()
    See also

    withPermit, the preferred way to acquire and release

    acquire for a version acquires a single permit

  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def available(): Long

    Returns the number of permits currently available.

    Returns the number of permits currently available. Always non-negative.

    The protocol is unsafe, the semaphore is used in concurrent settings and thus the value returned isn't stable or reliable. Use with care.

    Annotations
    @UnsafeProtocol() @UnsafeBecauseImpure()
  8. def awaitAvailable(n: Long): CancelableFuture[Unit]

    Returns a future that will be complete when the specified number of permits are available.

    Returns a future that will be complete when the specified number of permits are available.

    The protocol is unsafe because by the time the returned future completes, some other process might have already acquired the available permits and thus usage of awaitAvailable can lead to fragile concurrent logic. Use with care.

    Can be useful for termination logic, for example to execute a piece of logic once all available permits have been released.

    n

    is the number of permits waited on

    Annotations
    @UnsafeProtocol() @UnsafeBecauseImpure()
  9. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  10. def count(): Long

    Obtains a snapshot of the current count.

    Obtains a snapshot of the current count. Can be negative.

    Like available when permits are available but returns the number of permits callers are waiting for when there are no permits available.

    Annotations
    @UnsafeProtocol() @UnsafeBecauseImpure()
  11. def emptyCancelable: Cancelable
    Attributes
    protected
    Definition Classes
    AsyncSemaphore → GenericSemaphore
  12. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  14. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  15. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  16. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  17. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  18. def makeCancelable(f: (Listener[Unit]) ⇒ Unit, p: Listener[Unit]): Cancelable
    Attributes
    protected
    Definition Classes
    AsyncSemaphore → GenericSemaphore
  19. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  20. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  21. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  22. def release(): Unit

    Releases a permit, returning it to the pool.

    Releases a permit, returning it to the pool.

    If there are consumers waiting on permits being available, then the first in the queue will be selected and given a permit immediately.

    Annotations
    @UnsafeBecauseImpure()
    See also

    withPermit, the preferred way to acquire and release

  23. def releaseN(n: Long): Unit

    Releases n permits, potentially unblocking up to n outstanding acquires.

    Releases n permits, potentially unblocking up to n outstanding acquires.

    n

    number of permits to release - must be >= 0

    Annotations
    @UnsafeBecauseImpure()
    See also

    withPermit, the preferred way to acquire and release

  24. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  25. def toString(): String
    Definition Classes
    AnyRef → Any
  26. def tryAcquire(): Boolean

    Alias for tryAcquireN(1).

    Alias for tryAcquireN(1).

    The protocol is unsafe, because with the "try*" methods the user needs a firm grasp of what race conditions are and how they manifest and usage of such methods can lead to very fragile logic.

    Annotations
    @UnsafeProtocol() @UnsafeBecauseImpure()
    See also

    tryAcquireN for the version that can acquire multiple permits

    acquire for the version that can wait for acquisition

    withPermit the preferred way to acquire and release

  27. def tryAcquireN(n: Long): Boolean

    Acquires n permits now and returns true, or returns false immediately.

    Acquires n permits now and returns true, or returns false immediately. Error if n < 0.

    The protocol is unsafe, because with the "try*" methods the user needs a firm grasp of what race conditions are and how they manifest and usage of such methods can lead to very fragile logic.

    n

    number of permits to acquire - must be >= 0

    Annotations
    @UnsafeProtocol() @UnsafeBecauseImpure()
    See also

    tryAcquire for the alias that acquires a single permit

    acquireN for the version that can wait for acquisition

    withPermit, the preferred way to acquire and release

  28. final def unsafeAcquireN(n: Long, await: Listener[Unit]): Cancelable
    Attributes
    protected
    Definition Classes
    GenericSemaphore
    Annotations
    @tailrec()
  29. final def unsafeAsyncAcquireN(n: Long, await: Listener[Unit]): Cancelable
    Attributes
    protected
    Definition Classes
    GenericSemaphore
    Annotations
    @tailrec()
  30. final def unsafeAvailable(): Long
    Attributes
    protected
    Definition Classes
    GenericSemaphore
  31. final def unsafeAwaitAvailable(n: Long, await: Listener[Unit]): Cancelable
    Attributes
    protected
    Definition Classes
    GenericSemaphore
    Annotations
    @tailrec()
  32. final def unsafeCount(): Long
    Attributes
    protected
    Definition Classes
    GenericSemaphore
  33. final def unsafeReleaseN(n: Long): Unit
    Attributes
    protected
    Definition Classes
    GenericSemaphore
    Annotations
    @tailrec()
  34. final def unsafeTryAcquireN(n: Long): Boolean
    Attributes
    protected
    Definition Classes
    GenericSemaphore
    Annotations
    @tailrec()
  35. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  36. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  37. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  38. def withPermit[A](f: () ⇒ Future[A]): CancelableFuture[A]

    Returns a new future, ensuring that the given source acquires an available permit from the semaphore before it is executed.

    Returns a new future, ensuring that the given source acquires an available permit from the semaphore before it is executed.

    The returned future also takes care of resource handling, releasing its permit after being complete.

    f

    is a function returning the Future instance we want to evaluate after we get the permit from the semaphore

    Annotations
    @UnsafeBecauseImpure()
  39. def withPermitN[A](n: Long)(f: () ⇒ Future[A]): CancelableFuture[A]

    Returns a new future, ensuring that the given source acquires n available permits from the semaphore before it is executed.

    Returns a new future, ensuring that the given source acquires n available permits from the semaphore before it is executed.

    The returned future also takes care of resource handling, releasing its permits after being complete.

    n

    is the number of permits required for the given function to be executed

    f

    is a function returning the Future instance we want to evaluate after we get the permit from the semaphore

    Annotations
    @UnsafeBecauseImpure()

Inherited from GenericSemaphore[Cancelable]

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped