Skip to content

Commit 09a0ed3

Browse files
committedMar 12, 2016
Some fixes for Fiber.
1 parent 2aa0283 commit 09a0ed3

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed
 

‎machine/builtin/fiber.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,23 @@ namespace rubinius {
5353
void Fiber::start_on_stack() {
5454
#ifdef RBX_FIBER_ENABLED
5555
VM* vm = VM::current();
56-
State state(vm);
56+
State state_obj(vm), *state = &state_obj;
5757

58-
Fiber* fib = Fiber::current(&state);
58+
Fiber* fib = Fiber::current(state);
5959

6060
// Reset the current fiber again to reset the stack limits so
6161
// we can properly detect stack overflows
6262
vm->set_current_fiber(fib);
6363

6464
Array* result = nil<Array>();
65-
Object* obj = fib->starter()->send(&state, NULL, state.globals().sym_call.get(), fib->value(), cNil, false);
65+
Object* obj = fib->starter()->send(state, NULL, G(sym_call), fib->value(), cNil, false);
66+
6667
// GC has run! Don't use stack vars!
6768

68-
fib = Fiber::current(&state);
69+
fib = Fiber::current(state);
6970
fib->status_ = Fiber::eDead;
7071
fib->dead_ = cTrue;
72+
fib->set_call_frame(0);
7173

7274
Fiber* dest = fib->prev();
7375

@@ -80,19 +82,20 @@ namespace rubinius {
8082
// of returning, so we can deal with it in the same way
8183
// as *args from #yield, #resume, and #transfer
8284
if(obj) {
83-
result = Array::create(&state, 1);
84-
result->set(&state, 0, obj);
85+
result = Array::create(state, 1);
86+
result->set(state, 0, obj);
8587
} else {
86-
if(state.vm()->thread_state()->raise_reason() == cException) {
87-
dest->exception(&state, state.vm()->thread_state()->current_exception());
88+
if(state->vm()->thread_state()->raise_reason() == cException) {
89+
dest->exception(state, state->vm()->thread_state()->current_exception());
8890
}
8991
}
9092

91-
dest->run();
92-
dest->value(&state, result);
93-
state.vm()->set_current_fiber(dest);
93+
dest->run(state);
94+
dest->value(state, result);
95+
96+
dest->data_->switch_and_orphan(state, fib->data_);
9497

95-
dest->data_->switch_and_orphan(&state, fib->data_);
98+
// TODO: CallFrame: return from this function
9699

97100
assert(0 && "fatal start_on_stack error");
98101
#else
@@ -123,15 +126,15 @@ namespace rubinius {
123126
}
124127

125128
if(status_ == Fiber::eDead || data_->dead_p()) {
126-
Exception::fiber_error(state, "dead fiber called");
129+
Exception::raise_fiber_error(state, "dead fiber called");
127130
}
128131

129132
if(!prev_->nil_p()) {
130-
Exception::fiber_error(state, "double resume");
133+
Exception::raise_fiber_error(state, "double resume");
131134
}
132135

133136
if(data_->thread() && data_->thread() != state->vm()) {
134-
Exception::fiber_error(state, "cross thread fiber resuming is illegal");
137+
Exception::raise_fiber_error(state, "cross thread fiber resuming is illegal");
135138
}
136139

137140
Array* val = args.as_array(state);
@@ -140,10 +143,9 @@ namespace rubinius {
140143
Fiber* cur = Fiber::current(state);
141144
prev(state, cur);
142145

143-
cur->sleep(calling_environment);
146+
cur->sleep(state->vm()->call_frame());
144147

145-
run();
146-
state->vm()->set_current_fiber(this);
148+
run(state);
147149

148150
data_->switch_to(state, cur->data_);
149151

@@ -181,11 +183,11 @@ namespace rubinius {
181183
}
182184

183185
if(status_ == Fiber::eDead || data_->dead_p()) {
184-
Exception::fiber_error(state, "dead fiber called");
186+
Exception::raise_fiber_error(state, "dead fiber called");
185187
}
186188

187189
if(data_->thread() && data_->thread() != state->vm()) {
188-
Exception::fiber_error(state, "cross thread fiber resuming is illegal");
190+
Exception::raise_fiber_error(state, "cross thread fiber resuming is illegal");
189191
}
190192

191193
Array* val = args.as_array(state);
@@ -197,10 +199,9 @@ namespace rubinius {
197199

198200
prev(state, root);
199201

200-
cur->sleep(calling_environment);
202+
cur->sleep(state->vm()->call_frame());
201203

202-
run();
203-
state->vm()->set_current_fiber(this);
204+
run(state);
204205

205206
data_->switch_to(state, cur->data_);
206207

@@ -239,18 +240,17 @@ namespace rubinius {
239240
assert(cur != dest_fib);
240241

241242
if(cur->root_) {
242-
Exception::fiber_error(state, "can't yield from root fiber");
243+
Exception::raise_fiber_error(state, "can't yield from root fiber");
243244
}
244245

245246
cur->prev(state, nil<Fiber>());
246247

247248
Array* val = args.as_array(state);
248249
dest_fib->value(state, val);
249250

250-
cur->sleep(calling_environment);
251+
cur->sleep(state->vm()->call_frame());
251252

252-
dest_fib->run();
253-
state->vm()->set_current_fiber(dest_fib);
253+
dest_fib->run(state);
254254

255255
dest_fib->data_->switch_to(state, cur->data_);
256256

‎machine/builtin/fiber.hpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,23 @@ namespace rubinius {
5050
return data_->call_frame();
5151
}
5252

53-
void sleep(CallFrame* cf) {
54-
if(cf && !data_) rubinius::bug("bad fiber");
55-
data_->set_call_frame(cf);
53+
void set_call_frame(CallFrame* call_frame) {
54+
if(data_) {
55+
data_->set_call_frame(call_frame);
56+
}
57+
}
58+
59+
void sleep(CallFrame* call_frame) {
60+
if(call_frame && !data_) rubinius::bug("bad fiber");
61+
data_->set_call_frame(call_frame);
5662
status_ = eSleeping;
5763
}
5864

59-
void run() {
60-
if(data_) data_->set_call_frame(0);
65+
void run(STATE) {
66+
state->vm()->set_current_fiber(this);
67+
if(data_) {
68+
state->vm()->set_call_frame(data_->call_frame());
69+
}
6170
status_ = eRunning;
6271
}
6372

‎machine/fiber_data.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ namespace rubinius {
103103
return call_frame_;
104104
}
105105

106-
void set_call_frame(CallFrame* cf) {
107-
call_frame_ = cf;
106+
void set_call_frame(CallFrame* call_frame) {
107+
call_frame_ = call_frame;
108108
}
109109

110110
VM* thread() const {

0 commit comments

Comments
 (0)
Please sign in to comment.