Skip to content

Commit

Permalink
dispatch: Add CreateQueue, Main
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc committed Nov 22, 2023
1 parent 53e6adf commit 2653e6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 16 additions & 3 deletions dispatch/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ package dispatch
// void Dispatch_Release(void* queue);
// void Dispatch_Async(void* queue, uintptr_t task);
// void Dispatch_Sync(void* queue, uintptr_t task);
// void* Dispatch_Create_Queue(const char* label, void* attr);
// void* Dispatch_Create_Queue(const char* label, int type);
// void Dispatch_Main();
import "C"
import (
"math"
Expand All @@ -31,6 +32,13 @@ const (
QueuePriorityBackground QueuePriority = math.MinInt16
)

type QueueType int

const (
QueueTypeSerial QueueType = 0
QueueTypeConcurrent QueueType = 1
)

// Returns the serial dispatch queue associated with the application’s main thread. [Full Topic]
//
// [Full Topic]: https://developer.apple.com/documentation/dispatch/1452921-dispatch_get_main_queue?language=objc
Expand Down Expand Up @@ -75,11 +83,16 @@ func runQueueTask(p C.uintptr_t) {
h.Delete()
}

// Main executes blocks submitted to the main queue.
func Main() {
C.Dispatch_Main()
}

// Creates a new dispatch queue to which blocks can be submitted.
//
// [Full Topic]: https://developer.apple.com/documentation/dispatch/1453030-dispatch_queue_create?language=objc
func CreateQueue(label string, attr uintptr) Queue {
func CreateQueue(label string, queueType QueueType) Queue {
cLabel := C.CString(label)
p := C.Dispatch_Create_Queue(cLabel, unsafe.Pointer(attr))
p := C.Dispatch_Create_Queue(cLabel, C.int(queueType))
return Queue{p}
}
12 changes: 9 additions & 3 deletions dispatch/queue.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ void Dispatch_Sync(void* queue, uintptr_t task) {
});
}

void* Dispatch_Create_Queue(const char* label, void* attr) {
dispatch_queue_t queue = dispatch_queue_create(label, attr);
return queue;
void Dispatch_Main() {
dispatch_main();
}

void* Dispatch_Create_Queue(const char* label, int type) {
if (type == 0) {
return dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL);
}
return dispatch_queue_create(label, DISPATCH_QUEUE_CONCURRENT);
}

0 comments on commit 2653e6a

Please sign in to comment.