Zig.Nif.Threaded (zigler v0.9.1) View Source
Generates code for threaded nifs.
threaded functions require several parts to get right.
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.
a
packerfunction which takes the beam arguments and shoves them into resource struct, then launches the function, returning the resource.a
launcherfunction which runs thepackerwrapping the errors from the launch function. The launch function must be a nif function, as it will be called from the BEAM.a
harnessfunction 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
finishedvalue in yield_info is set totrue.If the thread has been "killed", then yield_info's
cancelledvalue is set to true and the thread is given 500us (in 50 us chunks) to set itsfinishedvalue 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.