Documentation
¶
Overview ¶
Package goid tells goroutines apart. Go gives no way to ask for the number of the running goroutine, so there are two ways to tell them apart here:
- Token is what the framework compares on its hot paths (is this call from the UI goroutine? SPEC §13). It is the goroutine's number. On amd64 and arm64 it is read from the runtime's record of the running goroutine (its "g") by a function in Go assembly, in a couple of nanoseconds, at the offset that a calibration found on the first call; if the calibration finds no single offset, and on every other architecture, it is ID.
- ID is the number of the goroutine, parsed from the header of runtime.Stack. It walks the stack: about 1.4 µs at a shallow depth and 30 µs and more at 200 frames. It is for messages (the goroutine numbers that a WrongThread error names), not for a path that runs for every widget or render object.
The runtime never gives a goroutine number to a second goroutine, so a token can be kept and compared after its goroutine ended: no goroutine that starts later has it. (Until 1.9.3 the token was the address of the g, which the runtime does give to a new goroutine after the first one ended.)
Flutter: no counterpart (Dart has isolates, not goroutines)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ID ¶
func ID() uint64
ID is the number of the calling goroutine, read from runtime.Stack. It costs microseconds and grows with the depth of the stack; use Token to compare goroutines.
func Token ¶
func Token() uint64
Token is a value that tells the calling goroutine from every other goroutine, alive or ended. It is the goroutine's number, the one that ID and the header of runtime.Stack give, and the runtime never gives a number to a second goroutine, so a token can be kept and compared after its goroutine ended.
On amd64 and arm64 the number is read from the runtime's record of the goroutine by a function in Go assembly (getg_amd64.s, getg_arm64.s) in about two nanoseconds, at any stack depth. The offset of the number in that record is found once, by the first call (which also runs in a few microseconds, on the calling goroutine and on new ones, and is safe from any goroutine at any time); if that finds no single offset, every call is ID, which is exact and slow. It is never 0 except when runtime.Stack gives no number.
Flutter: no counterpart
Types ¶
This section is empty.