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.
Known Limitations with Apple Frameworks ¶
When passing dispatch queues to Apple frameworks (like Virtualization, AVFoundation), some frameworks internally use ObjC dispatch_sync blocks which require cgo coordination. This package uses purego (cgo-free), which may cause crashes when:
- Framework wraps callbacks in ObjC dispatch_sync blocks
- Framework schedules work that calls back into Go
- Go callbacks execute on framework-managed threads
Workarounds:
- Use nil/default queue when passing to frameworks (let framework manage its queue)
- Use the main dispatch queue for framework interactions
- For Virtualization framework specifically, consider using cgo-based wrappers
For Go-to-Go dispatch operations (queue.Async, queue.Sync, groups, semaphores), this package works correctly with race detection and GC.
Code generated from Apple documentation. DO NOT EDIT.
Index ¶
- func After(when Dispatch_time_t, queue Queue, work func())
- func Main()
- type Data
- type DispatchWalltimeNow
- 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_vnode_flags_t
- type Dispatch_time_t
- type Dispatch_workloop_t
- type Group
- type IO
- type QOS
- type Queue
- type Semaphore
- type Source
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.
Types ¶
type Data ¶
type Data struct {
// contains filtered or unexported fields
}
Data wraps a dispatch data object.
func DataFromHandle ¶
DataFromHandle creates a Data from a raw handle.
type DispatchWalltimeNow ¶
type DispatchWalltimeNow uint
const ( // DISPATCH_WALLTIME_NOW: The current time. DISPATCH_WALLTIME_NOW DispatchWalltimeNow = 0 )
func (DispatchWalltimeNow) String ¶
func (e DispatchWalltimeNow) String() string
type Dispatch_block_t ¶
type Dispatch_block_t = func()
type Dispatch_data_applier_t ¶
type Dispatch_fd_t ¶
type Dispatch_fd_t = int
type Dispatch_function_t ¶
type Dispatch_io_close_flags_t ¶
type Dispatch_io_close_flags_t = uint
type Dispatch_io_handler_t ¶
type Dispatch_io_handler_t = func(bool, objectivec.Object, int)
type Dispatch_io_interval_flags_t ¶
type Dispatch_io_interval_flags_t = uint
type Dispatch_once_t ¶
type Dispatch_once_t = int
type Dispatch_qos_class_t ¶
type Dispatch_qos_class_t = uint32
type Dispatch_queue_concurrent_t ¶
type Dispatch_queue_concurrent_t = dispatch_queue_t
type Dispatch_queue_global_t ¶
type Dispatch_queue_global_t = dispatch_queue_t
type Dispatch_queue_main_t ¶
type Dispatch_queue_main_t = dispatch_queue_t
type Dispatch_queue_priority_t ¶
type Dispatch_queue_priority_t = int
type Dispatch_queue_serial_executor_t ¶
type Dispatch_queue_serial_executor_t = dispatch_queue_t
type Dispatch_queue_serial_t ¶
type Dispatch_queue_serial_t = dispatch_queue_t
type Dispatch_source_mach_recv_flags_t ¶
type Dispatch_source_mach_recv_flags_t = uint
type Dispatch_source_mach_send_flags_t ¶
type Dispatch_source_mach_send_flags_t = uint
type Dispatch_source_memorypressure_flags_t ¶
type Dispatch_source_memorypressure_flags_t = uint
type Dispatch_source_proc_flags_t ¶
type Dispatch_source_proc_flags_t = uint
type Dispatch_source_timer_flags_t ¶
type Dispatch_source_timer_flags_t = uint
type Dispatch_source_vnode_flags_t ¶
type Dispatch_source_vnode_flags_t = uint
type Dispatch_time_t ¶
type Dispatch_time_t = uint64
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
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 IOFromHandle ¶
IOFromHandle creates an IO from a raw handle.
type QOS ¶
type QOS uint32
QoS (Quality of Service) classes for dispatch queues. Higher QoS classes get more system resources.
const ( // 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 QueueFromHandle ¶
QueueFromHandle creates a Queue from a raw handle.
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) 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.
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 SourceFromHandle ¶
SourceFromHandle creates a Source from a raw handle.