/
fiber_stack.cpp
119 lines (94 loc) · 2.1 KB
/
fiber_stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "vm.hpp"
#include "fiber_stack.hpp"
#include "fiber_data.hpp"
#include "configuration.hpp"
#include "bug.hpp"
#include <stdlib.h>
namespace rubinius {
FiberStack::FiberStack(size_t size)
: address_(0)
, size_(size)
, refs_(0)
, user_(0)
{}
void FiberStack::allocate() {
assert(!address_);
address_ = malloc(size_);
}
void FiberStack::free() {
if(!address_) return;
::free(address_);
address_ = 0;
}
void FiberStack::flush(STATE) {
if(!user_) return;
// TODO assumes higher to lower stack growth.
user_->copy_to_heap(state);
}
void FiberStack::orphan(STATE, FiberData* user) {
if(user == user_) {
user_ = 0;
}
dec_ref();
}
FiberStacks::FiberStacks(VM* thread, SharedState& shared)
: max_stacks_(shared.config.fiber_stacks)
, stack_size_(shared.config.fiber_stack_size)
, thread_(thread)
, trampoline_(0)
{}
FiberStacks::~FiberStacks() {
for(Datas::iterator i = datas_.begin();
i != datas_.end();
++i)
{
(*i)->die();
}
for(Stacks::iterator i = stacks_.begin();
i != stacks_.end();
++i)
{
i->free();
}
}
FiberData* FiberStacks::new_data() {
FiberData* data = new FiberData(thread_);
datas_.push_back(data);
return data;
}
FiberStack* FiberStacks::allocate() {
for(Stacks::iterator i = stacks_.begin();
i != stacks_.end();
++i)
{
if(i->unused_p()) {
i->inc_ref();
return &*i;
}
}
FiberStack* stack = 0;
if(stacks_.size() < max_stacks_) {
stacks_.push_back(FiberStack(stack_size_));
stack = &stacks_.back();
stack->allocate();
} else {
for(Stacks::iterator i = stacks_.begin();
i != stacks_.end();
++i)
{
if(!stack || i->refs() < stack->refs()) {
stack = &*i;
}
}
assert(stack);
}
stack->inc_ref();
return stack;
}
void* FiberStacks::trampoline() {
if(trampoline_ == 0) {
trampoline_ = malloc(cTrampolineSize);
}
return trampoline_;
}
}