Move common message integration code to NodeBindings' base class.
This commit is contained in:
parent
41ff753339
commit
00fda0e778
4 changed files with 157 additions and 146 deletions
|
@ -7,7 +7,9 @@
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
|
#include "base/message_loop.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "v8/include/v8.h"
|
#include "v8/include/v8.h"
|
||||||
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
||||||
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
||||||
|
@ -15,13 +17,29 @@
|
||||||
#include "vendor/node/src/node_internals.h"
|
#include "vendor/node/src/node_internals.h"
|
||||||
#include "vendor/node/src/node_javascript.h"
|
#include "vendor/node/src/node_javascript.h"
|
||||||
|
|
||||||
|
using content::BrowserThread;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void UvNoOp(uv_async_t* handle, int status) {
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
NodeBindings::NodeBindings(bool is_browser)
|
NodeBindings::NodeBindings(bool is_browser)
|
||||||
: is_browser_(is_browser) {
|
: is_browser_(is_browser),
|
||||||
|
message_loop_(NULL),
|
||||||
|
uv_loop_(uv_default_loop()),
|
||||||
|
embed_closed_(false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeBindings::~NodeBindings() {
|
NodeBindings::~NodeBindings() {
|
||||||
|
// Clear uv.
|
||||||
|
embed_closed_ = true;
|
||||||
|
uv_thread_join(&embed_thread_);
|
||||||
|
uv_sem_destroy(&embed_sem_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeBindings::Initialize() {
|
void NodeBindings::Initialize() {
|
||||||
|
@ -97,4 +115,78 @@ void NodeBindings::BindTo(WebKit::WebFrame* frame) {
|
||||||
v8::Local<v8::Function>::Cast(result)->Call(context->Global(), 2, args);
|
v8::Local<v8::Function>::Cast(result)->Call(context->Global(), 2, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NodeBindings::PrepareMessageLoop() {
|
||||||
|
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
||||||
|
|
||||||
|
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
||||||
|
// nothing to do.
|
||||||
|
uv_async_init(uv_loop_, &dummy_uv_handle_, UvNoOp);
|
||||||
|
|
||||||
|
// Start worker that will interrupt main loop when having uv events.
|
||||||
|
uv_sem_init(&embed_sem_, 0);
|
||||||
|
uv_thread_create(&embed_thread_, EmbedThreadRunner, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeBindings::RunMessageLoop() {
|
||||||
|
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
||||||
|
|
||||||
|
// The MessageLoop should have been created, remember the one in main thread.
|
||||||
|
message_loop_ = base::MessageLoop::current();
|
||||||
|
|
||||||
|
// Get notified when libuv's watcher queue changes.
|
||||||
|
uv_loop_->data = this;
|
||||||
|
uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
|
||||||
|
|
||||||
|
// Run uv loop for once to give the uv__io_poll a chance to add all events.
|
||||||
|
UvRunOnce();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeBindings::UvRunOnce() {
|
||||||
|
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
||||||
|
|
||||||
|
// Enter node context while dealing with uv events.
|
||||||
|
v8::HandleScope scope;
|
||||||
|
v8::Context::Scope context_scope(node::g_context);
|
||||||
|
|
||||||
|
// Deal with uv events.
|
||||||
|
int r = uv_run(uv_loop_, (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT));
|
||||||
|
if (r == 0 || uv_loop_->stop_flag != 0)
|
||||||
|
message_loop_->QuitWhenIdle(); // Quit from uv.
|
||||||
|
|
||||||
|
// Tell the worker thread to continue polling.
|
||||||
|
uv_sem_post(&embed_sem_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeBindings::WakeupMainThread() {
|
||||||
|
DCHECK(message_loop_);
|
||||||
|
message_loop_->PostTask(FROM_HERE, base::Bind(&NodeBindings::UvRunOnce,
|
||||||
|
base::Unretained(this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void NodeBindings::EmbedThreadRunner(void *arg) {
|
||||||
|
NodeBindings* self = static_cast<NodeBindings*>(arg);
|
||||||
|
|
||||||
|
while (!self->embed_closed_) {
|
||||||
|
// Wait for the main loop to deal with events.
|
||||||
|
uv_sem_wait(&self->embed_sem_);
|
||||||
|
|
||||||
|
self->PollEvents();
|
||||||
|
|
||||||
|
// Deal with event in main thread.
|
||||||
|
self->WakeupMainThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void NodeBindings::OnWatcherQueueChanged(uv_loop_t* loop) {
|
||||||
|
NodeBindings* self = static_cast<NodeBindings*>(loop->data);
|
||||||
|
|
||||||
|
DCHECK(!self->is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
||||||
|
|
||||||
|
// We need to break the io polling in the kqueue thread when loop's watcher
|
||||||
|
// queue changes, otherwise new events cannot be notified.
|
||||||
|
uv_async_send(&self->dummy_uv_handle_);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -6,11 +6,16 @@
|
||||||
#define RAVE_COMMON_NODE_BINDINGS_
|
#define RAVE_COMMON_NODE_BINDINGS_
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
|
#include "vendor/node/deps/uv/include/uv.h"
|
||||||
|
|
||||||
namespace WebKit {
|
namespace WebKit {
|
||||||
class WebFrame;
|
class WebFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
class MessageLoop;
|
||||||
|
}
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class NodeBindings {
|
class NodeBindings {
|
||||||
|
@ -30,17 +35,51 @@ class NodeBindings {
|
||||||
virtual void BindTo(WebKit::WebFrame* frame);
|
virtual void BindTo(WebKit::WebFrame* frame);
|
||||||
|
|
||||||
// Prepare for message loop integration.
|
// Prepare for message loop integration.
|
||||||
virtual void PrepareMessageLoop() = 0;
|
virtual void PrepareMessageLoop();
|
||||||
|
|
||||||
// Do message loop integration.
|
// Do message loop integration.
|
||||||
virtual void RunMessageLoop() = 0;
|
virtual void RunMessageLoop();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit NodeBindings(bool is_browser);
|
explicit NodeBindings(bool is_browser);
|
||||||
|
|
||||||
|
// Called to poll events in new thread.
|
||||||
|
virtual void PollEvents() = 0;
|
||||||
|
|
||||||
|
// Run the libuv loop for once.
|
||||||
|
void UvRunOnce();
|
||||||
|
|
||||||
|
// Make the main thread run libuv loop.
|
||||||
|
void WakeupMainThread();
|
||||||
|
|
||||||
|
// Are we running in browser.
|
||||||
bool is_browser_;
|
bool is_browser_;
|
||||||
|
|
||||||
|
// Main thread's MessageLoop.
|
||||||
|
base::MessageLoop* message_loop_;
|
||||||
|
|
||||||
|
// Main thread's libuv loop.
|
||||||
|
uv_loop_t* uv_loop_;
|
||||||
|
|
||||||
|
// Dummy handle to make uv's loop not quit.
|
||||||
|
uv_async_t dummy_uv_handle_;
|
||||||
|
|
||||||
|
// Thread for polling events.
|
||||||
|
uv_thread_t embed_thread_;
|
||||||
|
|
||||||
|
// Whether we're done.
|
||||||
|
bool embed_closed_;
|
||||||
|
|
||||||
|
// Semaphore to wait for main loop in the embed thread.
|
||||||
|
uv_sem_t embed_sem_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Thread to poll uv events.
|
||||||
|
static void EmbedThreadRunner(void *arg);
|
||||||
|
|
||||||
|
// Called when uv's watcher queue changes.
|
||||||
|
static void OnWatcherQueueChanged(uv_loop_t* loop);
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(NodeBindings);
|
DISALLOW_COPY_AND_ASSIGN(NodeBindings);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,105 +4,32 @@
|
||||||
|
|
||||||
#include "common/node_bindings_mac.h"
|
#include "common/node_bindings_mac.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "base/message_loop.h"
|
|
||||||
#include "content/public/browser/browser_thread.h"
|
|
||||||
#include "vendor/node/src/node.h"
|
#include "vendor/node/src/node.h"
|
||||||
#include "vendor/node/src/node_internals.h"
|
#include "vendor/node/src/node_internals.h"
|
||||||
|
|
||||||
using content::BrowserThread;
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
void UvNoOp(uv_async_t* handle, int status) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
NodeBindingsMac::NodeBindingsMac(bool is_browser)
|
NodeBindingsMac::NodeBindingsMac(bool is_browser)
|
||||||
: NodeBindings(is_browser),
|
: NodeBindings(is_browser),
|
||||||
message_loop_(NULL),
|
kqueue_(kqueue()) {
|
||||||
uv_loop_(uv_default_loop()),
|
// Add uv's backend fd to kqueue.
|
||||||
kqueue_(kqueue()),
|
struct kevent ev;
|
||||||
embed_closed_(false) {
|
EV_SET(&ev, uv_backend_fd(uv_loop_), EVFILT_READ, EV_ADD | EV_ENABLE,
|
||||||
|
0, 0, 0);
|
||||||
|
kevent(kqueue_, &ev, 1, NULL, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeBindingsMac::~NodeBindingsMac() {
|
NodeBindingsMac::~NodeBindingsMac() {
|
||||||
// Clear uv.
|
|
||||||
embed_closed_ = true;
|
|
||||||
uv_thread_join(&embed_thread_);
|
|
||||||
uv_sem_destroy(&embed_sem_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeBindingsMac::PrepareMessageLoop() {
|
void NodeBindingsMac::PollEvents() {
|
||||||
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
||||||
|
|
||||||
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
|
||||||
// nothing to do.
|
|
||||||
uv_async_init(uv_loop_, &dummy_uv_handle_, UvNoOp);
|
|
||||||
|
|
||||||
// Start worker that will interrupt main loop when having uv events.
|
|
||||||
uv_sem_init(&embed_sem_, 0);
|
|
||||||
uv_thread_create(&embed_thread_, EmbedThreadRunner, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsMac::RunMessageLoop() {
|
|
||||||
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
||||||
|
|
||||||
// The MessageLoop should have been created, remember the one in main thread.
|
|
||||||
message_loop_ = base::MessageLoop::current();
|
|
||||||
|
|
||||||
// Get notified when libuv's watcher queue changes.
|
|
||||||
uv_loop_->data = this;
|
|
||||||
uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
|
|
||||||
|
|
||||||
// Run uv loop for once to give the uv__io_poll a chance to add all events.
|
|
||||||
UvRunOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsMac::UvRunOnce() {
|
|
||||||
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
||||||
|
|
||||||
// Enter node context while dealing with uv events.
|
|
||||||
v8::HandleScope scope;
|
|
||||||
v8::Context::Scope context_scope(node::g_context);
|
|
||||||
|
|
||||||
// Deal with uv events.
|
|
||||||
int r = uv_run(uv_loop_, (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT));
|
|
||||||
if (r == 0 || uv_loop_->stop_flag != 0)
|
|
||||||
message_loop_->QuitWhenIdle(); // Quit from uv.
|
|
||||||
|
|
||||||
// Tell the worker thread to continue polling.
|
|
||||||
uv_sem_post(&embed_sem_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsMac::WakeupMainThread() {
|
|
||||||
DCHECK(message_loop_);
|
|
||||||
message_loop_->PostTask(FROM_HERE, base::Bind(&NodeBindingsMac::UvRunOnce,
|
|
||||||
base::Unretained(this)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsMac::EmbedThreadRunner(void *arg) {
|
|
||||||
NodeBindingsMac* self = static_cast<NodeBindingsMac*>(arg);
|
|
||||||
|
|
||||||
uv_loop_t* loop = self->uv_loop_;
|
|
||||||
|
|
||||||
// Add uv's backend fd to kqueue.
|
|
||||||
struct kevent ev;
|
|
||||||
EV_SET(&ev, uv_backend_fd(loop), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
|
|
||||||
kevent(self->kqueue_, &ev, 1, NULL, 0, NULL);
|
|
||||||
|
|
||||||
while (!self->embed_closed_) {
|
|
||||||
// Wait for the main loop to deal with events.
|
|
||||||
uv_sem_wait(&self->embed_sem_);
|
|
||||||
|
|
||||||
struct timespec spec;
|
struct timespec spec;
|
||||||
int timeout = uv_backend_timeout(loop);
|
int timeout = uv_backend_timeout(uv_loop_);
|
||||||
if (timeout != -1) {
|
if (timeout != -1) {
|
||||||
spec.tv_sec = timeout / 1000;
|
spec.tv_sec = timeout / 1000;
|
||||||
spec.tv_nsec = (timeout % 1000) * 1000000;
|
spec.tv_nsec = (timeout % 1000) * 1000000;
|
||||||
|
@ -111,24 +38,13 @@ void NodeBindingsMac::EmbedThreadRunner(void *arg) {
|
||||||
// Wait for new libuv events.
|
// Wait for new libuv events.
|
||||||
int r;
|
int r;
|
||||||
do {
|
do {
|
||||||
r = ::kevent(self->kqueue_, NULL, 0, &ev, 1,
|
struct kevent ev;
|
||||||
|
r = ::kevent(kqueue_, NULL, 0, &ev, 1,
|
||||||
timeout == -1 ? NULL : &spec);
|
timeout == -1 ? NULL : &spec);
|
||||||
} while (r == -1 && errno == EINTR);
|
} while (r == -1 && errno == EINTR);
|
||||||
|
|
||||||
// Deal with event in main thread.
|
// Deal with event in main thread.
|
||||||
self->WakeupMainThread();
|
WakeupMainThread();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void NodeBindingsMac::OnWatcherQueueChanged(uv_loop_t* loop) {
|
|
||||||
NodeBindingsMac* self = static_cast<NodeBindingsMac*>(loop->data);
|
|
||||||
|
|
||||||
DCHECK(!self->is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
||||||
|
|
||||||
// We need to break the io polling in the kqueue thread when loop's watcher
|
|
||||||
// queue changes, otherwise new events cannot be notified.
|
|
||||||
uv_async_send(&self->dummy_uv_handle_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -7,11 +7,6 @@
|
||||||
|
|
||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
#include "common/node_bindings.h"
|
#include "common/node_bindings.h"
|
||||||
#include "vendor/node/deps/uv/include/uv.h"
|
|
||||||
|
|
||||||
namespace base {
|
|
||||||
class MessageLoop;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
@ -20,43 +15,12 @@ class NodeBindingsMac : public NodeBindings {
|
||||||
explicit NodeBindingsMac(bool is_browser);
|
explicit NodeBindingsMac(bool is_browser);
|
||||||
virtual ~NodeBindingsMac();
|
virtual ~NodeBindingsMac();
|
||||||
|
|
||||||
virtual void PrepareMessageLoop() OVERRIDE;
|
|
||||||
virtual void RunMessageLoop() OVERRIDE;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Run the libuv loop for once.
|
virtual void PollEvents() OVERRIDE;
|
||||||
void UvRunOnce();
|
|
||||||
|
|
||||||
// Make the main thread run libuv loop.
|
|
||||||
void WakeupMainThread();
|
|
||||||
|
|
||||||
// Thread to poll uv events.
|
|
||||||
static void EmbedThreadRunner(void *arg);
|
|
||||||
|
|
||||||
// Called when uv's watcher queue changes.
|
|
||||||
static void OnWatcherQueueChanged(uv_loop_t* loop);
|
|
||||||
|
|
||||||
// Main thread's MessageLoop.
|
|
||||||
base::MessageLoop* message_loop_;
|
|
||||||
|
|
||||||
// Main thread's libuv loop.
|
|
||||||
uv_loop_t* uv_loop_;
|
|
||||||
|
|
||||||
// Kqueue to poll for uv's backend fd.
|
// Kqueue to poll for uv's backend fd.
|
||||||
int kqueue_;
|
int kqueue_;
|
||||||
|
|
||||||
// Dummy handle to make uv's loop not quit.
|
|
||||||
uv_async_t dummy_uv_handle_;
|
|
||||||
|
|
||||||
// Thread for polling events.
|
|
||||||
uv_thread_t embed_thread_;
|
|
||||||
|
|
||||||
// Whether we're done.
|
|
||||||
bool embed_closed_;
|
|
||||||
|
|
||||||
// Semaphore to wait for main loop in the embed thread.
|
|
||||||
uv_sem_t embed_sem_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(NodeBindingsMac);
|
DISALLOW_COPY_AND_ASSIGN(NodeBindingsMac);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue