Zig.Nif.Threaded (zigler v0.9.1) View Source

Generates code for threaded nifs.

threaded functions require several parts to get right.

  1. a resource that holds space for arguments, result, and flags. This is created by the calling function and owned by the process that called the function.

  2. a packer function which takes the beam arguments and shoves them into resource struct, then launches the function, returning the resource.

  3. a launcher function which runs the packer wrapping the errors from the launch function. The launch function must be a nif function, as it will be called from the BEAM.

  4. a harness function which is passed the resource struct, and is responsible for unwrapping beam terms into function parameters. This is what runs the nif function.

Cleanup Strategy

  • If the thread is "naturally completed", then yield_info does not need to be cleaned up internally, it gets cleaned up by the harness function, and the finished value in yield_info is set to true.

  • If the thread has been "killed", then yield_info's cancelled value is set to true and the thread is given 500us (in 50 us chunks) to set its finished value to true.

  • If the thread has not self-terminated, it will be kept running, and its resources may not wind up being properly cleaned up.

Link to this section Summary

Link to this section Functions