From 967a8567b827413a9ef7c21b82396ae76fc70f02 Mon Sep 17 00:00:00 2001 From: Radu Berinde Date: Thu, 25 Sep 2025 12:54:02 -0700 Subject: [PATCH] cockroach: runtime: add CurrentP Add a simple function that returns the index of the P the goroutine is running on. This will be used for efficient sharding of data structures. --- src/runtime/cockroach.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/runtime/cockroach.go diff --git a/src/runtime/cockroach.go b/src/runtime/cockroach.go new file mode 100644 index 00000000000000..5ff4399db75c33 --- /dev/null +++ b/src/runtime/cockroach.go @@ -0,0 +1,12 @@ +package runtime + +// CurrentP returns the index of the current P, between 0 and the current +// GOMAXPROCS. +// +// This can only be used as a best-effort facility; there is no guarantee that +// the goroutine still runs on the same P when this function returns. +func CurrentP() int { + // Note: preemption is not possible in-between these loads (there are no + // preemption-safe points). + return int(getg().m.p.ptr().id) +}