dispatch

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package dispatch provides Go bindings for the Dispatch framework.

Execute code concurrently on multicore hardware by submitting work to dispatch queues managed by the system.

Dispatch, also known as Grand Central Dispatch (GCD), contains language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in macOS, iOS, watchOS, and tvOS.

Notes

  • High-level wrappers (Queue, Group, Semaphore) are provided alongside generated interop types.

Package dispatch provides Go bindings for Apple's Grand Central Dispatch (GCD).

GCD is a low-level API for managing concurrent operations. This package exposes Go-idiomatic wrappers (exported, like QueueCreate). Raw generated C bindings are package-private and used internally by these wrappers.

Go Runtime Compatibility

GCD and Go's runtime both manage threads. Key considerations:

  • Use dispatch_async_f (not dispatch_async) for Go function callbacks
  • Go closures passed to GCD will execute on GCD-managed threads
  • Use sync.WaitGroup or channels to coordinate with Go code
  • For main thread work on macOS, use the main dispatch queue

Basic Usage

queue := dispatch.GetGlobalQueue(dispatch.QOSDefault)
queue.Async(func() {
    // Work executes on a GCD thread
})

Groups for Coordination

group := dispatch.GroupCreate()
queue := dispatch.GetGlobalQueue(dispatch.QOSDefault)
for i := 0; i < 10; i++ {
    group.Async(queue, func() {
        // Parallel work
    })
}
group.Wait(dispatch.TimeForever)

Callback Memory

Each unique dispatch function (Async, Sync, BarrierAsync, After, Group.Async, Group.Notify) uses a single permanent purego callback. Work closures are passed via a context map, not via new callbacks. This means an unlimited number of dispatches can be performed without exhausting purego's 2000-callback limit.

Apple Framework Interop

When passing dispatch queues to Apple frameworks, prefer the main dispatch queue or a nil/default queue to let the framework manage thread affinity.

Code generated from Apple documentation. DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(when Dispatch_time_t, queue Queue, work func())

After schedules work to execute after a delay.

func Apply added in v0.5.3

func Apply(iterations int, queue Queue, work func(iteration int))

Apply submits work for parallel execution across the given number of iterations.

func ApplyAuto added in v0.5.3

func ApplyAuto(iterations int, work func(iteration int))

ApplyAuto submits work for parallel execution using libdispatch's automatic queue selection.

func DataGetSize added in v0.5.3

func DataGetSize(d Data) int

DataGetSize returns the number of bytes in the data object.

func DataToBytes added in v0.5.3

func DataToBytes(d Data) []byte

DataToBytes extracts bytes from a dispatch data object. Returns a copy the caller owns.

func Main

func Main()

Main enters the main dispatch event loop and does not return.

func Read added in v0.5.3

func Read(fd int, length int, queue Queue, handler func(data Data, err error))

Read schedules a one-shot asynchronous read from a file descriptor.

func ReadBytes added in v0.5.3

func ReadBytes(fd int, length int, queue Queue, handler func(data []byte, err error))

ReadBytes schedules a one-shot asynchronous read and copies the result into a Go slice.

func ReadFD added in v0.5.3

func ReadFD(fd int, length int, queue Queue, handler func(data []byte, err error))

ReadFD is a convenience function that reads up to length bytes from a file descriptor using a stream IO channel. The handler receives the accumulated bytes when reading is complete.

func Write added in v0.5.3

func Write(fd int, data Data, queue Queue, handler func(remaining Data, err error))

Write schedules a one-shot asynchronous write to a file descriptor.

func WriteBytes added in v0.5.3

func WriteBytes(fd int, buf []byte, queue Queue, handler func(remaining []byte, err error))

WriteBytes schedules a one-shot asynchronous write from a Go slice.

func WriteFD added in v0.5.3

func WriteFD(fd int, buf []byte, queue Queue, handler func(remaining []byte, err error))

WriteFD is a convenience function that writes bytes to a file descriptor.

Types

type AutoreleaseFrequency added in v0.5.3

type AutoreleaseFrequency uintptr

AutoreleaseFrequency controls autorelease pool behavior for queue attrs.

const (
	AutoreleaseFrequencyInherit  AutoreleaseFrequency = 0
	AutoreleaseFrequencyWorkItem AutoreleaseFrequency = 1
	AutoreleaseFrequencyNever    AutoreleaseFrequency = 2
)

type Data

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

Data wraps a dispatch data object.

func DataCreate added in v0.5.3

func DataCreate(buf []byte) Data

DataCreate creates a dispatch data object from a Go byte slice. The buffer is copied by libdispatch (NULL destructor = copy semantics), so the Go slice may be GC'd after this call.

func DataCreateConcat added in v0.5.3

func DataCreateConcat(a, b Data) Data

DataCreateConcat concatenates two data objects.

func DataEmpty added in v0.5.3

func DataEmpty() Data

DataEmpty returns the global empty dispatch data singleton.

func DataFromHandle

func DataFromHandle(handle uintptr) Data

DataFromHandle creates a Data from a raw handle.

func (Data) Bytes added in v0.5.3

func (d Data) Bytes() []byte

Bytes extracts a copy of the bytes from the data object.

func (Data) Handle

func (d Data) Handle() uintptr

Handle returns the underlying dispatch data handle.

func (Data) Len added in v0.5.3

func (d Data) Len() int

Len returns the number of bytes.

type DispatchWalltime added in v0.5.1

type DispatchWalltime uint
const (
	// DISPATCH_WALLTIME_NOW: The current time.
	DISPATCH_WALLTIME_NOW DispatchWalltime = 0
)

func (DispatchWalltime) String added in v0.5.1

func (e DispatchWalltime) String() string

type Dispatch_block_t

type Dispatch_block_t = func()

Dispatch_block_t is the prototype of blocks submitted to dispatch queues, which take no arguments and have no return value.

See: https://developer.apple.com/documentation/Dispatch/dispatch_block_t

type Dispatch_data_applier_t

type Dispatch_data_applier_t = func(objc.ID, uint32, unsafe.Pointer, uint32) bool

Dispatch_data_applier_t is a block to invoke for every contiguous memory region in a data object.

See: https://developer.apple.com/documentation/Dispatch/dispatch_data_applier_t

type Dispatch_fd_t

type Dispatch_fd_t = int

Dispatch_fd_t is a file descriptor used for I/O operations.

See: https://developer.apple.com/documentation/Dispatch/dispatch_fd_t

type Dispatch_function_t

type Dispatch_function_t = unsafe.Pointer

Dispatch_function_t is the prototype of functions submitted to dispatch queues.

See: https://developer.apple.com/documentation/Dispatch/dispatch_function_t

type Dispatch_io_close_flags_t

type Dispatch_io_close_flags_t = uint

Dispatch_io_close_flags_t is additional flags to use when closing an I/O channel.

See: https://developer.apple.com/documentation/Dispatch/dispatch_io_close_flags_t

type Dispatch_io_handler_t

type Dispatch_io_handler_t = func(bool, objc.ID, int)

Dispatch_io_handler_t is a handler block used to process operations on a dispatch I/O channel.

See: https://developer.apple.com/documentation/Dispatch/dispatch_io_handler_t

type Dispatch_io_interval_flags_t

type Dispatch_io_interval_flags_t = uint

Dispatch_io_interval_flags_t is the desired delivery behavior for interval events.

See: https://developer.apple.com/documentation/Dispatch/dispatch_io_interval_flags_t

type Dispatch_once_t

type Dispatch_once_t = int

Dispatch_once_t is a predicate for use with the `dispatch_once` function.

See: https://developer.apple.com/documentation/Dispatch/dispatch_once_t

type Dispatch_qos_class_t

type Dispatch_qos_class_t = uint32

Dispatch_qos_class_t is quality-of-service classes that specify the priorities for executing tasks.

See: https://developer.apple.com/documentation/Dispatch/dispatch_qos_class_t

type Dispatch_queue_concurrent_t

type Dispatch_queue_concurrent_t = dispatch_queue_t

Dispatch_queue_concurrent_t is a dispatch queue that executes tasks concurrently and in any order, respecting any barriers that may be in place.

See: https://developer.apple.com/documentation/Dispatch/dispatch_queue_concurrent_t

