Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (94 sloc) 2.154 kb
e38045b Evan Phoenix Restructure how fiber stacks are allocated
evanphx authored
1 #include "vm.hpp"
2 #include "fiber_stack.hpp"
3 #include "fiber_data.hpp"
4
794333e Evan Phoenix Make stack pool and size configurable
evanphx authored
5 #include "configuration.hpp"
6
e38045b Evan Phoenix Restructure how fiber stacks are allocated
evanphx authored
7 #include "bug.hpp"
8
9 #include <stdlib.h>
10
11 namespace rubinius {
12
13 FiberStack::FiberStack(size_t size)
14 : address_(0)
15 , size_(size)
16 , refs_(0)
17 , user_(0)
18 {}
19
20 void FiberStack::allocate() {
21 assert(!address_);
22 address_ = malloc(size_);
23 }
24
25 void FiberStack::free() {
26 if(!address_) return;
27 ::free(address_);
28 address_ = 0;
29 }
30
31 void FiberStack::flush(STATE) {
32 if(!user_) return;
33
34 // TODO assumes higher to lower stack growth.
35 user_->copy_to_heap(state);
36 }
37
38 void FiberStack::orphan(STATE, FiberData* user) {
75dce8b Evan Phoenix Work out the kinks in the fiber stack setup
evanphx authored
39 if(user == user_) {
40 user_ = 0;
41 }
42
e38045b Evan Phoenix Restructure how fiber stacks are allocated
evanphx authored
43 dec_ref();
44 }
45
c690b3c Evan Phoenix Kill Fibers when their Thread dies
evanphx authored
46 FiberStacks::FiberStacks(VM* thread, SharedState& shared)
794333e Evan Phoenix Make stack pool and size configurable
evanphx authored
47 : max_stacks_(shared.config.fiber_stacks)
48 , stack_size_(shared.config.fiber_stack_size)
c690b3c Evan Phoenix Kill Fibers when their Thread dies
evanphx authored
49 , thread_(thread)
794333e Evan Phoenix Make stack pool and size configurable
evanphx authored
50 , trampoline_(0)
51 {}
52
c690b3c Evan Phoenix Kill Fibers when their Thread dies
evanphx authored
53 FiberStacks::~FiberStacks() {
54 for(Datas::iterator i = datas_.begin();
55 i != datas_.end();
56 ++i)
57 {
58 (*i)->die();
59 }
60
61 for(Stacks::iterator i = stacks_.begin();
62 i != stacks_.end();
63 ++i)
64 {
65 i->free();
66 }
67 }
68
69 FiberData* FiberStacks::new_data() {
70 FiberData* data = new FiberData(thread_);
71 datas_.push_back(data);
72
73 return data;
74 }
75
e38045b Evan Phoenix Restructure how fiber stacks are allocated
evanphx authored
76 FiberStack* FiberStacks::allocate() {
77 for(Stacks::iterator i = stacks_.begin();
78 i != stacks_.end();
79 ++i)
80 {
81 if(i->unused_p()) {
82 i->inc_ref();
83 return &*i;
84 }
85 }
86
87 FiberStack* stack = 0;
88
794333e Evan Phoenix Make stack pool and size configurable
evanphx authored
89 if(stacks_.size() < max_stacks_) {
90 stacks_.push_back(FiberStack(stack_size_));
e38045b Evan Phoenix Restructure how fiber stacks are allocated
evanphx authored
91 stack = &stacks_.back();
92
93 stack->allocate();
94 } else {
95 for(Stacks::iterator i = stacks_.begin();
96 i != stacks_.end();
97 ++i)
98 {
99 if(!stack || i->refs() < stack->refs()) {
100 stack = &*i;
101 }
102 }
103
104 assert(stack);
105 }
106
107 stack->inc_ref();
108
109 return stack;
110 }
75dce8b Evan Phoenix Work out the kinks in the fiber stack setup
evanphx authored
111
112 void* FiberStacks::trampoline() {
113 if(trampoline_ == 0) {
114 trampoline_ = malloc(cTrampolineSize);
115 }
116
117 return trampoline_;
118 }
e38045b Evan Phoenix Restructure how fiber stacks are allocated
evanphx authored
119 }
Something went wrong with that request. Please try again.