# RFRFC : std thread at\_start(callback)

**URL:** <https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877>\
**Category:** libs\
**Created:** [November 7, 2015, 12:27pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877 "2015-11-07T12:27:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pnkfelix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pnkfelix/32/110_2.png) [@pnkfelix](https://internals.rust-lang.org/u/pnkfelix)\
**Post date:** [November 7, 2015, 12:27pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/1 "2015-11-07T12:27:21Z")

</div>

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](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:

```rust
/// 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:

[https://github.com/pnkfelix/rust/commit/bfff1821c26c3900b0abab531ff1b5f59260fcd3](https://github.com/pnkfelix/rust/commit/bfff1821c26c3900b0abab531ff1b5f59260fcd3)

* * *

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.

---

<div class="post-metadata">

**Author:** ![tomaka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tomaka/32/2842_2.png) [@tomaka](https://internals.rust-lang.org/u/tomaka)\
**Post date:** [November 7, 2015, 2:53pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/2 "2015-11-07T14:53:31Z")

</div>

I’m a bit reluctant, because this would be a feature that Rust adds over the operating system. Kind of like a new runtime.

I can see situations where Rust code is executed in threads not created by `thread::spawn` (for example using a Rust library from C, or inside a callback), in which case the functions registered with `at_start` haven’t been executed.

---

<div class="post-metadata">

**Author:** ![swgillespie](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/swgillespie/32/352_2.png) [@swgillespie](https://internals.rust-lang.org/u/swgillespie)\
**Post date:** [November 7, 2015, 6:17pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/3 "2015-11-07T18:17:19Z")

</div>

Spawning a Rust thread already does strictly more than `pthread_create` or the like, doesn’t it? Even today, can’t Rust threads that weren’t created by `thread::spawn` run without the stack overflow handler in place? ([here](https://github.com/rust-lang/rust/blob/219eca11b044de3644b3e9101124513c1a842b09/src/libstd/sys/common/thread.rs#L20 "link")). I understand wanting to make Rust threads as “pure” as possible, but we already have the hazard of stack overflow segfaults when running Rust on a thread not owned by Rust.

The main problem encountered when integrating Rust into the BDWGC, as @pnkfelix noted, is that BDWGC expects every thread that will be allocating to be registered with the collector. Since things like the test harness spawn threads and immediately allocate without ever calling into user code, there’s no way to inform the collector that we’re going to be allocating on a thread until we’ve already allocated. The BDWGC aborts if a collection is triggered on a thread that hasn’t been registered, and any allocation can trigger it.

I don’t usually participate in RFC-like discussions so my knowledge of Rust internals is limited, but I think that it would useful for runtime-like library features - in particular, GC-aware allocators or virtual machines - to be able to keep track of what Rust threads are currently running. As far as I can tell, there’s no way to do this on Linux. A mechanism like this would allow an allocator to require the registering of all spawned threads in a way that’s completely transparent to the user of the allocator.

---

<div class="post-metadata">

**Author:** ![Ericson2314](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ericson2314/32/246_2.png) [@Ericson2314](https://internals.rust-lang.org/u/Ericson2314)\
**Post date:** [November 7, 2015, 6:29pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/4 "2015-11-07T18:29:03Z")

</div>

@swgillespie maybe we need to interpose `pthread_create` or similar.

I don’t even see that is too bad, as I rather have something more static like a lang item anyways. That would also address @tomaka 's concerns. Only issue is it will be a pain to implement nicely in build-system.

---

<div class="post-metadata">

**Author:** ![swgillespie](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/swgillespie/32/352_2.png) [@swgillespie](https://internals.rust-lang.org/u/swgillespie)\
**Post date:** [November 7, 2015, 7:12pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/5 "2015-11-07T19:12:12Z")

</div>

@Ericson2314 Yeah, that’s what the `libgc` C/C++ API does - it #defines `pthread_create` to point to a function that creates the thread and registers it with the collector.

That’s an interesting idea - maybe we could have something like our allocator API, like `__rust_create_thread`? Could be interesting.

---

<div class="post-metadata">

**Author:** ![tamird](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tamird/32/14099_2.png) [@tamird](https://internals.rust-lang.org/u/tamird)\
**Post date:** [November 8, 2015, 3:13am UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/6 "2015-11-08T03:13:39Z")

</div>

> [@pnkfelix](#):
>
> /// FIXME: Should we also provide some way to unregister a function?

This could possibly return a closure that unregisters the function, in the success case.

---

<div class="post-metadata">

**Author:** ![abonander](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/abonander/32/1284_2.png) [@abonander](https://internals.rust-lang.org/u/abonander)\
**Post date:** [November 8, 2015, 3:33am UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/7 "2015-11-08T03:33:07Z")

</div>

Is there a problem with writing a wrapper that registers the thread on the first call to the allocation function?

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [November 9, 2015, 12:20pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/8 "2015-11-09T12:20:27Z")

</div>

~~[quote=“logician, post:7, topic:2877, full:true”] Is there a problem with writing a wrapper that registers the thread on the first call to the allocation function? [/quote]~~

~~That’s what I’d do. The GC-crate should offer its own `spawn` function. I’m fairly sure the type system can express a special `Send` type, to make sure that you don’t send GCable types across normally spawned threads. Here’s something along which lines it should work.~~

~~```rust impl !Send for Gc {} unsafe impl GSend for T where T: Send {} unsafe impl GSend for Gc {}~~

struct Hack\<RES: Send + 'static, F: GSend + FnOnce() -\> RES + 'static\>(F);

unsafe impl\<RES: Send + 'static, F: GSend + FnOnce() -\> RES + 'static\> Send for Hack\<RES, F\> {}

fn spawn\<F, T\>(f: F) -\> JoinHandle where F: FnOnce() -\> T, F: GSend + 'static, T: Send + 'static { let hack = Hack(f); // init gc here std::thread::spawn(move || (hack.0)()) }

```rust
<strike>Works in the [Playground](http://is.gd/ZaMGAl)</strike>

UPDATE: This doesn't work with the regular std-threading-tools like channels or mutexes... :( (see http://is.gd/YYz4xW for the attempt)
```

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [November 9, 2015, 2:13pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/9 "2015-11-09T14:13:02Z")

</div>

> [@abonander](#):
>
> Is there a problem with writing a wrapper that registers the thread on the first call to the allocation function?

I think this is why @pnkfelix mentioned making `Gc<T>` sendable -- in that case, you might wind up with GC'd data even if you yourself did not do any allocation.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [November 9, 2015, 2:13pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/10 "2015-11-09T14:13:44Z")

</div>

This seems fairly analogous to `atexit`. Of course, there is the obvious downside that threads spawned via some other means (e.g., direct calls to `pthread_create`) will not (and cannot) be automatically registered.

---

<div class="post-metadata">

**Author:** ![pnkfelix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pnkfelix/32/110_2.png) [@pnkfelix](https://internals.rust-lang.org/u/pnkfelix)\
**Post date:** [November 9, 2015, 2:14pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/11 "2015-11-09T14:14:38Z")

</div>

> Is there a problem with writing a wrapper that registers the thread on the first call to the allocation function?

Off the top of my head, this would require some facility for introspecting the address of the cold-end of the stack for the thread. (After all, the first allocation call may occur somewhere deep in the threads activity.)

We do not currently offer such a facility; we might be able to add it. (@alexcrichton warned me that not all platforms provide the primitives we would need to do this accurately for all targets, but for such targets, the system thread\_start code could initialize a thread-local with a a conservative approximation.

Registering the thread on startup allows us to side-step adding such introspection facility. (Having said that, I myself am not opposed to adding such introspection.)

* * *

Update: (And of course there is the issue that you have to give up `impl<T:Send+Sync> Send for Gc<T>` if you do this, as @nikomatsakis already reminded us above.)

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [November 9, 2015, 2:57pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/12 "2015-11-09T14:57:08Z")

</div>

is there any downside for a thread to have the gc-framework enabled if it doesn’t own/reference any gc-able types? (except the additional cost at the spawning of the thread, I mean while the thread is running)

---

<div class="post-metadata">

**Author:** ![pnkfelix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pnkfelix/32/110_2.png) [@pnkfelix](https://internals.rust-lang.org/u/pnkfelix)\
**Post date:** [November 9, 2015, 2:58pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/13 "2015-11-09T14:58:53Z")

</div>

> [@ker](#):
>
> is there any downside for a thread to have the gc-framework enabled if it doesn't own/reference any gc-able types? (except the additional cost at the spawning of the thread, I mean while the thread is running)

Well, for BDW, every registered thread will get paused by the collector when it does a stop-world for collection.

From what I read, It was precisely this drawback that led the people behind BDW away from automatically inferring the set of threads and instead started requiring each thread to register itself. This way threads that are not doing GC things do not get interrupted by the GC.

* * *

(Of course this argument can be used to argue against the using the `std::thread::at_start` API to auto-register threads, since such a system would then fall victim to the above problem. But I am willing to say "there are trade-offs -- let the end-developer decide what they want." Also, it would be interesting to figure out if the `std::thread::at_start` API can be generalized in some way so that each callback is only run from the tree to threads that are spawned from the original parent that registered that callback. That would allow for some amount of modular reasoning.)

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [November 9, 2015, 3:06pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/14 "2015-11-09T15:06:45Z")

</div>

so there are 4 alternatives:

1. `at_start` callback (slowdown for all threads)
2. rewrite every sync primitive for gc (lets not do that xD)
3. generic Sync + Send traits so the sync primitives can be re-used (not even sure if that would work)
4. check a lazy thread local at every alloc/dealloc call of the gc-crate (slowdown for every single allocation, not sure how much that is)

> [@pnkfelix](#):
>
> Also, it would be interesting to figure out if the std:🧵:at\_start API can be generalized in some way so that each callback is only run from the tree to threads that are spawned from the original parent that registered that callback. That would allow for some amount of modular reasoning.

You could still send a `Gc<T>` upwards in the tree until you are past the `at_start` setting

---

<div class="post-metadata">

**Author:** ![pnkfelix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pnkfelix/32/110_2.png) [@pnkfelix](https://internals.rust-lang.org/u/pnkfelix)\
**Post date:** [November 9, 2015, 7:36pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/15 "2015-11-09T19:36:44Z")

</div>

> [@ker](#):
>
> You could still send a Gc\<T\> upwards in the tree until you are past the at\_start setting

I don't quite understand what you are saying here: there is no frame above the one established for the thread's start.

(The `at_start` call itself is on the `main` thread, not the other threads that are spawned off of it. One _does_ need to register the `main` thread itself at the very beginning of the program run.)

Update: Oh, oh, you're referring to the tree of threads, not the tree of procedure calls. Never mind, I understand now. Yes, the thread-tree use case for `at_start` is probably not useful for a sendable `Gc<T>` -- its more something I was considering for other use cases...

---

<div class="post-metadata">

**Author:** ![pnkfelix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pnkfelix/32/110_2.png) [@pnkfelix](https://internals.rust-lang.org/u/pnkfelix)\
**Post date:** [November 9, 2015, 7:44pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/16 "2015-11-09T19:44:27Z")

</div>

> [@pnkfelix](#):
>
> where one registers each thread that may call into the GC

By the way, a "call into the GC" in this context includes allocation of roots, which, in BDW as I imagine it would work in Rust, means _all_ allocations.

(That is, even if you never allocate an object on the GC heap itself, BDW will _still_ complain if you allocate other state on an unregistered thread. This is probably a useful safe guard for the library, but it does complicate certain coding patterns.)

---

<div class="post-metadata">

**Author:** ![pnkfelix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pnkfelix/32/110_2.png) [@pnkfelix](https://internals.rust-lang.org/u/pnkfelix)\
**Post date:** [November 9, 2015, 7:52pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/17 "2015-11-09T19:52:40Z")

</div>

> [@ker](#):
>
> check a lazy thread local at every alloc/dealloc call of the gc-crate (slowdown for every single allocation, not sure how much that is)

I think @nikomatsakis 's point above against this still holds, no? Did I miss some counter-argument about this approach missing potential roots (sent to a thread that itself does no allocation? Am I being silly assuming such things exist?)

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [November 9, 2015, 10:48pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/18 "2015-11-09T22:48:30Z")

</div>

> [@pnkfelix](#):
>
> Did I miss some counter-argument about this approach missing potential roots (sent to a thread that itself does no allocation? Am I being silly assuming such things exist?)

Well, it's "sent to a thread that has not YET done allocation", which seems like something that will clearly exist.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [November 10, 2015, 9:36am UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/19 "2015-11-10T09:36:28Z")

</div>

> [@pnkfelix](#):
>
> I think @nikomatsakis 's point above against this still holds, no? Did I miss some counter-argument about this approach missing potential roots

No, I misunderstood.

Also it's not a question of missing roots. Since the allocator was replaced completely, even for normal rust allocations (still manually managed, but known to the gc), all of this is moot anyway. I'd wager your test fails with the same error if you simply allocate a `Box`.

If you use the Gc crate, all threads need to register with the Gc, OR there needs to be a way to use different allocators and make sure Gc-objects can't ever be put into an object allocated by a different allocator.

If you can create an instance of the following object with the normal allocator instead of the Gc allocator

```rust
struct EvilType {
    blub: Option<Gc<i32>>,
}

```

then you could send this object to a gc-thread, that thread could put a Gc in there and you quickly get a dangling reference once the gc collects the object you are pointing to.

---

<div class="post-metadata">

**Author:** ![whitequark](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/whitequark/32/12256_2.png) [@whitequark](https://internals.rust-lang.org/u/whitequark)\
**Post date:** [July 15, 2017, 5:53pm UTC](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877/20 "2017-07-15T17:53:46Z")

</div>

> [@swgillespie](#):
>
> Even today, can't Rust threads that weren't created by thread::spawn run without the stack overflow handler in place?

All that does is [shows debug output](https://github.com/rust-lang/rust/blob/219eca11b044de3644b3e9101124513c1a842b09/src/libstd/sys/unix/stack_overflow.rs#L85) so I would say it doesn't count. It's a debugging feature that can be completely replaced by a small gdb script, not a fundamental part of language or runtime.

[Next page](https://internals.rust-lang.org/t/rfrfc-std-thread-at-start-callback/2877.md?page=2)