type Dispatch_queue_global_t

type Dispatch_queue_global_t = dispatch_queue_t

Dispatch_queue_global_t is a dispatch queue that executes tasks concurrently using threads from the global thread pool.

See: https://developer.apple.com/documentation/Dispatch/dispatch_queue_global_t

type Dispatch_queue_main_t

type Dispatch_queue_main_t = dispatch_queue_t

Dispatch_queue_main_t is a dispatch queue that is bound to the app’s main thread and executes tasks serially on that thread.

See: https://developer.apple.com/documentation/Dispatch/dispatch_queue_main_t

type Dispatch_queue_priority_t

type Dispatch_queue_priority_t = int

Dispatch_queue_priority_t is the execution priority for tasks in a global concurrent queue.

See: https://developer.apple.com/documentation/Dispatch/dispatch_queue_priority_t

type Dispatch_queue_serial_t

type Dispatch_queue_serial_t = dispatch_queue_t

Dispatch_queue_serial_t is a dispatch queue that executes tasks serially in first-in, first-out (FIFO) order.

See: https://developer.apple.com/documentation/Dispatch/dispatch_queue_serial_t

type Dispatch_source_mach_recv_flags_t

type Dispatch_source_mach_recv_flags_t = uint

Dispatch_source_mach_recv_flags_t is mach receive-right flags.

See: https://developer.apple.com/documentation/Dispatch/dispatch_source_mach_recv_flags_t

type Dispatch_source_mach_send_flags_t

type Dispatch_source_mach_send_flags_t = uint

Dispatch_source_mach_send_flags_t is mach send-right flags.

See: https://developer.apple.com/documentation/Dispatch/dispatch_source_mach_send_flags_t

type Dispatch_source_memorypressure_flags_t

type Dispatch_source_memorypressure_flags_t = uint

Dispatch_source_memorypressure_flags_t is memory pressure events.

See: https://developer.apple.com/documentation/Dispatch/dispatch_source_memorypressure_flags_t

type Dispatch_source_proc_flags_t

type Dispatch_source_proc_flags_t = uint

Dispatch_source_proc_flags_t is events related to a process.

See: https://developer.apple.com/documentation/Dispatch/dispatch_source_proc_flags_t

type Dispatch_source_timer_flags_t

type Dispatch_source_timer_flags_t = uint

Dispatch_source_timer_flags_t is flags to use when configuring a timer dispatch source.

See: https://developer.apple.com/documentation/Dispatch/dispatch_source_timer_flags_t

type Dispatch_source_type_t added in v0.5.3

type Dispatch_source_type_t = dispatch_source_type_t

Dispatch_source_type_t aliases the unexported dispatch_source_type_t so the pipeline-generated _dispatch_source_create var can reference it.

type Dispatch_source_vnode_flags_t

type Dispatch_source_vnode_flags_t = uint

Dispatch_source_vnode_flags_t is events involving a change to a file system object.

See: https://developer.apple.com/documentation/Dispatch/dispatch_source_vnode_flags_t

type Dispatch_time_t

type Dispatch_time_t = uint64

Dispatch_time_t is an abstract representation of time.

See: https://developer.apple.com/documentation/Dispatch/dispatch_time_t

const (
	// TimeNow represents the current time (no delay).
	TimeNow Dispatch_time_t = 0

	// TimeForever represents an infinite timeout.
	TimeForever Dispatch_time_t = ^Dispatch_time_t(0)
)

Time constants for dispatch operations.

func TimeFromNow

func TimeFromNow(nanoseconds int64) Dispatch_time_t

TimeFromNow returns a dispatch time representing now + the given nanoseconds.

type Dispatch_workloop_t

type Dispatch_workloop_t = dispatch_queue_t

Dispatch_workloop_t is a dispatch queue that prioritizes the execution of tasks based on their quality-of-service level.

See: https://developer.apple.com/documentation/Dispatch/dispatch_workloop_t

type Group

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

Group wraps a dispatch group for coordinating multiple async operations.

func GroupCreate

func GroupCreate() Group

GroupCreate creates a new dispatch group for coordinating async operations.

func GroupFromHandle

