Permalink
Browse files

coroutine, feat: hold the process while at least one worker is working.

  • Loading branch information...
xicilion committed Nov 28, 2017
1 parent 38bdf89 commit e20a32ea10fac7dd49aea8ea42afa1bb949f25f4
Showing with 45 additions and 23 deletions.
  1. +2 −6 fibjs/include/Isolate.h
  2. +28 −17 fibjs/src/base/Runtime.cpp
  3. +5 −0 test/process/exec20.js
  4. +4 −0 test/process/exec20_worker.js
  5. +6 −0 test/process_test.js
View
@@ -90,15 +90,12 @@ class Isolate : public exlib::linkitem {
void start_profiler();
void Ref()
{
m_pendding.inc();
}
void Ref();
void Unref(int32_t hr = 0);
public:
int32_t m_id;
int32_t m_hr;
exlib::string m_fname;
QuickArray<void*> m_classInfo;
@@ -129,7 +126,6 @@ class Isolate : public exlib::linkitem {
int32_t m_currentFibers;
int32_t m_idleFibers;
exlib::atomic m_pendding;
exlib::atomic m_has_timer;
int64_t m_fid;
View
@@ -58,6 +58,7 @@ class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
exlib::LockedList<Isolate> s_isolates;
exlib::atomic s_iso_id;
exlib::atomic s_iso_ref;
Isolate::rt_base::rt_base(Isolate* cur)
: m_isolate((cur ? cur : Isolate::current()))
@@ -103,6 +104,7 @@ void init_proc(void* p)
Isolate::Isolate(exlib::string fname)
: m_id((int32_t)s_iso_id.inc())
, m_hr(0)
, m_test(NULL)
, m_currentFibers(0)
, m_idleFibers(0)
@@ -206,25 +208,34 @@ void Isolate::init()
m_AssertionError.Reset(m_isolate, AssertionError);
}
static result_t syncExit(Isolate* isolate)
{
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;
t._emit("beforeExit", &code, 1, r);
if (s_iso_ref == 1)
if (isolate->m_hr == 0)
process_base::exit();
else
process_base::exit(isolate->m_hr);
}
void Isolate::Ref()
{
s_iso_ref.inc();
}
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);
}
}
if (s_iso_ref.dec() == 0) {
Isolate* isolate = s_isolates.head();
isolate->m_hr = hr;
syncCall(isolate, syncExit, isolate);
}
}
void Isolate::start_profiler()
View
@@ -0,0 +1,5 @@
var coroutine = require('coroutine');
var path = require('path');
process.exitCode = 20;
new coroutine.Worker(path.join(__dirname, 'exec20_worker.js'));
@@ -0,0 +1,4 @@
var coroutine = require('coroutine');
coroutine.sleep(50);
console.log(2000);
View
@@ -134,6 +134,12 @@ describe('process', () => {
assert.equal(p.wait(), 19);
});
it("worker", () => {
var p = process.open(cmd, [path.join(__dirname, 'process', 'exec20.js')]);
assert.equal(p.readLine(), "2000");
assert.equal(p.wait(), 20);
});
it("bugfix: multi fiber async", () => {
var p = process.open(cmd, [path.join(__dirname, 'process', 'exec12.js')]);
assert.equal(p.readLine(), "600");

0 comments on commit e20a32e

Please sign in to comment.