Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
proposal: runtime: add ForEachOSThread and LockThreadAndDescendentsExclusive #23160
This is a proposal for
package runtime /* ForEachOSThread calls the provided closure on every operating system thread in the current process. The closure is executed with Go-level preemption disabled, guaranteeing that it has exclusive use of the corresponding OS thread while it is running. Calls to ForEachOSThread are serialized against each other within a process. Panics if it cannot fulfill its postconditions. */ func ForEachOSThread(func () ()) ()
First, what problem is this intended to solve?
We aren't going to run arbitrary user code with preemption disabled. That is a footgun. We could perhaps run the code with
Locking a set of goroutines to a single thread is problematic because it's a considerable change to the scheduler. The support for locking a single goroutine to a thread appears all through the scheduler. Being able to lock a set of goroutines would presumably do the same. We would need a very good reason to add that level of complexity for a case that would presumably be very rarely used.
There's no way to implement ForEachOSThread. What if the thread is off doing something that can't be interrupted? More generally, we try hard to keep scheduling details from the programs, so they can't depend on them. This walks quite far in the wrong direction.
LockThreadAndDescendentsExclusive is at least doable but very complex, and we already have a very hard time keeping LockOSThread (single-goroutine version) working well.