workgroup

package
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for darwin/amd64

Overview

Package workgroup provides zero-cgo bindings for macOS os_workgroup parallel workloads and pthread QoS class pinning on Apple Silicon.

On Apple Silicon, macOS schedules threads onto performance (P) or efficiency (E) cores based on their requested QoSClass and Group membership. Because Go multiplexes goroutines onto OS threads, callers that use SetThreadQoS or Group.Join directly must pin the goroutine with runtime.LockOSThread and undo the change before calling runtime.UnlockOSThread. RunPinned handles locking, QoS set/restore, workgroup join/leave, and panic-safe cleanup automatically.

The zero value of Group is not usable; create one with New.

Basic usage:

g, err := workgroup.New("decode")
if err != nil {
	log.Fatal(err)
}
defer g.Close()

err = workgroup.RunPinned(ctx, workgroup.UserInteractive, g, func(ctx context.Context) error {
	// Runs on a locked OS thread at UserInteractive QoS in g.
	return nil
})
Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/tmc/apple/x/workgroup"
)

func main() {
	g, err := workgroup.New("example.decode")
	if err != nil {
		log.Fatal(err)
	}
	defer g.Close()

	err = workgroup.RunPinned(context.Background(), workgroup.UserInteractive, g, func(ctx context.Context) error {
		qos, _, err := workgroup.CurrentThreadQoS()
		if err != nil {
			return err
		}
		fmt.Println(qos)
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}
Output:
UserInteractive

Index

Examples

Constants

View Source
const MinRelativePriority = -15

MinRelativePriority is the lowest valid relative priority offset within a QoS class (QOS_MIN_RELATIVE_PRIORITY from <sys/qos.h>).

Variables

View Source
var ErrClosed = errors.New("workgroup: group closed")

ErrClosed is returned when an operation is attempted on a closed Group.

View Source
var ErrUnsupported = errors.New("workgroup: unsupported on this system")

ErrUnsupported reports that the required libSystem symbols are unavailable on this macOS version.

Functions

func RunPinned

func RunPinned(ctx context.Context, qos QoSClass, group *Group, fn func(context.Context) error) error

RunPinned executes fn on a locked OS thread (runtime.LockOSThread) with the requested QoS class and optional Group membership.

If qos is Unspecified, the thread's QoS class is left unchanged. If group is nil, no workgroup is joined. Before returning — including if fn panics — RunPinned leaves the workgroup, restores the thread's prior QoS class, and calls runtime.UnlockOSThread.

If ctx is already canceled, RunPinned returns ctx.Err() without locking the OS thread.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/tmc/apple/x/workgroup"
)

func main() {
	err := workgroup.RunPinned(context.Background(), workgroup.Background, nil, func(ctx context.Context) error {
		qos, _, err := workgroup.CurrentThreadQoS()
		if err != nil {
			return err
		}
		fmt.Println(qos)
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}
Output:
Background

func SetThreadQoS

func SetThreadQoS(class QoSClass, relPriority int) error

SetThreadQoS sets the requested QoS class and relative priority of the calling OS thread via pthread_set_qos_class_self_np.

relPriority must be in [MinRelativePriority, 0]. Callers should hold runtime.LockOSThread for the duration that the QoS class is needed.

Example
package main

import (
	"fmt"
	"log"
	"runtime"

	"github.com/tmc/apple/x/workgroup"
)

func main() {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	prev, rel, err := workgroup.CurrentThreadQoS()
	if err != nil {
		log.Fatal(err)
	}
	if prev == workgroup.Unspecified {
		prev, rel = workgroup.Default, 0
	}
	defer workgroup.SetThreadQoS(prev, rel)

	if err := workgroup.SetThreadQoS(workgroup.UserInitiated, 0); err != nil {
		log.Fatal(err)
	}
	got, _, err := workgroup.CurrentThreadQoS()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(got)
}
Output:
UserInitiated

Types

type Group

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

Group wraps a macOS parallel os_workgroup_t. Threads that join the same Group signal to the kernel scheduler that they are collaborating on a latency-sensitive parallel workload.

Group is safe for concurrent use by multiple goroutines. The zero value is not usable; create one with New.

func New

func New(name string) (*Group, error)

New creates a new parallel os_workgroup with the given label. Call Group.Close when the group is no longer needed.

func (*Group) Cancel

func (g *Group) Cancel()

Cancel marks the workgroup as canceled. Already-joined threads may still call Group.Leave, but subsequent Group.Join calls fail. Cancel is idempotent.

func (*Group) Canceled

func (g *Group) Canceled() bool

Canceled reports whether the workgroup has been canceled or closed.

func (*Group) Close

func (g *Group) Close() error

Close cancels and releases the underlying os_workgroup_t. Close is idempotent. Threads already joined via Group.Join may still safely call Group.Leave after Close.

func (*Group) Join

func (g *Group) Join() (*JoinToken, error)

Join adds the calling OS thread to the workgroup and returns a JoinToken that must be released with Group.Leave or JoinToken.Leave on the same OS thread. Callers must hold runtime.LockOSThread across Join and Leave.

func (*Group) Leave

func (g *Group) Leave(token *JoinToken) error

Leave removes the calling OS thread from the workgroup using a token previously returned by Group.Join. Leave is idempotent for a given token.

func (*Group) MaxParallelThreads

func (g *Group) MaxParallelThreads() int

MaxParallelThreads returns the system's recommended maximum number of parallel threads for this workgroup, or 0 if the group is closed or the query is unsupported.

func (*Group) Name

func (g *Group) Name() string

Name returns the label passed to New.

type JoinToken

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

JoinToken represents an OS thread's active membership in a Group. Pass it to Group.Leave (or call JoinToken.Leave) on the same OS thread that called Group.Join, in reverse order of nested joins.

func (*JoinToken) Leave

func (t *JoinToken) Leave() error

Leave removes the calling OS thread from the workgroup that issued t. Leave is idempotent.

type QoSClass

type QoSClass uint32

QoSClass represents a Darwin thread Quality of Service class (qos_class_t).

const (
	UserInteractive QoSClass = 0x21 // QOS_CLASS_USER_INTERACTIVE (P-cores, highest priority)
	UserInitiated   QoSClass = 0x19 // QOS_CLASS_USER_INITIATED (P-cores)
	Default         QoSClass = 0x15 // QOS_CLASS_DEFAULT
	Utility         QoSClass = 0x11 // QOS_CLASS_UTILITY (energy efficient)
	Background      QoSClass = 0x09 // QOS_CLASS_BACKGROUND (E-cores on Apple Silicon)
	Unspecified     QoSClass = 0x00 // QOS_CLASS_UNSPECIFIED
)

Darwin QoS classes from <sys/qos.h>.

func CurrentThreadQoS

func CurrentThreadQoS() (QoSClass, int, error)

CurrentThreadQoS returns the requested QoS class and relative priority of the calling OS thread via pthread_get_qos_class_np.

func (QoSClass) String

func (c QoSClass) String() string

String returns the human-readable name of c.

Jump to

Keyboard shortcuts

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