Permalink
Browse files

core, refactor: support ref/unref in isolate, refactor the logic of h…

…olding process.
  • Loading branch information...
xicilion committed Nov 27, 2017
1 parent b617bb8 commit ff75686678557082de2137d687bda951c5f52405
@@ -27,7 +27,7 @@ class AsyncEvent : public exlib::Task_base {
public:
void sync(Isolate* isolate)
{
isolate->m_pendding.inc();
isolate->Ref();
isolate->m_jobs.putTail(this);
isolate->m_sem.post();
}
@@ -357,15 +357,15 @@ class AsyncCallBack : public AsyncEvent {
AsyncCallBack(v8::Local<v8::Function> cb)
{
m_isolate = Isolate::current();
m_isolate->m_pendding.inc();
m_isolate->Ref();
m_cb.Reset(m_isolate->m_isolate, cb);
}
AsyncCallBack(object_base* pThis, v8::Local<v8::Function> cb);
~AsyncCallBack()
{
m_isolate->m_pendding.dec();
m_isolate->Unref();
m_cb.Reset();
}
View
@@ -90,6 +90,13 @@ class Isolate : public exlib::linkitem {
void start_profiler();
void Ref()
{
m_pendding.inc();
}
void Unref(int32_t hr = 0);
public:
int32_t m_id;
exlib::string m_fname;
View
@@ -10,6 +10,7 @@
#include "SandBox.h"
#include "console.h"
#include "LruCache.h"
#include "Trigger.h"
#include "ifs/global.h"
#include "ifs/process.h"
#include "ifs/coroutine.h"
@@ -205,14 +206,35 @@ void Isolate::init()
m_AssertionError.Reset(m_isolate, AssertionError);
}
void Isolate::Unref(int32_t hr)
{
if (m_pendding.dec() == 0)
if (m_id == 1) {
v8::HandleScope handle_scope(m_isolate);
JSFiber::scope s;
JSTrigger t(m_isolate, process_base::class_info().getModule(this));
v8::Local<v8::Value> code = v8::Number::New(m_isolate, m_exitCode);
bool r;
Ref();
t._emit("beforeExit", &code, 1, r);
if (m_pendding.dec() == 0) {
if (hr >= 0)
process_base::exit();
else
process_base::exit(hr);
}
}
}
void Isolate::start_profiler()
{
if (g_prof) {
char name[32];
obj_ptr<Timer_base> tm;
sprintf(name, "fibjs-%08x.log", (uint32_t)(intptr_t)this);
profiler_base::start(name, -1, g_prof_interval, tm);
m_pendding.dec();
Unref();
}
}
void InvokeApiInterruptCallbacks(v8::Isolate* isolate);
@@ -125,7 +125,7 @@ AsyncCallBack::AsyncCallBack(object_base* pThis, v8::Local<v8::Function> cb)
{
m_pThis = pThis;
m_isolate = pThis->holder();
m_isolate->m_pendding.inc();
m_isolate->Ref();
m_cb.Reset(m_isolate->m_isolate, cb);
}
@@ -7,7 +7,6 @@
#include "object.h"
#include "Fiber.h"
#include "Trigger.h"
#include "ifs/os.h"
#include "ifs/process.h"
#include "v8_api.h"
@@ -71,23 +70,7 @@ void JSFiber::fiber_proc(void* p)
hr = ae->js_invoke();
}
if (isolate->m_pendding.dec() == 0)
if (isolate->m_id == 1) {
v8::HandleScope handle_scope(isolate->m_isolate);
JSFiber::scope s;
JSTrigger t(isolate->m_isolate, process_base::class_info().getModule(isolate));
v8::Local<v8::Value> code = v8::Number::New(isolate->m_isolate, isolate->m_exitCode);
bool r;
isolate->m_pendding.inc();
t._emit("beforeExit", &code, 1, r);
if (isolate->m_pendding.dec() == 0) {
if (hr >= 0)
process_base::exit();
else
process_base::exit(hr);
}
}
isolate->Unref(hr);
}
isolate->m_currentFibers--;
@@ -122,7 +105,7 @@ void JSFiber::start()
set_caller(JSFiber::current());
isolate->m_pendding.inc();
isolate->Ref();
isolate->m_jobs.putTail(this);
isolate->m_sem.post();
Ref();
@@ -31,7 +31,7 @@ class JSTimer : public Timer {
for (int i = 0; i < nArgCount; i++)
m_argv[i].Reset(isolate->m_isolate, args[i]);
isolate->m_pendding.inc();
isolate->Ref();
m_callback.Reset(isolate->m_isolate, callback);
}
@@ -81,7 +81,7 @@ class JSTimer : public Timer {
m_callback.Reset();
if (!m_clear_pendding) {
m_clear_pendding = true;
holder()->m_pendding.dec();
holder()->Unref();
}
}
@@ -262,7 +262,7 @@ result_t process_base::exit()
v8::Local<v8::Value> v = v8::Number::New(isolate->m_isolate, code);
bool r;
isolate->m_pendding.inc();
isolate->Ref();
t._emit("exit", &v, 1, r);
flushLog();

0 comments on commit ff75686

Please sign in to comment.