On this page:
Executor
Executor.register
Executor.execute
Executor.maybe_  execute
0.45+9.2.0.3

16.3 Will Executors🔗ℹ

 import: rhombus/will package: rhombus-lib

A will executor support finalization by maintaining a collection of will functions associated to Rhombus objects. When an object becomes unreachable, its will are ready to be executed. See Wills and Will Executors for more information.

A will executor can be used as a synchronizable event. A will executor is ready for synchronization when will.Executor.execute would not block, and the synchronization result is the executor itself.

class

class will.Executor()

Represents a will executor.

method

method (executor :: will.Executor).register(

  v :: Any,

  v_will :: (Any) -> ~any

) :: Void

Registers the value v with the function v_will in executor. When v is proven nreachable (see Garbage Collection), then the function v_will is ready to be called with v as its argument via will.Executor.execute or will.Executor.maybe_execute.

The v_will argument is strongly referenced by executor until it is executed, which is why v is supplied back to v_will instead of expecting v to be in v_will’s closure.

See will.Executor.execute for an example.

method

method (executor :: will.Executor).execute()

 

method

method (executor :: will.Executor).maybe_execute(

  ~none: none :: Any = #false

)

Invokes the will function for a single otherwise-unreachable value registered with executor. The values returned by the will procedure are the result of the will.Executor.execute or will.Executor.maybe_execute call.

If no will is ready for immediate execution, will.Executor.execute blocks until one is ready, while will.Executor.maybe_execute returns none immediately.

The following example reflects a relatively simple pattern where will functions can run at any time, so a thread is created to run wills as soon as they become available.

block:

  def exor = will.Executor()

  def th:

    thread:

      while #true:

        exor.execute()

  def mutable box_to_track = Box(#false)

  exor.register(box_to_track,

                fun (bx):

                  println("a-box is now garbage"))

  memory.gc()

  box_to_track := #false

  memory.gc()

  // printing is likely at this point

  Evt.system_idle.sync()

  #void