gil

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BreakerGILDropRequest is set when another thread wants the GIL.
	BreakerGILDropRequest uint32 = 1 << 0

	// BreakerSignalsPending is set when a Unix signal arrived.
	BreakerSignalsPending uint32 = 1 << 1

	// BreakerCallsPending is set when Pending.Add queued a callback.
	BreakerCallsPending uint32 = 1 << 2

	// BreakerAsyncException is set by PyThreadState_SetAsyncExc.
	BreakerAsyncException uint32 = 1 << 3

	// BreakerGCScheduled is set when the cycle collector wants to run.
	BreakerGCScheduled uint32 = 1 << 4

	// BreakerPleaseStop asks the eval loop to bail (interpreter shutdown).
	BreakerPleaseStop uint32 = 1 << 5

	// BreakerExplicitMerge is the free-threaded build's per-thread
	// QSBR merge bit. No-op in v0.6.
	BreakerExplicitMerge uint32 = 1 << 6

	// BreakerJITInvalidateCold is the JIT's cold-code invalidation
	// bit. No-op in v0.6 (no JIT).
	BreakerJITInvalidateCold uint32 = 1 << 7

	// BreakerEventsMask is the mask of all currently defined event
	// bits. CPython reserves the low 8 bits.
	BreakerEventsMask uint32 = (1 << 8) - 1
)
View Source
const PendingQueueSize = 32

PendingQueueSize is the bounded ring-buffer capacity.

CPython: Include/cpython/ceval.h NPENDINGCALLS (32)

Variables

View Source
var ErrPendingFull = errors.New("gil: pending-call queue full")

ErrPendingFull is returned by Add when the queue is full.

Functions

This section is empty.

Types

type Breaker

type Breaker struct {
	// contains filtered or unexported fields
}

Breaker holds the eval-loop poll bits. One per Thread.

func (*Breaker) Clear

func (b *Breaker) Clear(bit uint32)

Clear turns off the named bit.

CPython: Include/internal/pycore_ceval.h _Py_unset_eval_breaker_bit

func (*Breaker) IsSet

func (b *Breaker) IsSet(bit uint32) bool

IsSet reports whether the named bit is on.

func (*Breaker) Load

func (b *Breaker) Load() uint32

Load reads the bitfield. Hot path.

func (*Breaker) Set

func (b *Breaker) Set(bit uint32)

Set turns on the named bit. Safe from any goroutine.

CPython: Include/internal/pycore_ceval.h _Py_set_eval_breaker_bit

type GIL

type GIL struct {
	// contains filtered or unexported fields
}

GIL is the per-interpreter execution lock.

func New

func New() *GIL

New returns an unlocked GIL.

func (*GIL) Acquire

func (g *GIL) Acquire(ts holderID)

Acquire blocks until ts holds the GIL. Pass any opaque per-thread pointer for ts; the GIL only uses it for ownership checks.

CPython: Python/ceval_gil.c take_gil

func (*GIL) DropRequested

func (g *GIL) DropRequested() bool

DropRequested reports whether another goroutine asked the holder to drop. Cheap atomic read.

func (*GIL) Holder

func (g *GIL) Holder() holderID

Holder returns the current holder (or nil). Read with the lock held; useful only for diagnostics.

func (*GIL) Release

func (g *GIL) Release(ts holderID)

Release drops the GIL. Mismatched releases panic to surface lock inversion bugs early.

CPython: Python/ceval_gil.c drop_gil

func (*GIL) RequestDrop

func (g *GIL) RequestDrop()

RequestDrop marks the GIL as wanting to be dropped at the next poll point. The eval loop checks DropRequested on each iteration and releases voluntarily when set.

CPython: Python/ceval_gil.c COMPUTE_EVAL_BREAKER (gil-drop bit)

func (*GIL) SetSwitchInterval

func (g *GIL) SetSwitchInterval(d time.Duration)

SetSwitchInterval updates the cooperative-yield interval.

CPython: Python/sysmodule.c sys_setswitchinterval

func (*GIL) SwitchInterval

func (g *GIL) SwitchInterval() time.Duration

SwitchInterval returns the cooperative-yield interval.

CPython: Python/sysmodule.c sys_getswitchinterval

type Pending

type Pending struct {
	// contains filtered or unexported fields
}

Pending is a per-interpreter ring buffer of callbacks.

func (*Pending) Add

func (p *Pending) Add(fn PendingFunc) error

Add enqueues fn. Returns ErrPendingFull if the queue is at capacity. Existing entries are not lost on overflow.

CPython: Python/ceval_gil.c _Py_AddPendingCall

func (*Pending) Drain

func (p *Pending) Drain() error

Drain runs queued callbacks in FIFO order until the queue is empty or one of them errors. The first error stops draining and is returned; remaining callbacks stay queued.

CPython: Python/ceval_gil.c _Py_MakePendingCalls

func (*Pending) Len

func (p *Pending) Len() int

Len returns the number of queued callbacks.

type PendingFunc

type PendingFunc func() error

PendingFunc is a callback to run on the eval-loop thread.

type SignalBridge

type SignalBridge struct {
	// contains filtered or unexported fields
}

SignalBridge wires os/signal delivery to a Breaker and a Pending queue. One per interpreter.

func NewSignalBridge

func NewSignalBridge(b *Breaker, p *Pending) *SignalBridge

NewSignalBridge constructs an unstarted bridge.

func (*SignalBridge) Handle

func (s *SignalBridge) Handle(sig os.Signal, fn PendingFunc)

Handle registers fn to run (under the GIL, at the next poll point) when sig arrives. Replaces any previous handler for sig.

CPython: Modules/signalmodule.c signal_signal_impl

func (*SignalBridge) Stop

func (s *SignalBridge) Stop()

Stop tears the bridge down. Any in-flight signal that has been dispatched to the channel still triggers its handler.

func (*SignalBridge) Unhandle

func (s *SignalBridge) Unhandle(sig os.Signal)

Unhandle stops receiving sig.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL