ox

package ox

Members list

Packages

package ox.channels
package ox.retry

Type members

Classlikes

trait CancellableFork[T] extends Fork[T]

A fork started using forkCancellable, backed by a (virtual) thread.

A fork started using forkCancellable, backed by a (virtual) thread.

Attributes

Supertypes
trait Fork[T]
class Object
trait Matchable
class Any
class DefaultSupervisor() extends Supervisor

Attributes

Supertypes
trait Supervisor
class Object
trait Matchable
class Any
trait Fork[T]

A fork started using fork, forkUser, forkCancellable or forkUnsupervised, backed by a (virtual) thread.

A fork started using fork, forkUser, forkCancellable or forkUnsupervised, backed by a (virtual) thread.

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
trait CancellableFork[T]
class ForkLocal[T](scopedValue: ScopedValue[T], default: T)

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object ForkLocal

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
ForkLocal.type
object NoOpSupervisor extends Supervisor

Attributes

Supertypes
trait Supervisor
class Object
trait Matchable
class Any
Self type
case class Ox(scope: StructuredTaskScope[Any], finalizers: AtomicReference[List[() => Unit]], supervisor: Supervisor)

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
trait Supervisor

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
object NoOpSupervisor.type
object syntax

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
syntax.type

Value members

Concrete methods

def collectPar[I, O, C <: (Iterable)](parallelism: Int)(iterable: => C[I])(pf: PartialFunction[I, O]): C[O]

Runs partial function in parallel on each element of iterable for which the partial function is defined. If function is not defined for an element such element is skipped. Using not more than parallelism forks concurrently.

Runs partial function in parallel on each element of iterable for which the partial function is defined. If function is not defined for an element such element is skipped. Using not more than parallelism forks concurrently.

Type parameters

C

type of iterable, must be a subtype of Iterable

I

type of elements of iterable

O

type of elements of result

Value parameters

iterable

collection to transform

parallelism

maximum number of concurrent forks

pf

partial function to apply to those elements of iterable for which it is defined

Attributes

Returns

collection of results of applying pf to elements of iterable for which it is defined. The returned collection is of the same type as iterable

def filterPar[I, C <: (Iterable)](parallelism: Int)(iterable: => C[I])(predicate: I => Boolean): C[I]

Runs predicate in parallel on each element of iterable. Elements for which predicate returns true are returned in the same order as in iterable. Elements for which predicate returns false are skipped. Using not more than parallelism forks concurrently.

Runs predicate in parallel on each element of iterable. Elements for which predicate returns true are returned in the same order as in iterable. Elements for which predicate returns false are skipped. Using not more than parallelism forks concurrently.

Type parameters

C

type of iterable, must be a subtype of Iterable

I

type of elements in iterable

Value parameters

iterable

collection to filter

parallelism

maximum number of concurrent forks

predicate

predicate to run on each element of iterable

Attributes

Returns

filtered collection

def foreachPar[I, C <: Iterable[I]](parallelism: Int)(iterable: => C)(operation: I => Any): Unit

Parallelize a foreach operation. Runs the operation on each element of the iterable in parallel. Using not more than parallelism forks concurrently.

Parallelize a foreach operation. Runs the operation on each element of the iterable in parallel. Using not more than parallelism forks concurrently.

Type parameters

C

the type of the iterable, must be a subtype of Iterable[I]

I

the type of the elements in the iterable

Value parameters

iterable

the collection to iterate over

operation

the operation to perform on each element

parallelism

the number of threads to use

Attributes

def forever(f: => Unit): Nothing
def fork[T](f: => T)(using Ox): Fork[T]

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes.

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes.

