Skip to content

Commit

Permalink
core, refactor: call interrupt callback function directly if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 17, 2017
1 parent c627387 commit 4ec77e3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
66 changes: 59 additions & 7 deletions fibjs/src/base/Runtime.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "LruCache.h" #include "LruCache.h"
#include "ifs/global.h" #include "ifs/global.h"
#include "ifs/process.h" #include "ifs/process.h"
#include "ifs/coroutine.h"


namespace fibjs { namespace fibjs {


Expand Down Expand Up @@ -171,20 +172,71 @@ void Isolate::init()
m_topSandbox->initRoot(); m_topSandbox->initRoot();
} }


void InvokeApiInterruptCallbacks(v8::Isolate* isolate); class CallbackData : public obj_base {
static result_t js_timer(Isolate* isolate) public:
CallbackData(Isolate* isolate, v8::InterruptCallback callback, void* data)
: m_isolate(isolate)
, m_callback(callback)
, m_data(data)
{
}

void invoke()
{
if (m_invoked.CompareAndSwap(0, 1) == 0)
m_callback(m_isolate->m_isolate, m_data);
}

public:
Isolate* m_isolate;
v8::InterruptCallback m_callback;
void* m_data;
exlib::atomic m_invoked;
};

static result_t js_timer(CallbackData* cd)
{ {
JSFiber::scope s; JSFiber::scope s;
isolate->m_has_timer = 0; cd->invoke();
InvokeApiInterruptCallbacks(isolate->m_isolate); cd->Unref();
return 0; return 0;
} }


static void _InterruptCallback(v8::Isolate* isolate, void* data)
{
CallbackData* cd = (CallbackData*)data;
cd->invoke();
cd->Unref();
}

void Isolate::RequestInterrupt(v8::InterruptCallback callback, void* data) void Isolate::RequestInterrupt(v8::InterruptCallback callback, void* data)
{ {
m_isolate->RequestInterrupt(callback, data); CallbackData* cd = new CallbackData(this, callback, data);
if (m_has_timer.CompareAndSwap(0, 1) == 0) cd->Ref();
syncCall(this, js_timer, this);
if (m_isolate->IsInUse()) {
cd->Ref();
m_isolate->RequestInterrupt(_InterruptCallback, cd);

exlib::OSThread::sleep(1);

if (!cd->m_invoked) {
cd->Ref();
syncCall(this, js_timer, cd);
}
} else {
cd->Ref();
syncCall(this, js_timer, cd);

exlib::OSThread::sleep(1);

if (!cd->m_invoked) {
cd->Ref();
m_isolate->RequestInterrupt(_InterruptCallback, cd);
}
}

cd->Unref();
} }


} /* namespace fibjs */ } /* namespace fibjs */
6 changes: 0 additions & 6 deletions fibjs/src/base/v8_api.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@


namespace fibjs { namespace fibjs {


void InvokeApiInterruptCallbacks(v8::Isolate* isolate)
{
v8::internal::Isolate* v8_isolate = (v8::internal::Isolate*)isolate;
v8_isolate->InvokeApiInterruptCallbacks();
}

struct V8FrameInfo { struct V8FrameInfo {
void* entry_fp; void* entry_fp;
void* handle; void* handle;
Expand Down

0 comments on commit 4ec77e3

Please sign in to comment.