Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/runtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

static dispatch_source_t bare__poll;
static dispatch_source_t bare__timer;
static CFRunLoopObserverRef bare__observer;

static bool bare__exited;
static bool bare__running;

static void
bare__on_shutdown(uv_async_t *handle) {
Expand Down Expand Up @@ -62,9 +64,15 @@
bare__run(void) {
int err;

if (bare__running) return;

bare__running = true;

err = bare_run(bare, UV_RUN_NOWAIT);
assert(err >= 0);

bare__running = false;

if (bare__exited) return;

int timeout = uv_backend_timeout(bare__loop);
Expand Down Expand Up @@ -135,13 +143,31 @@
dispatch_resume(bare__poll);
dispatch_resume(bare__timer);

bare__observer = CFRunLoopObserverCreateWithHandler(
NULL,
kCFRunLoopBeforeWaiting,
true,
0,
^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
if (bare__exited) return;

bare__run();
}
);

CFRunLoopAddObserver(CFRunLoopGetMain(), bare__observer, kCFRunLoopCommonModes);

bare__run();
}

static void
bare__terminate(void) {
int err;

CFRunLoopRemoveObserver(CFRunLoopGetMain(), bare__observer, kCFRunLoopCommonModes);

CFRelease(bare__observer);

dispatch_source_cancel(bare__poll);
dispatch_source_cancel(bare__timer);

Expand Down
Loading