Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 142 lines (111 sloc) 2.728 kB
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
1 #include "vm.hpp"
2 #include "fiber_stack.hpp"
3 #include "fiber_data.hpp"
4
794333e @evanphx Make stack pool and size configurable
evanphx authored
5 #include "configuration.hpp"
6
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
7 #include "bug.hpp"
8
ae50198 @evanphx Move lowlevel Fiber marking to the Thread level
evanphx authored
9 #include "gc/gc.hpp"
10
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
11 #include <stdlib.h>
12
13 namespace rubinius {
14
15 FiberStack::FiberStack(size_t size)
16 : address_(0)
17 , size_(size)
18 , refs_(0)
19 , user_(0)
20 {}
21
22 void FiberStack::allocate() {
23 assert(!address_);
24 address_ = malloc(size_);
25 }
26
27 void FiberStack::free() {
28 if(!address_) return;
29 ::free(address_);
30 address_ = 0;
31 }
32
33 void FiberStack::flush(STATE) {
34 if(!user_) return;
35
36 // TODO assumes higher to lower stack growth.
37 user_->copy_to_heap(state);
38 }
39
40 void FiberStack::orphan(STATE, FiberData* user) {
75dce8b @evanphx Work out the kinks in the fiber stack setup
evanphx authored
41 if(user == user_) {
42 user_ = 0;
43 }
44
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
45 dec_ref();
46 }
47
c690b3c @evanphx Kill Fibers when their Thread dies
evanphx authored
48 FiberStacks::FiberStacks(VM* thread, SharedState& shared)
794333e @evanphx Make stack pool and size configurable
evanphx authored
49 : max_stacks_(shared.config.fiber_stacks)
50 , stack_size_(shared.config.fiber_stack_size)
c690b3c @evanphx Kill Fibers when their Thread dies
evanphx authored
51 , thread_(thread)
794333e @evanphx Make stack pool and size configurable
evanphx authored
52 , trampoline_(0)
53 {}
54
c690b3c @evanphx Kill Fibers when their Thread dies
evanphx authored
55 FiberStacks::~FiberStacks() {
56 for(Datas::iterator i = datas_.begin();
57 i != datas_.end();
58 ++i)
59 {
1d2e229 @dbussink Revert "Plug memory leak in fibers"
dbussink authored
60 (*i)->die();
c690b3c @evanphx Kill Fibers when their Thread dies
evanphx authored
61 }
62
63 for(Stacks::iterator i = stacks_.begin();
64 i != stacks_.end();
65 ++i)
66 {
67 i->free();
68 }
69 }
70
ae50198 @evanphx Move lowlevel Fiber marking to the Thread level
evanphx authored
71 void FiberStacks::gc_scan(GarbageCollector* gc) {
72 for(Datas::iterator i = datas_.begin();
73 i != datas_.end();
74 ++i)
75 {
76 FiberData* data = *i;
77 if(data->dead_p()) continue;
78
79 AddressDisplacement dis(data->data_offset(),
80 data->data_lower_bound(),
81 data->data_upper_bound());
82
83 if(CallFrame* cf = data->call_frame()) {
84 gc->walk_call_frame(cf, &dis);
85 }
86
87 gc->scan(data->variable_root_buffers(), false, &dis);
88 }
89 }
90
c98174b @evanphx Track the root fiber properly. Fixes #1568
evanphx authored
91 FiberData* FiberStacks::new_data(bool root) {
92 FiberData* data = new FiberData(thread_, root);
c690b3c @evanphx Kill Fibers when their Thread dies
evanphx authored
93 datas_.push_back(data);
94
95 return data;
96 }
97
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
98 FiberStack* FiberStacks::allocate() {
99 for(Stacks::iterator i = stacks_.begin();
100 i != stacks_.end();
101 ++i)
102 {
103 if(i->unused_p()) {
104 i->inc_ref();
105 return &*i;
106 }
107 }
108
109 FiberStack* stack = 0;
110
794333e @evanphx Make stack pool and size configurable
evanphx authored
111 if(stacks_.size() < max_stacks_) {
112 stacks_.push_back(FiberStack(stack_size_));
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
113 stack = &stacks_.back();
114
115 stack->allocate();
116 } else {
117 for(Stacks::iterator i = stacks_.begin();
118 i != stacks_.end();
119 ++i)
120 {
121 if(!stack || i->refs() < stack->refs()) {
122 stack = &*i;
123 }
124 }
125
126 assert(stack);
127 }
128
129 stack->inc_ref();
130
131 return stack;
132 }
75dce8b @evanphx Work out the kinks in the fiber stack setup
evanphx authored
133
134 void* FiberStacks::trampoline() {
135 if(trampoline_ == 0) {
136 trampoline_ = malloc(cTrampolineSize);
137 }
138
139 return trampoline_;
140 }
e38045b @evanphx Restructure how fiber stacks are allocated
evanphx authored
141 }
Something went wrong with that request. Please try again.