Provide a better mechanism for handling thread-local data through the FFI
!120 (merged) removed support for pinning processes, as the API was a bit clunky to use, it complicates the process scheduler a fair bit, and can lead to thread starvation (e.g. if you have 8 threads, 16 processes, and 8 are pinned, the other 8 won't run).
When interacting with C, there are times where we need to perform two or more calls and guarantee the process isn't moved to a different thread in between. The most simple case is when you call a C function and want to check errno
after that. With pinning removed, there's no reliable way of doing this.
One option is to not introduce pinning, but rather a "hint" that ensures a process doesn't suspend/reschedule unless it has to. This would basically set the available reductions to an infinite value (or just ignore it), preventing automatic suspension. The "hint" part is important: if the process waits for a future or IO, it simply has to suspend and there's no way around it. A hypothetical API might look as following:
import std::process::(no_suspend)
import std::ffi::(Library, Type)
let lib = Librari.new(...).unwrap
let var = lib.variable('errno').unwrap
no_suspend fn {
foo()
bar()
if var.read(type: Type.U8, offset: 0) as Int == 0 {
...
}
}
Here the name no_suspend
is a just a temporary name, and we'd need something better for the final version.
For APIs that demand C code runs on the same thread always, I'm not sure I want to provide any specific APIs. The above wouldn't work for that, as there's no guarantee as to what thread the code runs on before/after the no_suspend
. Probably the best option here is to just advice users to run the code in the main
process, as that process is always scheduled onto the main thread, and no other processes run on that thread.