If ran in a supervised scope:

  • the fork behaves as a daemon thread
  • an exception thrown while evaluating t will cause the enclosing scope to end (cancelling all other running forks)
  • if the main body of the scope completes successfully, and all other user forks complete successfully, the scope will end, cancelling all running forks (including this one, if it's still running). That is, successful completion of this fork isn't required to end the scope.

For alternate behaviors, see forkUser, forkCancellable and forkUnsupervised.

If ran in an unsupervised scope (scoped):

  • in case an exception is thrown while evaluating t, it will be thrown when calling the returned Fork's .join() method.
  • if the main body of the scope completes successfully, while this fork is still running, the fork will be cancelled

Attributes

def forkAll[T](fs: Seq[() => T])(using Ox): Fork[Seq[T]]

For each thunk in the given sequence, starts a fork using fork. All forks are guaranteed to complete before the enclosing supervised or scoped block completes.

For each thunk in the given sequence, starts a fork using fork. All forks are guaranteed to complete before the enclosing supervised or scoped block completes.

If ran in a supervised scope, all forks behave as daemon threads (see fork for details).

Attributes

def forkCancellable[T](f: => T)(using Ox): CancellableFork[T]

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes, and which can be cancelled on-dmeand.

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes, and which can be cancelled on-dmeand.

In case an exception is thrown while evaluating t, it will be thrown when calling the returned Fork's .join() method.

The fork is unsupervised (similarly to forkUnsupervised), hence success or failure isn't signalled to the supervisor, and doesn't influence the scope's lifecycle.

For alternate behaviors, see fork, forkUser and forkUnsupervised.

Implementation note: a cancellable fork is created by starting a nested scope in a fork, and then starting a fork there. Hence, it is more expensive than fork, as two virtual threads are started.

Attributes

def forkUnsupervised[T](f: => T)(using Ox): Fork[T]

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes.

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes.

In case an exception is thrown while evaluating t, it will be thrown when calling the returned Fork's .join() method.

Success or failure isn't signalled to the supervisor, and doesn't influence the scope's lifecycle.

For alternate behaviors, see fork, forkUser and forkCancellable.

Attributes

def forkUser[T](f: => T)(using Ox): Fork[T]

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes.

Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing supervised or scoped block completes.

If ran in a supervised scope:

  • the fork behaves as a user-level thread
  • an exception thrown while evaluating t will cause the enclosing scope to end (cancelling all other running forks)
  • the scope won't end until the main body of the scope, and all other user forks (including this one) complete successfully. That is, successful completion of this fork is required to end the scope.

For alternate behaviors, see fork, forkCancellable and forkUnsupervised.

If ran in an unsupervised scope (scoped):

  • in case an exception is thrown while evaluating t, it will be thrown when calling the returned Fork's .join() method.
  • if the main body of the scope completes successfully, while this fork is still running, the fork will be cancelled

Attributes

def mapPar[I, O, C <: (Iterable)](parallelism: Int)(iterable: => C[I])(transform: I => O): C[O]

Runs parallel transformations on iterable. Using not more than parallelism forks concurrently.

Runs parallel transformations on iterable. Using not more than parallelism forks concurrently.

Type parameters

C

type of iterable, must be a subtype of Iterable

I

type of elements in iterable

O

type of elements in result

Value parameters

iterable

collection to transform

parallelism

maximum number of concurrent forks

transform

transformation to apply to each element of iterable

Attributes

Returns

transformed collection of the same type as input one

def par[T1, T2](t1: => T1)(t2: => T2): (T1, T2)

Runs the given computations in parallel. If any fails, interrupts the others, and re-throws the exception.

Runs the given computations in parallel. If any fails, interrupts the others, and re-throws the exception.

Attributes

def par[T1, T2, T3](t1: => T1)(t2: => T2)(t3: => T3): (T1, T2, T3)

Runs the given computations in parallel. If any fails, interrupts the others, and re-throws the exception.

Runs the given computations in parallel. If any fails, interrupts the others, and re-throws the exception.

Attributes

def par[T](ts: Seq[() => T]): Seq[T]

Runs the given computations in parallel. If any fails, interrupts the others, and re-throws the exception.

Runs the given computations in parallel. If any fails, interrupts the others, and re-throws the exception.

Attributes

def parLimit[T](parallelism: Int)(ts: Seq[() => T]): Seq[T]

Runs the given computations in parallel, with at most parallelism running in parallel at the same time. If any computation fails, interrupts the others, and re-throws the exception.

Runs the given computations in parallel, with at most parallelism running in parallel at the same time. If any computation fails, interrupts the others, and re-throws the exception.

Attributes

def raceResult[T](fs: Seq[() => T]): T

Returns the result of the first computation to complete (either successfully or with an exception).

Returns the result of the first computation to complete (either successfully or with an exception).

Attributes

def raceResult[T](f1: => T)(f2: => T): T

Returns the result of the first computation to complete (either successfully or with an exception).

Returns the result of the first computation to complete (either successfully or with an exception).

Attributes

def raceSuccess[T](fs: Seq[() => T]): T

Returns the result of the first computation to complete successfully, or if all fail - throws the first exception.

Returns the result of the first computation to complete successfully, or if all fail - throws the first exception.

Attributes

def raceSuccess[T](f1: => T)(f2: => T): T

Returns the result of the first computation to complete successfully, or if all fail - throws the first exception.

Returns the result of the first computation to complete successfully, or if all fail - throws the first exception.

Attributes

def repeatUntil(f: => Boolean): Unit

Repeat evaluating f until it evaluates to true.

Repeat evaluating f until it evaluates to true.

Attributes

def repeatWhile(f: => Boolean): Unit

Repeat evaluating f while it evaluates to true.

Repeat evaluating f while it evaluates to true.

Attributes

def scoped[T](f: Ox ?=> T): T

Starts a new scope, which allows starting forks in the given code block f. Forks can be started using fork, forkUser, forkCancellable and forkUnsupervised. All forks are guaranteed to complete before this scope completes.

Starts a new scope, which allows starting forks in the given code block f. Forks can be started using fork, forkUser, forkCancellable and forkUnsupervised. All forks are guaranteed to complete before this scope completes.

'''Warning:''' It is advisable to use supevised scopes if possible, as they minimise the chances of an error to go unnoticed. scoped scopes are considered an advanced feature, and should be used with caution.

The scope is ran in unsupervised mode, that is:

  • the scope ends once the f main body completes; this causes any running forks started within f to be cancelled
  • the scope completes (that is, this method returns) only once all forks started by f have completed (either successfully, or with an exception)
  • fork failures aren't handled in any special way, but can be inspected using Fork.join

Forks created using fork, forkUser and forkUnsupervised will behave exactly the same.

Attributes

See also

supervised Starts a scope in supervised mode

def supervised[T](f: Ox ?=> T): T

Starts a new scope, which allows starting forks in the given code block f. Forks can be started using fork, forkUser, forkCancellable and forkUnsupervised. All forks are guaranteed to complete before this scope completes.

Starts a new scope, which allows starting forks in the given code block f. Forks can be started using fork, forkUser, forkCancellable and forkUnsupervised. All forks are guaranteed to complete before this scope completes.

The scope is ran in supervised mode, that is:

  • the scope ends once all user, supervised forks (started using forkUser), including the f main body, succeed. Forks started using fork (daemon) don't have to complete successfully for the scope to end.
  • the scope also ends once the first supervised fork (including the f main body) fails with an exception
  • when the scope ends, all running forks are cancelled
  • the scope completes (that is, this method returns) only once all forks started by f have completed (either successfully, or with an exception)

Attributes

See also

scoped Starts a scope in unsupervised mode

def supervisor[T](supervisor: Supervisor)(f: Ox ?=> T)(using Ox): T

Change the supervisor that is being used when running f. Doesn't affect existing usages of the current supervisor, or forks ran outside of f.

Change the supervisor that is being used when running f. Doesn't affect existing usages of the current supervisor, or forks ran outside of f.

Attributes

def timeout[T](duration: FiniteDuration)(t: => T): T

The result of computation t, if it took less than duration, and a TimeoutException otherwise.

The result of computation t, if it took less than duration, and a TimeoutException otherwise.

Attributes

Throws
TimeoutException

If t took more than duration.

def timeoutOption[T](duration: FiniteDuration)(t: => T): Option[T]

A Some if the computation t took less than duration, and None otherwise.

A Some if the computation t took less than duration, and None otherwise.

Attributes

def uninterruptible[T](f: => T): T
def useCloseableInScope[T <: AutoCloseable](c: => T)(using Ox): T
def useInScope[T](acquire: => T)(release: T => Unit)(using Ox): T

Use the given resource in the current scope. The resource is allocated using acquire, and released after the scope is done using release. Releasing is uninterruptible.

Use the given resource in the current scope. The resource is allocated using acquire, and released after the scope is done using release. Releasing is uninterruptible.

Attributes

def useScoped[T, U](acquire: => T)(release: T => Unit)(b: T => U): U
def useScoped[T <: AutoCloseable, U](acquire: => T)(b: T => U): U
def useSupervised[T, U](acquire: => T)(release: T => Unit)(b: T => U): U
def useSupervised[T <: AutoCloseable, U](acquire: => T)(b: T => U): U