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 ¶
- func After(when Dispatch_time_t, queue Queue, work func())
- func Apply(iterations int, queue Queue, work func(iteration int))
- func ApplyAuto(iterations int, work func(iteration int))
- func DataGetSize(d Data) int
- func DataToBytes(d Data) []byte
- func Main()
- func Read(fd int, length int, queue Queue, handler func(data Data, err error))
- func ReadBytes(fd int, length int, queue Queue, handler func(data []byte, err error))
- func ReadFD(fd int, length int, queue Queue, handler func(data []byte, err error))
- func Write(fd int, data Data, queue Queue, handler func(remaining Data, err error))
- func WriteBytes(fd int, buf []byte, queue Queue, handler func(remaining []byte, err error))
- func WriteFD(fd int, buf []byte, queue Queue, handler func(remaining []byte, err error))
- type AutoreleaseFrequency
- type Data
- type DispatchWalltime
- type Dispatch_block_t
- type Dispatch_data_applier_t
- type Dispatch_fd_t
- type Dispatch_function_t
- type Dispatch_io_close_flags_t
- type Dispatch_io_handler_t
- type Dispatch_io_interval_flags_t
- type Dispatch_once_t
- type Dispatch_qos_class_t
- type Dispatch_queue_concurrent_t
- type Dispatch_queue_global_t
- type Dispatch_queue_main_t
- type Dispatch_queue_priority_t
- type Dispatch_queue_serial_executor_t
- type Dispatch_queue_serial_t
- type Dispatch_source_mach_recv_flags_t
- type Dispatch_source_mach_send_flags_t
- type Dispatch_source_memorypressure_flags_t
- type Dispatch_source_proc_flags_t
- type Dispatch_source_timer_flags_t
- type Dispatch_source_type_t
- type Dispatch_source_vnode_flags_t
- type Dispatch_time_t
- type Dispatch_workloop_t
- type Group
- type IO
- func (i IO) Barrier(fn func())
- func (i IO) Close(flags IOCloseFlags)
- func (i IO) Descriptor() int
- func (i IO) Handle() uintptr
- func (i IO) Read(offset int64, length uint64, queue Queue, ...)
- func (i IO) SetHighWater(bytes uint64)
- func (i IO) SetInterval(interval time.Duration, flags IOIntervalFlags)
- func (i IO) SetLowWater(bytes uint64)
- func (i IO) Write(offset int64, data Data, queue Queue, ...)
- type IOCloseFlags
- type IOIntervalFlags
- type IOType
- type MemoryPressureFlags
- type ProcFlags
- type QOS
- type Queue
- func GetGlobalQueue(qos QOS) Queue
- func MainQueue() Queue
- func QueueCreate(label string) Queue
- func QueueCreateConcurrent(label string) Queue
- func QueueCreateWithAttr(label string, attr QueueAttr) Queue
- func QueueCreateWithTarget(label string, attr QueueAttr, target Queue) Queue
- func QueueFromHandle(handle uintptr) Queue
- func (q Queue) Activate()
- func (q Queue) AssertCurrent()
- func (q Queue) AssertCurrentBarrier()
- func (q Queue) AssertNotCurrent()
- func (q Queue) Async(work func())
- func (q Queue) AsyncAndWait(work func())
- func (q Queue) AsyncAndWaitWorkItem(item WorkItem)
- func (q Queue) AsyncWorkItem(item WorkItem)
- func (q Queue) BarrierAsync(work func())
- func (q Queue) BarrierAsyncWorkItem(item WorkItem)
- func (q Queue) BarrierSync(work func())
- func (q Queue) BarrierSyncWorkItem(item WorkItem)
- func (q Queue) GetID() objc.ID
- func (q Queue) Handle() uintptr
- func (q Queue) ID() objc.ID
- func (q Queue) Label() string
- func (q Queue) QOSClass() (QOS, int)
- func (q Queue) Resume()
- func (q Queue) SetTargetQueue(target Queue)
- func (q Queue) Suspend()
- func (q Queue) Sync(work func())
- func (q Queue) SyncWorkItem(item WorkItem)
- type QueueAttr
- type Semaphore
- type Source
- func NewDataAddSource(queue Queue, handler func(value uintptr)) Source
- func NewDataOrSource(queue Queue, handler func(value uintptr)) Source
- func NewDataReplaceSource(queue Queue, handler func(value uintptr)) Source
- func NewMemoryPressureSource(mask MemoryPressureFlags, queue Queue, ...) Source
- func NewProcessSource(pid int, mask ProcFlags, queue Queue, handler func(events ProcFlags)) Source
- func NewReadSource(fd int, queue Queue, handler func(bytesAvailable uintptr)) Source
- func NewSignalSource(sig syscall.Signal, queue Queue, handler func(count uintptr)) Source
- func NewTimerSource(interval, leeway time.Duration, queue Queue, handler func()) Source
- func NewVnodeSource(fd int, mask VnodeFlags, queue Queue, handler func(events VnodeFlags)) Source
- func NewWriteSource(fd int, queue Queue, handler func(bufferSpace uintptr)) Source
- func SourceCreate(typ SourceType, handle, mask uintptr, queue Queue) Source
- func SourceFromHandle(handle uintptr) Source
- func (s Source) Activate()
- func (s Source) Cancel()
- func (s Source) GetData() uintptr
- func (s Source) GetHandle() uintptr
- func (s Source) GetMask() uintptr
- func (s Source) Handle() uintptr
- func (s Source) IsCancelled() bool
- func (s Source) MergeData(value uintptr)
- func (s Source) Resume()
- func (s Source) SetCancelHandler(handler func())
- func (s Source) SetEventHandler(handler func())
- func (s Source) SetRegistrationHandler(handler func())
- func (s Source) SetTimer(start Dispatch_time_t, interval, leeway time.Duration)
- func (s Source) Suspend()
- type SourceType
- type TimerFlags
- type VnodeFlags
- type WorkItem
- type WorkItemFlags
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
Apply submits work for parallel execution across the given number of iterations.
func ApplyAuto ¶ added in v0.5.3
ApplyAuto submits work for parallel execution using libdispatch's automatic queue selection.
func DataGetSize ¶ added in v0.5.3
DataGetSize returns the number of bytes in the data object.
func DataToBytes ¶ added in v0.5.3
DataToBytes extracts bytes from a dispatch data object. Returns a copy the caller owns.
func ReadBytes ¶ added in v0.5.3
ReadBytes schedules a one-shot asynchronous read and copies the result into a Go slice.
func ReadFD ¶ added in v0.5.3
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 WriteBytes ¶ added in v0.5.3
WriteBytes schedules a one-shot asynchronous write from a Go slice.
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
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
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 ¶
DataFromHandle creates a Data from a raw handle.
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 ¶
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 ¶
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 ¶
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_executor_t ¶
type Dispatch_queue_serial_executor_t = dispatch_queue_t
See: https://developer.apple.com/documentation/Dispatch/dispatch_queue_serial_executor_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 ¶
GroupFromHandle creates a Group from a raw handle.
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) 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
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
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 ¶
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
Descriptor returns the file descriptor associated with the channel.
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
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
SetLowWater sets the low-water mark for IO operations.
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 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.
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 ¶
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 ¶
QueueCreate creates a new serial dispatch queue with the given label. Serial queues execute tasks one at a time in FIFO order.
func QueueCreateConcurrent ¶
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
QueueCreateWithAttr creates a queue with the given attributes.
func QueueCreateWithTarget ¶ added in v0.5.3
QueueCreateWithTarget creates a queue with the given attributes and target queue. A zero target queue uses libdispatch's default target queue.
func QueueFromHandle ¶
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
AsyncAndWaitWorkItem submits a work item and waits for it to complete.
func (Queue) AsyncWorkItem ¶ added in v0.5.3
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
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
BarrierSyncWorkItem submits a barrier work item and waits for it to complete.
func (Queue) QOSClass ¶ added in v0.5.3
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
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
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
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.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore wraps a dispatch semaphore for resource counting.
func SemaphoreCreate ¶
SemaphoreCreate creates a counting semaphore with the given initial value. Use value=0 for signaling, value>0 for resource counting.
func SemaphoreFromHandle ¶
SemaphoreFromHandle creates a Semaphore from a raw handle.
func (Semaphore) Handle ¶
Handle returns the underlying dispatch semaphore handle for advanced usage.
func (Semaphore) Signal ¶
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
NewDataAddSource creates an activated data-add source.
func NewDataOrSource ¶ added in v0.5.3
NewDataOrSource creates an activated data-or source.
func NewDataReplaceSource ¶ added in v0.5.3
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
NewProcessSource creates an activated source that fires when process events occur.
func NewReadSource ¶ added in v0.5.3
NewReadSource creates an activated source that fires when bytes are ready to read.
func NewSignalSource ¶ added in v0.5.3
NewSignalSource creates an activated source that fires when the current process receives sig.
func NewTimerSource ¶ added in v0.5.3
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
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 ¶
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
GetData returns pending data for the source (meaning varies by source type).
func (Source) GetHandle ¶ added in v0.5.3
GetHandle returns the monitored system handle associated with the source.
func (Source) GetMask ¶ added in v0.5.3
GetMask returns the monitored event mask associated with the source.
func (Source) IsCancelled ¶ added in v0.5.3
IsCancelled returns true if the source has been cancelled.
func (Source) MergeData ¶ added in v0.5.3
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).
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
WorkItemHandle returns the underlying dispatch block handle.
func (WorkItem) IsCancelled ¶ added in v0.5.3
IsCancelled reports whether the work item has been cancelled.
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 )