Skip to content

Commit

Permalink
core, refactor: revert to the previous code (v8 fixed the deadlock er…
Browse files Browse the repository at this point in the history
…ror).
  • Loading branch information
xicilion committed Oct 31, 2017
1 parent 6ddc436 commit b4cd3c1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 71 deletions.
7 changes: 1 addition & 6 deletions fibjs/include/Isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ class Isolate : public exlib::linkitem {
return m_isolate->GetCurrentContext();
}

bool IsInUse()
{
return m_in_use != 0;
}

void start_profiler();

public:
Expand Down Expand Up @@ -126,7 +121,7 @@ class Isolate : public exlib::linkitem {
int32_t m_idleFibers;

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

int64_t m_fid;

Expand Down
2 changes: 2 additions & 0 deletions fibjs/include/v8_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace fibjs {

void InvokeApiInterruptCallbacks(v8::Isolate* isolate);

struct V8FrameInfo {
void* entry_fp;
void* handle;
Expand Down
69 changes: 7 additions & 62 deletions fibjs/src/base/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ Isolate::rt_base::rt_base(Isolate* cur)

fb->m_c_entry_fp_ = _fi.entry_fp;
fb->m_handler_ = _fi.handle;

m_isolate->m_in_use = 0;
}

Isolate::rt_base::~rt_base()
{
m_isolate->m_in_use = 1;
}

static void fb_GCCallback(v8::Isolate* js_isolate, v8::GCType type, v8::GCCallbackFlags flags)
Expand Down Expand Up @@ -191,72 +188,20 @@ void Isolate::start_profiler()
m_pendding.dec();
}
}

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)
void InvokeApiInterruptCallbacks(v8::Isolate* isolate);
static result_t js_timer(Isolate* isolate)
{
JSFiber::scope s;
cd->invoke();
cd->Unref();
isolate->m_has_timer = 0;
InvokeApiInterruptCallbacks(isolate->m_isolate);
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)
{
CallbackData* cd = new CallbackData(this, callback, data);
cd->Ref();

if (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 (IsInUse() && !cd->m_invoked) {
cd->Ref();
m_isolate->RequestInterrupt(_InterruptCallback, cd);
}
}

cd->Unref();
m_isolate->RequestInterrupt(callback, data);
if (m_has_timer.CompareAndSwap(0, 1) == 0)
syncCall(this, js_timer, this);
}

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

namespace fibjs {

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

V8FrameInfo save_fi(v8::Isolate* isolate)
{
v8::internal::Isolate* v8_isolate = (v8::internal::Isolate*)isolate;
Expand Down
2 changes: 0 additions & 2 deletions fibjs/src/coroutine/Fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ void JSFiber::fiber_proc(void* p)
v8::HandleScope handle_scope(isolate->m_isolate);
AsyncEvent* ae = (AsyncEvent*)isolate->m_jobs.getHead();

isolate->m_in_use = 1;
hr = ae->js_invoke();
isolate->m_in_use = 0;
}

if (isolate->m_pendding.dec() == 0)
Expand Down
2 changes: 1 addition & 1 deletion vender

0 comments on commit b4cd3c1

Please sign in to comment.