This is a Request For an RFC (thus “RFRFC”), for a standard library function that allows one to register a procedure that is invoked for every subsequent thread that is spawned.
(One might think this the dual to thread::at_exit, but that’s not quite true … at_exit only invokes its callbacks when the main thread exits. This is meant to run when any thread starts.)
The immediate use case I have in mind is for the boehm-demers-weiser (BDW) conservative GC, which strongly encourages a configuration where one registers each thread that may call into the GC. See also https://github.com/swgillespie/boehm_gc_allocator/issues/2
- (I can register each thread I create manually, which is what I am planning to do for the short term. But an interesting aspect of the BDW collector is that it should allow
Gc<T> to derive Send if the T itself is Sync, I think – something which historically Rust has deliberately not attempted to support, because the past GC’s were meant to be isolated to their own thread, but if BDW can do it, then it might be nice to provide a way to support it. Which definitely requires registration of the threads via std::thread::at_start.)
Here is my rough draft of its signature and documentation:
/// Enqueues a procedure to run when a thread is started.
///
/// Returns `Ok` if the handler was successfully registered, meaning
/// that the closure will be run sometime after each subsequent thread
/// is constructed, but before it starts running its associated main
/// routine. Returns `Err` to indicate that the closure could not be
/// registered, meaning that it is not scheduled to be run.
///
/// FIXME: Should we also provide some way to unregister a function?
#[unstable(feature = "TODO_unnamed_feature", reason = "recent API addition", issue="99999999")]
pub fn at_start<F: Fn() + Send + 'static>(f: F) -> ::result::Result<(), ()> {
sys_common::at_thread_start(f)
}
You can see my prototype implementation here:
Anyway, I largely am posting this here to find out if other people have example use cases for such a function. It would encourage me to actually jump into writing an RFC if I felt like there is demand for this.