Skip to content

Commit b72356b

Browse files
authored
Merge pull request #6 from sedwards2009/windows_fix
Trigger the event loop semaphore at the right time
2 parents 2b2ce83 + 41c633c commit b72356b

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

qode/integration/node_integration.cc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ NodeIntegration::NodeIntegration()
1919
embed_closed_(false) {
2020
}
2121

22+
constexpr uint64_t EVENT_BATCH_TIMEOUT_MS = 8;
23+
constexpr int EVENT_BATCH_SIZE = 16;
24+
2225
NodeIntegration::~NodeIntegration() {
2326
// Quit the embed thread.
2427
embed_closed_ = true;
@@ -63,9 +66,6 @@ void NodeIntegration::UvRunOnce() {
6366

6467
// Deal with uv events.
6568
uv_run(uv_loop_, UV_RUN_NOWAIT);
66-
67-
// Tell the worker thread to continue polling.
68-
uv_sem_post(&embed_sem_);
6969
}
7070

7171
void NodeIntegration::CallNextTick() {
@@ -79,6 +79,19 @@ void NodeIntegration::ReleaseHandleRef() {
7979
void NodeIntegration::WakeupMainThread() {
8080
PostTask([this] {
8181
this->UvRunOnce();
82+
// ^ This also updates the uv_now() timestamp.
83+
uint64_t start_time_ms = uv_now(uv_loop_);
84+
85+
int loop_count = EVENT_BATCH_SIZE;
86+
uint64_t elapsed_ms = 0;
87+
while (loop_count != 0 && (elapsed_ms < EVENT_BATCH_TIMEOUT_MS)) {
88+
loop_count--;
89+
this->UvRunOnce();
90+
elapsed_ms = uv_now(uv_loop_) - start_time_ms;
91+
}
92+
93+
// Tell the worker thread to continue polling.
94+
uv_sem_post(&this->embed_sem_);
8295
});
8396
}
8497

@@ -91,8 +104,6 @@ void NodeIntegration::EmbedThreadRunner(void *arg) {
91104
NodeIntegration* self = static_cast<NodeIntegration*>(arg);
92105

93106
while (true) {
94-
// Wait for the main loop to deal with events.
95-
uv_sem_wait(&self->embed_sem_);
96107
if (self->embed_closed_)
97108
break;
98109

@@ -113,6 +124,9 @@ void NodeIntegration::EmbedThreadRunner(void *arg) {
113124

114125
// Deal with event in main thread.
115126
self->WakeupMainThread();
127+
128+
// Wait for the main loop to deal with events.
129+
uv_sem_wait(&self->embed_sem_);
116130
}
117131
}
118132

qode/integration/node_integration_win.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "qode/integration/node_integration_win.h"
66
#include "uv/src/uv-common.h"
7+
#include <stdio.h>
78

89
// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
910
extern "C" IMAGE_DOS_HEADER __ImageBase;
@@ -70,7 +71,11 @@ void NodeIntegrationWin::PostTask(const std::function<void()>& task) {
7071
::EnterCriticalSection(&lock_);
7172
tasks_[++task_id_] = task;
7273
::LeaveCriticalSection(&lock_);
73-
::PostMessage(message_window_, WM_USER, task_id_, 0L);
74+
BOOL result = ::PostMessage(message_window_, WM_USER, task_id_, 0L);
75+
if (result == 0) {
76+
int lastError = ::GetLastError();
77+
fprintf(stderr, "Qode: PostMessage() failed! LastError=%d\n", lastError);
78+
}
7479
}
7580

7681
void NodeIntegrationWin::OnTask(int id) {

0 commit comments

Comments
 (0)