Skip to content

Commit

Permalink
retain and release dispatch queue
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-skrenkovic committed May 6, 2024
1 parent e67c25c commit 6dd9088
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion fsevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func DeviceForPath(path string) (int32, error) {
// ...
type EventStream struct {
stream fsEventStreamRef
qref fsDispatchQueueRef
hasFinalizer bool
registryID uintptr
uuid string
Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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)
}

0 comments on commit 6dd9088

Please sign in to comment.