func GroupFromHandle(handle uintptr) Group

GroupFromHandle creates a Group from a raw handle.

func (Group) Async

func (g Group) Async(queue Queue, work func())

Async submits work to a queue as part of the group.

func (Group) Enter

func (g Group) Enter()

Enter manually increments the group's task count. Each Enter must be balanced with a Leave.

func (Group) Handle

func (g Group) Handle() uintptr

Handle returns the underlying dispatch group handle for advanced usage.

func (Group) Leave

func (g Group) Leave()

Leave manually decrements the group's task count.

func (Group) Notify

func (g Group) Notify(queue Queue, work func())

Notify schedules work to execute when all current group work completes.

func (Group) Wait

func (g Group) Wait(timeout Dispatch_time_t) bool

Wait blocks until all work in the group completes or timeout expires. Returns true if all work completed, false if timeout expired.

type IO

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

IO wraps a dispatch I/O channel.

func IOCreate added in v0.5.3

func IOCreate(typ IOType, fd int, queue Queue, cleanup func(err error)) IO

IOCreate creates a dispatch IO channel for the given file descriptor. The cleanup handler is called when the channel is closed.

func IOCreateWithIO added in v0.5.3

func IOCreateWithIO(typ IOType, parent IO, queue Queue, cleanup func(err error)) IO

IOCreateWithIO creates a dispatch IO channel derived from an existing channel.

func IOCreateWithPath added in v0.5.3

func IOCreateWithPath(typ IOType, path string, oflag int, mode uint32, queue Queue, cleanup func(err error)) IO

IOCreateWithPath creates a dispatch IO channel for the given file path.

func IOFromHandle

func IOFromHandle(handle uintptr) IO

IOFromHandle creates an IO from a raw handle.

func (IO) Barrier added in v0.5.3

func (i IO) Barrier(fn func())

Barrier schedules a barrier on the IO channel. The barrier function executes when all prior read/write operations complete.

func (IO) Close added in v0.5.3

func (i IO) Close(flags IOCloseFlags)

Close closes the IO channel. Pass IOStop to cancel outstanding operations.

func (IO) Descriptor added in v0.5.3

func (i IO) Descriptor() int

Descriptor returns the file descriptor associated with the channel.

func (IO) Handle

func (i IO) Handle() uintptr

Handle returns the underlying dispatch I/O handle.

func (IO) Read added in v0.5.3

func (i IO) Read(offset int64, length uint64, queue Queue, handler func(done bool, data Data, err error))

Read reads data from the channel at the given offset. The handler is called one or more times as data becomes available. When done is true, all data has been read (or an error occurred).

func (IO) SetHighWater added in v0.5.3

func (i IO) SetHighWater(bytes uint64)

SetHighWater sets the high-water mark for IO operations.

func (IO) SetInterval added in v0.5.3

func (i IO) SetInterval(interval time.Duration, flags IOIntervalFlags)

SetInterval sets the delivery interval for IO handler callbacks.

func (IO) SetLowWater added in v0.5.3

func (i IO) SetLowWater(bytes uint64)

SetLowWater sets the low-water mark for IO operations.

func (IO) Write added in v0.5.3

func (i IO) Write(offset int64, data Data, queue Queue, handler func(done bool, remaining Data, err error))

Write writes data to the channel at the given offset.

type IOCloseFlags added in v0.5.3

type IOCloseFlags uint

IOCloseFlags for dispatch_io_close.

const (
	IOStop IOCloseFlags = 0x1
)

type IOIntervalFlags added in v0.5.3

type IOIntervalFlags uint

IOIntervalFlags for dispatch_io_set_interval.

const (
	IOStrictInterval IOIntervalFlags = 0x1
)

type IOType added in v0.5.3

type IOType uint

IOType identifies the type of a dispatch IO channel.

const (
	IOStream IOType = 0
	IORandom IOType = 1
)

type MemoryPressureFlags added in v0.5.3

type MemoryPressureFlags uintptr

MemoryPressureFlags for DISPATCH_SOURCE_TYPE_MEMORYPRESSURE sources.

