Skip to content

Commit b4d5c34

Browse files
committed
Distributed ThreadNexus to Machine, Threads, ThreadState.
1 parent 236ab47 commit b4d5c34

20 files changed

+809
-967
lines changed

machine/capi/thread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ extern "C" {
187187

188188
ThreadState* state = env->state();
189189
ENTER_CAPI(state);
190-
state->managed_phase(state);
190+
state->managed_phase();
191191

192192
void* ret = (*func)(data);
193193

194-
env->state()->unmanaged_phase(state);
194+
env->state()->unmanaged_phase();
195195
LEAVE_CAPI(env->state());
196196

197197
return ret;

machine/class/fiber.cpp

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -176,65 +176,62 @@ namespace rubinius {
176176

177177
SET_THREAD_UNWIND(state);
178178

179-
if(state->thread_unwinding_p()) {
180-
// TODO halt
181-
return 0;
182-
}
183-
184-
RUBINIUS_THREAD_START(
185-
const_cast<RBX_DTRACE_CHAR_P>(
186-
state->name().c_str()), state->fiber()->fiber_id()->to_native(), 0);
179+
if(!state->thread_unwinding_p()) {
180+
RUBINIUS_THREAD_START(
181+
const_cast<RBX_DTRACE_CHAR_P>(
182+
state->name().c_str()), state->fiber()->fiber_id()->to_native(), 0);
187183

188-
state->fiber()->pid(state, Fixnum::from(gettid()));
189-
190-
if(state->configuration()->log_fiber_lifetime.value) {
191-
logger::write("fiber: run: %s, %d, %#x",
192-
state->name().c_str(), state->fiber()->pid()->to_native(),
193-
(intptr_t)pthread_self());
194-
}
184+
state->fiber()->pid(state, Fixnum::from(gettid()));
195185

196-
NativeMethod::init_thread(state);
186+
if(state->configuration()->log_fiber_lifetime.value) {
187+
logger::write("fiber: run: %s, %d, %#x",
188+
state->name().c_str(), state->fiber()->pid()->to_native(),
189+
(intptr_t)pthread_self());
190+
}
197191

198-
state->fiber()->suspend_and_continue(state);
192+
NativeMethod::init_thread(state);
199193

200-
Object* value = state->fiber()->block()->send(state, G(sym_call),
201-
as<Array>(state->thread()->fiber_value()), state->fiber()->block());
202-
state->set_call_frame(nullptr);
194+
state->fiber()->suspend_and_continue(state);
203195

204-
if(value) {
205-
state->fiber()->value(state, value);
206-
state->thread()->fiber_value(state, value);
207-
} else {
208-
state->fiber()->value(state, cNil);
209-
state->thread()->fiber_value(state, cNil);
210-
}
196+
Object* value = state->fiber()->block()->send(state, G(sym_call),
197+
as<Array>(state->thread()->fiber_value()), state->fiber()->block());
198+
state->set_call_frame(nullptr);
211199

212-
if(state->unwind_state()->raise_reason() != cFiberCancel) {
213-
if(state->fiber()->status() == eTransfer) {
214-
// restart the root Fiber
215-
state->thread()->fiber()->invoke_context(state);
216-
state->thread()->fiber()->restart(state);
200+
if(value) {
201+
state->fiber()->value(state, value);
202+
state->thread()->fiber_value(state, value);
217203
} else {
218-
state->fiber()->invoke_context()->fiber()->restart(state);
204+
state->fiber()->value(state, cNil);
205+
state->thread()->fiber_value(state, cNil);
219206
}
220-
}
221207

222-
{
223-
std::lock_guard<std::mutex> guard(state->fiber_wait_mutex());
208+
if(state->unwind_state()->raise_reason() != cFiberCancel) {
209+
if(state->fiber()->status() == eTransfer) {
210+
// restart the root Fiber
211+
state->thread()->fiber()->invoke_context(state);
212+
state->thread()->fiber()->restart(state);
213+
} else {
214+
state->fiber()->invoke_context()->fiber()->restart(state);
215+
}
216+
}
217+
218+
{
219+
std::lock_guard<std::mutex> guard(state->fiber_wait_mutex());
224220

225-
state->fiber()->status(eDead);
226-
state->set_suspended();
221+
state->fiber()->status(eDead);
222+
state->set_suspended();
223+
}
227224
}
228225

229-
state->unmanaged_phase(state);
226+
state->unmanaged_phase();
230227

231228
NativeMethod::cleanup_thread(state);
232229

233230
if(state->configuration()->log_fiber_lifetime.value) {
234231
logger::write("fiber: exit: %s %fs", state->name().c_str(), state->run_time());
235232
}
236233

237-
state->machine()->thread_nexus()->remove_thread_state(state);
234+
state->threads()->remove_thread_state(state);
238235
state->set_thread_dead();
239236

240237
RUBINIUS_THREAD_STOP(
@@ -277,7 +274,7 @@ namespace rubinius {
277274
std::ostringstream name;
278275
name << "fiber." << fiber->fiber_id()->to_native();
279276

280-
fiber->thread_state(state->thread_nexus()->create_thread_state(state->machine(), name.str().c_str()));
277+
fiber->thread_state(state->threads()->create_thread_state(name.str().c_str()));
281278

282279
fiber->thread_state()->set_kind(ThreadState::eFiber);
283280
fiber->thread_state()->set_suspending();
@@ -466,15 +463,35 @@ namespace rubinius {
466463
}
467464

468465
Array* Fiber::s_list(STATE) {
469-
return state->machine()->vm_fibers(state);
466+
Array* fibers = Array::create(state, 0);
467+
468+
state->threads()->each(state, [fibers](STATE, ThreadState* thread_state) {
469+
if(thread_state->kind() == ThreadState::eFiber
470+
&& !thread_state->fiber()->nil_p()
471+
&& thread_state->fiber()->status() != Fiber::eDead) {
472+
fibers->append(state, thread_state->fiber());
473+
}
474+
});
475+
476+
return fibers;
470477
}
471478

472479
Fiber* Fiber::s_main(STATE) {
473480
return state->thread()->fiber();
474481
}
475482

476483
Fixnum* Fiber::s_count(STATE) {
477-
return state->machine()->vm_fibers_count(state);
484+
intptr_t count = 0;
485+
486+
state->threads()->each(state, [&](STATE, ThreadState* thread_state) {
487+
if(thread_state->kind() == ThreadState::eFiber
488+
&& !thread_state->fiber()->nil_p()
489+
&& thread_state->fiber()->status() != Fiber::eDead) {
490+
count++;
491+
}
492+
});
493+
494+
return Fixnum::from(count);
478495
}
479496

480497
void Fiber::finalize(STATE, Fiber* fiber) {

machine/class/native_function.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ namespace rubinius {
315315

316316
ThreadState* state = env->state();
317317

318-
state->managed_phase(state);
318+
state->managed_phase();
319319

320320
Array* args = Array::create(state, stub->arg_count);
321321
OnStack<1> os(state, args);
@@ -545,7 +545,7 @@ namespace rubinius {
545545
break;
546546
}
547547

548-
state->unmanaged_phase(state);
548+
state->unmanaged_phase();
549549
}
550550

551551

@@ -989,104 +989,104 @@ namespace rubinius {
989989
env->set_current_call_frame(state->call_frame());
990990

991991
state->interrupt_with_signal();
992-
state->unmanaged_phase(state);
992+
state->unmanaged_phase();
993993

994994
switch(ffi_data_local->ret_info.type) {
995995
case RBX_FFI_TYPE_CHAR: {
996996
ffi_arg result = 0;
997997
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
998-
state->managed_phase(state);
998+
state->managed_phase();
999999
ret = Fixnum::from((intptr_t)result);
10001000
break;
10011001
}
10021002
case RBX_FFI_TYPE_UCHAR: {
10031003
ffi_arg result = 0;
10041004
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1005-
state->managed_phase(state);
1005+
state->managed_phase();
10061006
ret = Fixnum::from((intptr_t)result);
10071007
break;
10081008
}
10091009
case RBX_FFI_TYPE_BOOL: {
10101010
ffi_arg result = 0;
10111011
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1012-
state->managed_phase(state);
1012+
state->managed_phase();
10131013
ret = RBOOL(result);
10141014
break;
10151015
}
10161016
case RBX_FFI_TYPE_SHORT: {
10171017
ffi_arg result = 0;
10181018
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1019-
state->managed_phase(state);
1019+
state->managed_phase();
10201020
ret = Fixnum::from((intptr_t)result);
10211021
break;
10221022
}
10231023
case RBX_FFI_TYPE_USHORT: {
10241024
ffi_arg result = 0;
10251025
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1026-
state->managed_phase(state);
1026+
state->managed_phase();
10271027
ret = Fixnum::from((intptr_t)result);
10281028
break;
10291029
}
10301030
case RBX_FFI_TYPE_INT: {
10311031
ffi_arg result = 0;
10321032
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1033-
state->managed_phase(state);
1033+
state->managed_phase();
10341034
ret = Integer::from(state, (intptr_t)result);
10351035
break;
10361036
}
10371037
case RBX_FFI_TYPE_UINT: {
10381038
ffi_arg result = 0;
10391039
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1040-
state->managed_phase(state);
1040+
state->managed_phase();
10411041
ret = Integer::from(state, (unsigned int)result);
10421042
break;
10431043
}
10441044
case RBX_FFI_TYPE_LONG: {
10451045
long result = 0;
10461046
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1047-
state->managed_phase(state);
1047+
state->managed_phase();
10481048
ret = Integer::from(state, result);
10491049
break;
10501050
}
10511051
case RBX_FFI_TYPE_ULONG: {
10521052
unsigned long result = 0;
10531053
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1054-
state->managed_phase(state);
1054+
state->managed_phase();
10551055
ret = Integer::from(state, result);
10561056
break;
10571057
}
10581058
case RBX_FFI_TYPE_FLOAT: {
10591059
float result = 0.0;
10601060
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1061-
state->managed_phase(state);
1061+
state->managed_phase();
10621062
ret = Float::create(state, (double)result);
10631063
break;
10641064
}
10651065
case RBX_FFI_TYPE_DOUBLE: {
10661066
double result = 0.0;
10671067
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1068-
state->managed_phase(state);
1068+
state->managed_phase();
10691069
ret = Float::create(state, result);
10701070
break;
10711071
}
10721072
case RBX_FFI_TYPE_LONG_LONG: {
10731073
long long result = 0;
10741074
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1075-
state->managed_phase(state);
1075+
state->managed_phase();
10761076
ret = Integer::from(state, result);
10771077
break;
10781078
}
10791079
case RBX_FFI_TYPE_ULONG_LONG: {
10801080
unsigned long long result = 0;
10811081
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1082-
state->managed_phase(state);
1082+
state->managed_phase();
10831083
ret = Integer::from(state, result);
10841084
break;
10851085
}
10861086
case RBX_FFI_TYPE_PTR: {
10871087
void* result = NULL;
10881088
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1089-
state->managed_phase(state);
1089+
state->managed_phase();
10901090
if(result == NULL) {
10911091
ret = cNil;
10921092
} else {
@@ -1098,7 +1098,7 @@ namespace rubinius {
10981098
ffi_arg result = 0;
10991099
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
11001100

1101-
state->managed_phase(state);
1101+
state->managed_phase();
11021102

11031103
Array* ary = Array::create(state, 1);
11041104
ary->set(state, 0, Integer::from(state, (intptr_t)result));
@@ -1109,7 +1109,7 @@ namespace rubinius {
11091109
case RBX_FFI_TYPE_CALLBACK: {
11101110
void* result = NULL;
11111111
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1112-
state->managed_phase(state);
1112+
state->managed_phase();
11131113
if(result == NULL) {
11141114
ret = cNil;
11151115
} else {
@@ -1128,7 +1128,7 @@ namespace rubinius {
11281128
case RBX_FFI_TYPE_STRING: {
11291129
char* result = NULL;
11301130
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1131-
state->managed_phase(state);
1131+
state->managed_phase();
11321132
if(result == NULL) {
11331133
ret = cNil;
11341134
} else {
@@ -1143,7 +1143,7 @@ namespace rubinius {
11431143
Object* p = cNil;
11441144

11451145
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1146-
state->managed_phase(state);
1146+
state->managed_phase();
11471147

11481148
if(result) {
11491149
s = String::create(state, result);
@@ -1161,7 +1161,7 @@ namespace rubinius {
11611161
case RBX_FFI_TYPE_VOID: {
11621162
ffi_arg result = 0;
11631163
ffi_call(cif, FFI_FN(ffi_data_local->ep), &result, values);
1164-
state->managed_phase(state);
1164+
state->managed_phase();
11651165
ret = cNil;
11661166
break;
11671167
}

0 commit comments

Comments
 (0)