From 6dd9088aae09edbe5c9946a99225cddd4f8f6f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Skrenkovic=CC=81?= Date: Tue, 7 May 2024 00:17:43 +0200 Subject: [PATCH] retain and release dispatch queue --- fsevents.go | 4 +++- wrap.go | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/fsevents.go b/fsevents.go index 4093eb1..15c8bb6 100644 --- a/fsevents.go +++ b/fsevents.go @@ -54,6 +54,7 @@ func DeviceForPath(path string) (int32, error) { // ... type EventStream struct { stream fsEventStreamRef + qref fsDispatchQueueRef hasFinalizer bool registryID uintptr uuid string @@ -163,8 +164,9 @@ func (es *EventStream) Flush(sync bool) { // Stop stops listening to the event stream. func (es *EventStream) Stop() { if es.stream != nil { - stop(es.stream) + stop(es.stream, es.qref) es.stream = nil + es.qref = nil } // Remove eventstream from the registry diff --git a/wrap.go b/wrap.go index aacec73..c9b7c73 100644 --- a/wrap.go +++ b/wrap.go @@ -22,6 +22,14 @@ static FSEventStreamRef EventStreamCreate(FSEventStreamContext * context, uintpt context->info = (void*) info; return FSEventStreamCreate(NULL, (FSEventStreamCallback) fsevtCallback, context, paths, since, latency, flags); } + +static void DispatchQueueRetain(dispatch_queue_t queue) { + dispatch_retain(queue); +} + +static void DispatchQueueRelease(dispatch_queue_t queue) { + dispatch_release(queue); +} */ import "C" import ( @@ -278,6 +286,8 @@ func fsevtCallback(stream C.FSEventStreamRef, info uintptr, numEvents C.size_t, es.Events <- events } +type fsDispatchQueueRef C.dispatch_queue_t + // fsEventStreamRef wraps C.FSEventStreamRef type fsEventStreamRef C.FSEventStreamRef @@ -426,14 +436,16 @@ func (es *EventStream) start(paths []string, callbackInfo uintptr) error { es.stream = setupStream(paths, es.Flags, callbackInfo, since, es.Latency, es.Device) - q := C.dispatch_queue_create(nil, nil) - C.FSEventStreamSetDispatchQueue(es.stream, q) + es.qref = fsDispatchQueueRef(C.dispatch_queue_create(nil, nil)) + C.DispatchQueueRetain(es.qref) + C.FSEventStreamSetDispatchQueue(es.stream, es.qref) if C.FSEventStreamStart(es.stream) == 0 { // cleanup stream C.FSEventStreamInvalidate(es.stream) C.FSEventStreamRelease(es.stream) es.stream = nil + es.qref = nil return fmt.Errorf("failed to start eventstream") } @@ -463,8 +475,9 @@ func flush(stream fsEventStreamRef, sync bool) { } // stop requests fsevents stops streaming events -func stop(stream fsEventStreamRef) { +func stop(stream fsEventStreamRef, qref fsDispatchQueueRef) { C.FSEventStreamStop(stream) C.FSEventStreamInvalidate(stream) C.FSEventStreamRelease(stream) + C.DispatchQueueRelease(qref) }