const (
	MemPressureNormal   MemoryPressureFlags = 0x01
	MemPressureWarn     MemoryPressureFlags = 0x02
	MemPressureCritical MemoryPressureFlags = 0x04
)

type ProcFlags added in v0.5.3

type ProcFlags uintptr

ProcFlags for DISPATCH_SOURCE_TYPE_PROC sources.

const (
	ProcExit   ProcFlags = 0x80000000
	ProcFork   ProcFlags = 0x40000000
	ProcExec   ProcFlags = 0x20000000
	ProcSignal ProcFlags = 0x08000000
)

type QOS

type QOS uint32

QoS (Quality of Service) classes for dispatch queues. Higher QoS classes get more system resources.

const (
	// QOSUnspecified means no explicit quality of service class.
	QOSUnspecified QOS = 0

	// QOSUserInteractive is for work that is interacting with the user,
	// such as operating on the main thread, refreshing the UI, or performing animations.
	QOSUserInteractive QOS = 0x21

	// QOSUserInitiated is for work that the user has initiated and is waiting for.
	QOSUserInitiated QOS = 0x19

	// QOSDefault is the default QoS class used when no explicit class is specified.
	QOSDefault QOS = 0x15

	// QOSUtility is for work that the user is unlikely to be immediately waiting for.
	QOSUtility QOS = 0x11

	// QOSBackground is for work that is not visible to the user, such as indexing or syncing.
	QOSBackground QOS = 0x09
)

type Queue

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

Queue wraps a dispatch queue with Go-friendly methods.

func GetGlobalQueue

func GetGlobalQueue(qos QOS) Queue

GetGlobalQueue returns a global concurrent queue with the specified QoS class. Global queues are shared system resources and should not be released.

func MainQueue

func MainQueue() Queue

MainQueue returns the main dispatch queue. Work submitted to this queue executes on the application's main thread.

func QueueCreate

func QueueCreate(label string) Queue

QueueCreate creates a new serial dispatch queue with the given label. Serial queues execute tasks one at a time in FIFO order.

func QueueCreateConcurrent

func QueueCreateConcurrent(label string) Queue

QueueCreateConcurrent creates a new concurrent dispatch queue with the given label. Concurrent queues execute tasks in parallel, subject to system resources.

func QueueCreateWithAttr added in v0.5.3

func QueueCreateWithAttr(label string, attr QueueAttr) Queue

QueueCreateWithAttr creates a queue with the given attributes.

func QueueCreateWithTarget added in v0.5.3

func QueueCreateWithTarget(label string, attr QueueAttr, target Queue) Queue

QueueCreateWithTarget creates a queue with the given attributes and target queue. A zero target queue uses libdispatch's default target queue.

func QueueFromHandle

func QueueFromHandle(handle uintptr) Queue

QueueFromHandle creates a Queue from a raw handle.

func (Queue) Activate added in v0.5.3

func (q Queue) Activate()

Activate activates an initially inactive queue.

func (Queue) AssertCurrent added in v0.5.3

func (q Queue) AssertCurrent()

AssertCurrent verifies that the current execution context is this queue.

func (Queue) AssertCurrentBarrier added in v0.5.3

func (q Queue) AssertCurrentBarrier()

AssertCurrentBarrier verifies that the current execution context is this queue as a barrier.

func (Queue) AssertNotCurrent added in v0.5.3

func (q Queue) AssertNotCurrent()

AssertNotCurrent verifies that the current execution context is not this queue.

func (Queue) Async

func (q Queue) Async(work func())

Async submits a function for asynchronous execution on the queue. The function will execute on a GCD-managed thread.

func (Queue) AsyncAndWait added in v0.5.3

func (q Queue) AsyncAndWait(work func())

AsyncAndWait submits a function for execution and waits for it to complete.

func (Queue) AsyncAndWaitWorkItem added in v0.5.3

func (q Queue) AsyncAndWaitWorkItem(item WorkItem)

AsyncAndWaitWorkItem submits a work item and waits for it to complete.

func (Queue) AsyncWorkItem added in v0.5.3

func (q Queue) AsyncWorkItem(item WorkItem)

AsyncWorkItem submits a work item for asynchronous execution.

func (Queue) BarrierAsync

func (q Queue) BarrierAsync(work func())

