2013-04-13 15:05:13 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2013-04-14 09:33:44 +00:00
|
|
|
#include "common/node_bindings_mac.h"
|
2013-04-13 15:05:13 +00:00
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
#include <errno.h>
|
2013-04-19 13:21:04 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2013-04-13 15:05:13 +00:00
|
|
|
#include "vendor/node/src/node.h"
|
|
|
|
#include "vendor/node/src/node_internals.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
NodeBindingsMac::NodeBindingsMac(bool is_browser)
|
|
|
|
: NodeBindings(is_browser),
|
2013-07-22 06:58:25 +00:00
|
|
|
kqueue_(kqueue()) {
|
|
|
|
// Add uv's backend fd to kqueue.
|
|
|
|
struct kevent ev;
|
|
|
|
EV_SET(&ev, uv_backend_fd(uv_loop_), EVFILT_READ, EV_ADD | EV_ENABLE,
|
|
|
|
0, 0, 0);
|
|
|
|
kevent(kqueue_, &ev, 1, NULL, 0, NULL);
|
2013-04-13 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeBindingsMac::~NodeBindingsMac() {
|
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
void NodeBindingsMac::PollEvents() {
|
|
|
|
struct timespec spec;
|
|
|
|
int timeout = uv_backend_timeout(uv_loop_);
|
|
|
|
if (timeout != -1) {
|
|
|
|
spec.tv_sec = timeout / 1000;
|
|
|
|
spec.tv_nsec = (timeout % 1000) * 1000000;
|
2013-04-13 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// Wait for new libuv events.
|
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
struct kevent ev;
|
|
|
|
r = ::kevent(kqueue_, NULL, 0, &ev, 1,
|
|
|
|
timeout == -1 ? NULL : &spec);
|
|
|
|
} while (r == -1 && errno == EINTR);
|
2013-04-13 15:05:13 +00:00
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// Deal with event in main thread.
|
|
|
|
WakeupMainThread();
|
2013-04-13 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-07-04 11:47:48 +00:00
|
|
|
NodeBindings* NodeBindings::CreateInBrowser() {
|
|
|
|
return new NodeBindingsMac(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
NodeBindings* NodeBindings::CreateInRenderer() {
|
|
|
|
return new NodeBindingsMac(false);
|
2013-04-13 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|