iyi

Standard library, 0.22.0

std/shared_proc

SharedProc, a proc that may be called from any thread - SPEC.md III.4.4, III.4.13.

import std/shared_proc to use it. Source: src/std/shared_proc.iyi at v0.22.0.

A Proc is never Share: what it captured is not written in its type. So a value that holds one - a table of routes, a handler a server calls on every core - is refused by every parallel block that reaches it, whatever the proc closed over. SharedProc(A, R) is the one way past that the compiler can vouch for: it is made only by SharedProc.of, whose block is checked where it is written as a parallel task's is - every value it captures Share, every captured local assigned once, the constants and class variables it names Share - because of is @[Share]. What it holds is then reached by nothing but call, from as many threads as call it, and the type is Share whatever A and R are: a call's argument and answer belong to the thread that makes the call.

  doubled = SharedProc(Int32, Int32).of { |n| n * 2 }
  group do |g|
    4.times { |i| g.spawn(unit: :new) { doubled.call(i) } }
  end

A def that keeps a block and is itself @[Share] - std/http's Server.serve { }, std/router's SharedRouter#handle - hands its block on with SharedProc(...).of(&block): that block was checked where the def was given it, and it is the one proc a boundary takes on trust. Any other &proc is refused there.

SharedProc(A, R)struct

std/shared_prociyi doc src/std/shared_proc.iyi
pub struct SharedProc(A, R)
  def call(argument : A) : R
  # The block, checked as a parallel task's block is, as a value any
  # thread may call. With `-Dheap_partitions` what it captured is
  # published here (heap22 row 8); *pinned* is the compiler's, as
  # `spawn_new`'s is.
  def self.of(*, pinned : Bool = false, &block : (A -> R)) : SharedProc(A, R)
end