BarrierAsync submits a barrier function for asynchronous execution. Only meaningful on concurrent queues - waits for all prior tasks to complete, executes exclusively, then resumes concurrent execution.

func (Queue) BarrierAsyncWorkItem added in v0.5.3

func (q Queue) BarrierAsyncWorkItem(item WorkItem)

BarrierAsyncWorkItem submits a barrier work item for asynchronous execution.

func (Queue) BarrierSync added in v0.5.3

func (q Queue) BarrierSync(work func())

BarrierSync submits a barrier function and waits for it to complete.

func (Queue) BarrierSyncWorkItem added in v0.5.3

func (q Queue) BarrierSyncWorkItem(item WorkItem)

BarrierSyncWorkItem submits a barrier work item and waits for it to complete.

func (Queue) GetID

func (q Queue) GetID() objc.ID

GetID implements the objectivec.IObject interface.

func (Queue) Handle

func (q Queue) Handle() uintptr

Handle returns the underlying dispatch queue handle for advanced usage.

func (Queue) ID

func (q Queue) ID() objc.ID

ID returns the underlying queue handle as an objc.ID for framework compatibility.

func (Queue) Label

func (q Queue) Label() string

Label returns the label assigned to the queue at creation time.

func (Queue) QOSClass added in v0.5.3

func (q Queue) QOSClass() (QOS, int)

QOSClass returns the queue's QoS class and relative priority.

func (Queue) Resume added in v0.5.3

func (q Queue) Resume()

Resume resumes execution of a suspended queue.

func (Queue) SetTargetQueue added in v0.5.3

func (q Queue) SetTargetQueue(target Queue)

SetTargetQueue changes the queue's target queue. A zero target queue resets to libdispatch's default target queue.

func (Queue) Suspend added in v0.5.3

func (q Queue) Suspend()

Suspend suspends execution of the queue.

func (Queue) Sync

func (q Queue) Sync(work func())

Sync submits a function for synchronous execution on the queue. This blocks until the work completes.

Warning: Do not call Sync on a serial queue from within that same queue, as this will deadlock.

func (Queue) SyncWorkItem added in v0.5.3

func (q Queue) SyncWorkItem(item WorkItem)

SyncWorkItem submits a work item for synchronous execution.

type QueueAttr added in v0.5.3

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

QueueAttr configures queue creation.

func ConcurrentQueueAttr added in v0.5.3

func ConcurrentQueueAttr() QueueAttr

ConcurrentQueueAttr returns the concurrent queue attribute.

func SerialQueueAttr added in v0.5.3

func SerialQueueAttr() QueueAttr

SerialQueueAttr returns the default serial queue attribute.

func (QueueAttr) InitiallyInactive added in v0.5.3

func (a QueueAttr) InitiallyInactive() QueueAttr

InitiallyInactive returns a queue attribute that creates inactive queues.

func (QueueAttr) WithAutoreleaseFrequency added in v0.5.3

func (a QueueAttr) WithAutoreleaseFrequency(freq AutoreleaseFrequency) (QueueAttr, error)

WithAutoreleaseFrequency returns a queue attribute with the given autorelease frequency.

func (QueueAttr) WithQOS added in v0.5.3

func (a QueueAttr) WithQOS(qos QOS, relativePriority int) (QueueAttr, error)

WithQOS returns a queue attribute with the given QoS class and relative priority.

type Semaphore

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

Semaphore wraps a dispatch semaphore for resource counting.

func SemaphoreCreate

func SemaphoreCreate(value int) Semaphore

SemaphoreCreate creates a counting semaphore with the given initial value. Use value=0 for signaling, value>0 for resource counting.

func SemaphoreFromHandle

func SemaphoreFromHandle(handle uintptr) Semaphore

SemaphoreFromHandle creates a Semaphore from a raw handle.

func (Semaphore) Handle

func (s Semaphore) Handle() uintptr

Handle returns the underlying dispatch semaphore handle for advanced usage.

func (Semaphore) Signal

func (s Semaphore) Signal() bool

Signal increments the semaphore count. If threads are waiting, one will be woken. Returns true if a thread was woken.

func (Semaphore) Wait

