Permalink
Browse files

core, refactor: call interrupt callback function directly if possible.

  • Loading branch information...
xicilion committed Oct 17, 2017
1 parent c627387 commit 4ec77e32415de683274ee5e90295a8195e1401a5
Showing with 59 additions and 13 deletions.
  1. +59 −7 fibjs/src/base/Runtime.cpp
  2. +0 −6 fibjs/src/base/v8_api.cpp
View
@@ -12,6 +12,7 @@
#include "LruCache.h"
#include "ifs/global.h"
#include "ifs/process.h"
#include "ifs/coroutine.h"
namespace fibjs {
@@ -171,20 +172,71 @@ void Isolate::init()
m_topSandbox->initRoot();
}
void InvokeApiInterruptCallbacks(v8::Isolate* isolate);
static result_t js_timer(Isolate* isolate)
class CallbackData : public obj_base {
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;
isolate->m_has_timer = 0;
InvokeApiInterruptCallbacks(isolate->m_isolate);
cd->invoke();
cd->Unref();
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)
{
m_isolate->RequestInterrupt(callback, data);
if (m_has_timer.CompareAndSwap(0, 1) == 0)
syncCall(this, js_timer, this);
CallbackData* cd = new CallbackData(this, callback, data);
cd->Ref();
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 */
@@ -19,12 +19,6 @@
namespace fibjs {
void InvokeApiInterruptCallbacks(v8::Isolate* isolate)
{
v8::internal::Isolate* v8_isolate = (v8::internal::Isolate*)isolate;
v8_isolate->InvokeApiInterruptCallbacks();
}
struct V8FrameInfo {
void* entry_fp;
void* handle;

0 comments on commit 4ec77e3

Please sign in to comment.