/
fiber.cr
232 lines (196 loc) · 5.56 KB
/
fiber.cr
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require "c/sys/mman"
# :nodoc:
@[NoInline]
fun _fiber_get_stack_top : Void*
dummy = uninitialized Int32
pointerof(dummy).as(Void*)
end
class Fiber
STACK_SIZE = 8 * 1024 * 1024
@@first_fiber : Fiber? = nil
@@last_fiber : Fiber? = nil
@@stack_pool = [] of Void*
@stack : Void*
@resume_event : Event::Event?
protected property stack_top : Void*
protected property stack_bottom : Void*
protected property next_fiber : Fiber?
protected property prev_fiber : Fiber?
property name : String?
def initialize(@name : String? = nil, &@proc : ->)
@stack = Fiber.allocate_stack
@stack_bottom = @stack + STACK_SIZE
fiber_main = ->(f : Fiber) { f.run }
stack_ptr = @stack + STACK_SIZE - sizeof(Void*)
# Align the stack pointer to 16 bytes
stack_ptr = Pointer(Void*).new(stack_ptr.address & ~0x0f_u64)
# @stack_top will be the stack pointer on the initial call to `resume`
ifdef x86_64
# In x86-64, the context switch push/pop 7 registers
@stack_top = (stack_ptr - 7).as(Void*)
stack_ptr[0] = fiber_main.pointer # Initial `resume` will `ret` to this address
stack_ptr[-1] = self.as(Void*) # This will be `pop` into %rdi (first argument)
elsif i686
# In IA32, the context switch push/pops 4 registers.
# Add two more to store the argument of `fiber_main`
@stack_top = (stack_ptr - 6).as(Void*)
stack_ptr[0] = self.as(Void*) # First argument passed on the stack
stack_ptr[-1] = Pointer(Void).null # Empty space to keep the stack alignment (16 bytes)
stack_ptr[-2] = fiber_main.pointer # Initial `resume` will `ret` to this address
else
{{ raise "Unsupported platform, only x86_64 and i686 are supported." }}
end
@prev_fiber = nil
if last_fiber = @@last_fiber
@prev_fiber = last_fiber
last_fiber.next_fiber = @@last_fiber = self
else
@@first_fiber = @@last_fiber = self
end
end
def initialize
@proc = Proc(Void).new { }
@stack = Pointer(Void).null
@stack_top = _fiber_get_stack_top
@stack_bottom = LibGC.stackbottom
@name = "main"
@@first_fiber = @@last_fiber = self
end
protected def self.allocate_stack
@@stack_pool.pop? || LibC.mmap(nil, Fiber::STACK_SIZE,
LibC::PROT_READ | LibC::PROT_WRITE,
LibC::MAP_PRIVATE | LibC::MAP_ANON,
-1, 0).tap do |pointer|
raise Errno.new("Cannot allocate new fiber stack") if pointer == LibC::MAP_FAILED
ifdef linux
LibC.madvise(pointer, Fiber::STACK_SIZE, LibC::MADV_NOHUGEPAGE)
end
LibC.mprotect(pointer, 4096, LibC::PROT_NONE)
end
end
def self.stack_pool_collect
return if @@stack_pool.size == 0
free_count = @@stack_pool.size > 1 ? @@stack_pool.size / 2 : 1
free_count.times do
stack = @@stack_pool.pop
LibC.munmap(stack, Fiber::STACK_SIZE)
end
end
def run
@proc.call
rescue ex
# Don't use STDERR here because we are at a lower level than that
msg = String.build do |io|
io.puts "Unhandled exception:"
ex.inspect_with_backtrace io
end
LibC.write(2, msg, msg.bytesize)
ensure
@@stack_pool << @stack
# Remove the current fiber from the linked list
if prev_fiber = @prev_fiber
prev_fiber.next_fiber = @next_fiber
else
@@first_fiber = @next_fiber
end
if next_fiber = @next_fiber
next_fiber.prev_fiber = @prev_fiber
else
@@last_fiber = @prev_fiber
end
# Delete the resume event if it was used by `yield` or `sleep`
@resume_event.try &.free
Scheduler.reschedule
end
@[NoInline]
@[Naked]
protected def self.switch_stacks(current, to)
ifdef x86_64
asm(%(
pushq %rdi
pushq %rbx
pushq %rbp
pushq %r12
pushq %r13
pushq %r14
pushq %r15
movq %rsp, ($0)
movq ($1), %rsp
popq %r15
popq %r14
popq %r13
popq %r12
popq %rbp
popq %rbx
popq %rdi)
:: "r"(current), "r"(to))
elsif i686
asm(%(
pushl %edi
pushl %ebx
pushl %ebp
pushl %esi
movl %esp, ($0)
movl ($1), %esp
popl %esi
popl %ebp
popl %ebx
popl %edi)
:: "r"(current), "r"(to))
end
end
def resume
current, @@current = @@current, self
LibGC.stackbottom = @stack_bottom
Fiber.switch_stacks(pointerof(current.@stack_top), pointerof(@stack_top))
end
def sleep(time)
event = @resume_event ||= Scheduler.create_resume_event(self)
event.add(time)
Scheduler.reschedule
end
def yield
sleep(0)
end
def self.sleep(time)
Fiber.current.sleep(time)
end
def self.yield
Fiber.current.yield
end
def to_s(io)
io << "#<" << self.class.name << ":0x"
object_id.to_s(16, io)
if name = @name
io << ": " << name
end
io << ">"
end
def inspect(io)
to_s(io)
end
protected def push_gc_roots
# Push the used section of the stack
LibGC.push_all_eager @stack_top, @stack_bottom
end
@@root = new
def self.root : self
@@root
end
# TODO: Boehm GC doesn't scan thread local vars, so we can't use it yet
# @[ThreadLocal]
@@current : Fiber = root
def self.current : self
@@current
end
@@prev_push_other_roots = LibGC.get_push_other_roots
# This will push all fibers stacks whenever the GC wants to collect some memory
LibGC.set_push_other_roots ->do
@@prev_push_other_roots.call
fiber = @@first_fiber
while fiber
fiber.push_gc_roots unless fiber == @@current
fiber = fiber.next_fiber
end
end
end