func (s Semaphore) Wait(timeout Dispatch_time_t) bool

Wait decrements the semaphore count, blocking if count is zero. Returns true if the semaphore was acquired, false on timeout.

type Source

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

Source wraps a dispatch event source.

func NewDataAddSource added in v0.5.3

func NewDataAddSource(queue Queue, handler func(value uintptr)) Source

NewDataAddSource creates an activated data-add source.

func NewDataOrSource added in v0.5.3

func NewDataOrSource(queue Queue, handler func(value uintptr)) Source

NewDataOrSource creates an activated data-or source.

func NewDataReplaceSource added in v0.5.3

func NewDataReplaceSource(queue Queue, handler func(value uintptr)) Source

NewDataReplaceSource creates an activated data-replace source.

func NewMemoryPressureSource added in v0.5.3

func NewMemoryPressureSource(mask MemoryPressureFlags, queue Queue, handler func(events MemoryPressureFlags)) Source

NewMemoryPressureSource creates an activated source for memory pressure changes.

func NewProcessSource added in v0.5.3

func NewProcessSource(pid int, mask ProcFlags, queue Queue, handler func(events ProcFlags)) Source

NewProcessSource creates an activated source that fires when process events occur.

func NewReadSource added in v0.5.3

func NewReadSource(fd int, queue Queue, handler func(bytesAvailable uintptr)) Source

NewReadSource creates an activated source that fires when bytes are ready to read.

func NewSignalSource added in v0.5.3

func NewSignalSource(sig syscall.Signal, queue Queue, handler func(count uintptr)) Source

NewSignalSource creates an activated source that fires when the current process receives sig.

func NewTimerSource added in v0.5.3

func NewTimerSource(interval, leeway time.Duration, queue Queue, handler func()) Source

NewTimerSource creates and configures a timer dispatch source. The source is returned in an activated state.

func NewVnodeSource added in v0.5.3

func NewVnodeSource(fd int, mask VnodeFlags, queue Queue, handler func(events VnodeFlags)) Source

NewVnodeSource creates an activated source that fires when vnode events occur.

func NewWriteSource added in v0.5.3

func NewWriteSource(fd int, queue Queue, handler func(bufferSpace uintptr)) Source

NewWriteSource creates an activated source that fires when buffer space is available.

func SourceCreate added in v0.5.3

func SourceCreate(typ SourceType, handle, mask uintptr, queue Queue) Source

SourceCreate creates a new dispatch source to monitor low-level system events. The source is created in a suspended state; call Activate or Resume to begin receiving events.

func SourceFromHandle

func SourceFromHandle(handle uintptr) Source

SourceFromHandle creates a Source from a raw handle.

func (Source) Activate added in v0.5.3

func (s Source) Activate()

Activate marks the source as ready to receive events (macOS 10.12+).

func (Source) Cancel added in v0.5.3

func (s Source) Cancel()

Cancel asynchronously cancels the source, preventing further events.

func (Source) GetData added in v0.5.3

func (s Source) GetData() uintptr

GetData returns pending data for the source (meaning varies by source type).

func (Source) GetHandle added in v0.5.3

func (s Source) GetHandle() uintptr

GetHandle returns the monitored system handle associated with the source.

func (Source) GetMask added in v0.5.3

func (s Source) GetMask() uintptr

GetMask returns the monitored event mask associated with the source.

func (Source) Handle

func (s Source) Handle() uintptr

Handle returns the underlying dispatch source handle.

func (Source) IsCancelled added in v0.5.3

func (s Source) IsCancelled() bool

IsCancelled returns true if the source has been cancelled.

func (Source) MergeData added in v0.5.3

func (s Source) MergeData(value uintptr)

MergeData merges a value into the source's pending data (for DATA_ADD/DATA_OR sources).

func (Source) Resume added in v0.5.3

func (s Source) Resume()

Resume resumes event delivery (balances a Suspend call or initial creation).

func (Source) SetCancelHandler added in v0.5.3

func (s Source) SetCancelHandler(handler func())

SetCancelHandler installs a handler called when the source is cancelled.

func (Source) SetEventHandler added in v0.5.3

