Skip to content

Commit

Permalink
core, refactor: move RequestInterrupt into isolate.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 15, 2017
1 parent eb4d6bc commit 7c9a6cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
3 changes: 3 additions & 0 deletions fibjs/include/Isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Isolate : public exlib::linkitem {
static Isolate* current();
void init();

void RequestInterrupt(v8::InterruptCallback callback, void* data);

v8::Local<v8::String> NewString(const char* data, int length = -1)
{
return fibjs::NewString(m_isolate, data, length);
Expand Down Expand Up @@ -111,6 +113,7 @@ class Isolate : public exlib::linkitem {
int32_t m_idleFibers;

exlib::atomic m_pendding;
exlib::atomic m_has_timer;

int64_t m_fid;

Expand Down
16 changes: 16 additions & 0 deletions fibjs/src/base/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,20 @@ void Isolate::init()
m_topSandbox->initRoot();
}

void InvokeApiInterruptCallbacks(v8::Isolate* isolate);
static result_t js_timer(Isolate* isolate)
{
JSFiber::scope s;
isolate->m_has_timer = 0;
InvokeApiInterruptCallbacks(isolate->m_isolate);
return 0;
}

void Isolate::RequestInterrupt(v8::InterruptCallback callback, void* data)
{
m_isolate->RequestInterrupt(callback, data);
if (m_has_timer.CompareAndSwap(0, 1) == 0)
syncCall(this, js_timer, this);
}

} /* namespace fibjs */
27 changes: 3 additions & 24 deletions fibjs/src/global/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

namespace fibjs {

void InvokeApiInterruptCallbacks(v8::Isolate* isolate);

#define TIMEOUT_MAX 2147483647 // 2^31-1

class JSTimer : public Timer {
Expand All @@ -42,14 +40,9 @@ class JSTimer : public Timer {
{
Isolate* isolate = holder();

if (m_hr) {
isolate->m_isolate->RequestInterrupt(_InterruptCallback, this);

if (m_has_worker.CompareAndSwap(0, 1) == 0) {
Ref();
syncCall(isolate, js_worker, this);
}
} else
if (m_hr)
isolate->RequestInterrupt(_InterruptCallback, this);
else
syncCall(isolate, _callback, this);
}

Expand Down Expand Up @@ -92,22 +85,8 @@ class JSTimer : public Timer {
}
}

static result_t js_worker(JSTimer* pThis)
{
JSFiber::scope s;
Isolate* isolate = pThis->holder();

InvokeApiInterruptCallbacks(isolate->m_isolate);

pThis->m_has_worker = 0;
pThis->Unref();

return 0;
}

private:
bool m_hr;
exlib::atomic m_has_worker;
bool m_clear_pendding;
QuickArray<v8::Global<v8::Value>> m_argv;
v8::Global<v8::Function> m_callback;
Expand Down

0 comments on commit 7c9a6cb

Please sign in to comment.