@@ -4,15 +4,46 @@ package machine
44
55const numCPU = 2 // RP2040 and RP2350 both have 2 cores
66
7- // LockCore implementation for the cores scheduler.
7+ // LockCore sets the affinity for the current goroutine to the specified core.
8+ // This does not immediately migrate the goroutine; migration occurs at the next
9+ // scheduling point. See machine_rp2.go for full documentation.
10+ // Important: LockCore sets the affinity but does not immediately migrate the
11+ // goroutine to the target core. The actual migration happens at the next
12+ // scheduling point (e.g., channel operation, time.Sleep, or Gosched). After
13+ // that point, the goroutine will wait in the target core's queue if that core
14+ // is busy running another goroutine.
15+ //
16+ // To avoid potential blocking on a busy core, consider calling LockCore in an
17+ // init function before any other goroutines have started. This guarantees the
18+ // target core is available.
19+ //
20+ // This is useful for:
21+ // - Isolating time-critical operations to a dedicated core
22+ // - Improving cache locality for performance-sensitive code
23+ // - Exclusive access to core-local resources
24+ //
25+ // Warning: Pinning goroutines can lead to load imbalance. The goroutine will
26+ // wait in the specified core's queue even if other cores are idle. If a
27+ // long-running goroutine occupies the target core, LockCore may appear to
28+ // block indefinitely (until the next scheduling point on the target core).
29+ //
30+ // Valid core values are 0 and 1. Panics if core is out of range.
31+ //
32+ // Only available on RP2040 and RP2350 with the "cores" scheduler.
833func LockCore (core int ) {
934 if core < 0 || core >= numCPU {
1035 panic ("machine: core out of range" )
1136 }
1237 machineLockCore (core )
1338}
1439
15- // UnlockCore implementation for the cores scheduler.
40+ // UnlockCore unpins the calling goroutine, allowing it to run on any available core.
41+ // This undoes a previous call to LockCore.
42+ //
43+ // After calling UnlockCore, the scheduler is free to schedule the goroutine on
44+ // any core for automatic load balancing.
45+ //
46+ // Only available on RP2040 and RP2350 with the "cores" scheduler.
1647func UnlockCore () {
1748 machineUnlockCore ()
1849}
0 commit comments