func (s Source) SetEventHandler(handler func())

SetEventHandler installs a handler called each time the source fires.

func (Source) SetRegistrationHandler added in v0.5.3

func (s Source) SetRegistrationHandler(handler func())

SetRegistrationHandler installs a handler called once when the source is first activated (registered with the kernel).

func (Source) SetTimer added in v0.5.3

func (s Source) SetTimer(start Dispatch_time_t, interval, leeway time.Duration)

SetTimer configures a timer source's start time, interval, and leeway.

func (Source) Suspend added in v0.5.3

func (s Source) Suspend()

Suspend pauses event delivery.

type SourceType added in v0.5.3

type SourceType uintptr

SourceType identifies the type of a dispatch source.

var (
	SourceTypeDataAdd     SourceType
	SourceTypeDataOr      SourceType
	SourceTypeDataReplace SourceType
	SourceTypeMachSend    SourceType
	SourceTypeMachRecv    SourceType
	SourceTypeMemPressure SourceType
	SourceTypeProc        SourceType
	SourceTypeRead        SourceType
	SourceTypeSignal      SourceType
	SourceTypeTimer       SourceType
	SourceTypeVnode       SourceType
	SourceTypeWrite       SourceType
)

Source type constants loaded via dlsym.

type TimerFlags added in v0.5.3

type TimerFlags uint

TimerFlags for dispatch_source_set_timer.

const (
	TimerStrict TimerFlags = 0x1
)

type VnodeFlags added in v0.5.3

type VnodeFlags uintptr

VnodeFlags for DISPATCH_SOURCE_TYPE_VNODE sources.

const (
	VnodeDelete  VnodeFlags = 0x1
	VnodeWrite   VnodeFlags = 0x2
	VnodeExtend  VnodeFlags = 0x4
	VnodeAttrib  VnodeFlags = 0x8
	VnodeLink    VnodeFlags = 0x10
	VnodeRename  VnodeFlags = 0x20
	VnodeRevoke  VnodeFlags = 0x40
	VnodeFunlock VnodeFlags = 0x100
)

type WorkItem added in v0.5.3

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

WorkItem wraps a dispatch block object.

func WorkItemCreate added in v0.5.3

func WorkItemCreate(flags WorkItemFlags, work func()) WorkItem

WorkItemCreate creates a work item from a Go function.

func WorkItemCreateWithQOS added in v0.5.3

func WorkItemCreateWithQOS(flags WorkItemFlags, qos QOS, relativePriority int, work func()) WorkItem

WorkItemCreateWithQOS creates a work item with an explicit QoS class.

func (WorkItem) Cancel added in v0.5.3

func (w WorkItem) Cancel()

Cancel cancels future execution of the work item.

func (WorkItem) Handle added in v0.5.3

func (w WorkItem) Handle() uintptr

WorkItemHandle returns the underlying dispatch block handle.

func (WorkItem) IsCancelled added in v0.5.3

func (w WorkItem) IsCancelled() bool

IsCancelled reports whether the work item has been cancelled.

func (WorkItem) Notify added in v0.5.3

func (w WorkItem) Notify(queue Queue, work func())

Notify schedules work to run when the work item completes.

func (WorkItem) Perform added in v0.5.3

func (w WorkItem) Perform()

Perform invokes the work item directly on the current thread.

func (WorkItem) Release added in v0.5.3

func (w WorkItem) Release()

Release releases the underlying block object.

func (WorkItem) Wait added in v0.5.3

func (w WorkItem) Wait(timeout Dispatch_time_t) bool

Wait blocks until the work item completes or timeout expires.

type WorkItemFlags added in v0.5.3

type WorkItemFlags uintptr

WorkItemFlags configure dispatch work items.

const (
	WorkItemBarrier         WorkItemFlags = 0x1
	WorkItemDetached        WorkItemFlags = 0x2
	WorkItemAssignCurrent   WorkItemFlags = 0x4
	WorkItemNoQOSClass      WorkItemFlags = 0x8
	WorkItemInheritQOSClass WorkItemFlags = 0x10
	WorkItemEnforceQOSClass WorkItemFlags = 0x20
)

Jump to

Keyboard shortcuts

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