package domainpc

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Domainpc.DomainSource

include module type of Domain
Sourcetype !'a t

A domain of type 'a t runs independently, eventually producing a result of type 'a, or an exception

Sourceval join : 'a t -> 'a

join d blocks until domain d runs to completion. If d results in a value, then that is returned by join d. If d raises an uncaught exception, then that is re-raised by join d.

Sourcetype id = private int

Domains have unique integer identifiers

Sourceval get_id : 'a t -> id

get_id d returns the identifier of the domain d

Sourceval self : unit -> id

self () is the identifier of the currently running domain

Sourceval before_first_spawn : (unit -> unit) -> unit

before_first_spawn f registers f to be called before the first domain is spawned by the program. The functions registered with before_first_spawn are called on the main (initial) domain. The functions registered with before_first_spawn are called in 'first in, first out' order: the oldest function added with before_first_spawn is called first.

Sourceval at_exit : (unit -> unit) -> unit

at_exit f registers f to be called when the current domain exits. Note that at_exit callbacks are domain-local and only apply to the calling domain. The registered functions are called in 'last in, first out' order: the function most recently added with at_exit is called first. An example:

let temp_file_key = Domain.DLS.new_key (fun _ ->
  let tmp = snd (Filename.open_temp_file "" "") in
  Domain.at_exit (fun () -> close_out_noerr tmp);
  tmp)

The snippet above creates a key that when retrieved for the first time will open a temporary file and register an at_exit callback to close it, thus guaranteeing the descriptor is not leaked in case the current domain exits.

Sourceval cpu_relax : unit -> unit

If busy-waiting, calling cpu_relax () between iterations will improve performance on some CPU architectures

Sourceval is_main_domain : unit -> bool

is_main_domain () returns true if called from the initial domain.

The recommended maximum number of domains which should be running simultaneously (including domains already running).

The value returned is at least 1.

Sourceval self_index : unit -> int

The index of the current domain. It is an integer unique among currently-running domains, in the interval 0; N-1 where N is the peak number of domains running simultaneously so far.

The index of a terminated domain may be reused for a new domain. Use (Domain.self () :> int) instead for an identifier unique among all domains ever created by the program.

  • since 5.3
Sourcemodule DLS : sig ... end

Domain-local Storage

Sourceval wait_on_unavailable : unit -> unit

wait_on_unavailable () when all cores are used and more are requested, wait for cores to be freed instead of crashing.

Sourceval get_available_cores : unit -> int

get_available_cores () returns the number of available cores.

Sourceval isolate_current : unit -> unit

isolate_current () ensures that the current domain (usually the main one) is restricted to run on a core where other isolated domains can't run.

Sourceval spawn : ?isolated:bool -> (unit -> 'a) -> 'a Domain.t

spawn ?isolated f if isolated is true spawns a domain that runs on a given core, otherwise behaves as Domain.spawn.

isolated is true by default.

  • raises Failure

    if spawn is called multiple times (or both spawn and spawn_n are called) with different values of isolated.

Sourceval spawn_n : ?isolated:bool -> ?n:int -> (unit -> 'a) -> 'a Domain.t array

spawn_n ?isolated ?n f if isolated is true, n is provided and is less than or equal to the number of free physical cores, spawns n new domains. If n is not provided and there is at least one free physical core, spawns as many domains as there are physical cores.

If isolated is false, the spawned domains are not guaranteed to run on separate cores.

isolated is true by default.

  • raises Failure

    if there aren't enough free physical cores.

  • raises Failure

    if spawn_n is called multiple times (or both spawn and spawn_n are called) with different values of isolated.