Skip to content

Commit aae3498

Browse files
committed
Keep StackPool as main interface (now storing Fiber::Stack)
1 parent 9d9d079 commit aae3498

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

src/compiler/crystal/interpreter/context.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ class Crystal::Repl::Context
106106
# Once the block returns, the stack is returned to the pool.
107107
# The stack is not cleared after or before it's used.
108108
def checkout_stack(& : UInt8* -> _)
109-
stack, _ = @stack_pool.checkout
109+
stack = @stack_pool.checkout
110110

111111
begin
112-
yield stack.as(UInt8*)
112+
yield stack.pointer.as(UInt8*)
113113
ensure
114114
@stack_pool.release(stack)
115115
end

src/fiber.cr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ class Fiber
9898
#
9999
# *name* is an optional and used only as an internal reference.
100100
def self.new(name : String? = nil, &proc : ->)
101-
new(name, Stack.new, &proc)
101+
stack =
102+
{% if flag?(:interpreted) %}
103+
# the interpreter is managing the stacks
104+
Stack.new(Pointer(Void).null, 0)
105+
{% else %}
106+
Crystal::Scheduler.stack_pool.checkout
107+
{% end %}
108+
new(name, stack, &proc)
102109
end
103110

104111
# :nodoc:
@@ -159,7 +166,7 @@ class Fiber
159166

160167
@alive = false
161168
{% unless flag?(:interpreted) %}
162-
@stack.release
169+
Crystal::Scheduler.stack_pool.release(@stack)
163170
{% end %}
164171
Fiber.suspend
165172
end

src/fiber/stack.cr

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@ class Fiber
22
# :nodoc:
33
struct Stack
44
getter pointer : Void*
5-
getter bottom : Void*
5+
getter bytesize : Int32
66
getter? reusable : Bool
77

8-
def self.new : self
9-
{% if flag?(:interpreted) %}
10-
new Pointer(Void).null, Pointer(Void).null
11-
{% else %}
12-
stack, stack_bottom = Crystal::Scheduler.stack_pool.checkout
13-
new(stack, stack_bottom, reusable: true)
14-
{% end %}
8+
def initialize(@pointer, bottom : Void*, *, @reusable = false)
9+
@bytesize = (bottom - @pointer).to_i32
1510
end
1611

17-
def initialize(@pointer, @bottom, *, @reusable = false)
12+
def initialize(@pointer, @bytesize, *, @reusable = false)
13+
end
14+
15+
def bottom : Void*
16+
@pointer + @bytesize
1817
end
1918

2019
def first_addressable_pointer : Void**
21-
ptr = @bottom # stacks grow down
20+
ptr = bottom # stacks grow down
2221
ptr -= sizeof(Void*) # point to first addressable pointer
2322
Pointer(Void*).new(ptr.address & ~15_u64) # align to 16 bytes
2423
end
25-
26-
def release : Nil
27-
Crystal::Scheduler.stack_pool.release(@pointer) if @reusable
28-
end
2924
end
3025
end

src/fiber/stack_pool.cr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class Fiber
1212
# Interpreter stacks grow upwards (pushing values increases the stack
1313
# pointer value) rather than downwards, so *protect* must be false.
1414
def initialize(@protect : Bool = true)
15-
@deque = Deque(Void*).new
15+
@deque = Deque(Stack).new
1616
end
1717

1818
def finalize
1919
@deque.each do |stack|
20-
Crystal::System::Fiber.free_stack(stack, STACK_SIZE)
20+
Crystal::System::Fiber.free_stack(stack.pointer, stack.bytesize)
2121
end
2222
end
2323

@@ -26,7 +26,7 @@ class Fiber
2626
def collect(count = lazy_size // 2) : Nil
2727
count.times do
2828
if stack = @deque.shift?
29-
Crystal::System::Fiber.free_stack(stack, STACK_SIZE)
29+
Crystal::System::Fiber.free_stack(stack.pointer, stack.bytesize)
3030
else
3131
return
3232
end
@@ -41,18 +41,19 @@ class Fiber
4141
end
4242

4343
# Removes a stack from the bottom of the pool, or allocates a new one.
44-
def checkout : {Void*, Void*}
44+
def checkout : Stack
4545
if stack = @deque.pop?
46-
Crystal::System::Fiber.reset_stack(stack, STACK_SIZE, @protect)
46+
Crystal::System::Fiber.reset_stack(stack.pointer, stack.bytesize, @protect)
47+
stack
4748
else
48-
stack = Crystal::System::Fiber.allocate_stack(STACK_SIZE, @protect)
49+
pointer = Crystal::System::Fiber.allocate_stack(STACK_SIZE, @protect)
50+
Stack.new(pointer, STACK_SIZE)
4951
end
50-
{stack, stack + STACK_SIZE}
5152
end
5253

5354
# Appends a stack to the bottom of the pool.
54-
def release(stack) : Nil
55-
@deque.push(stack)
55+
def release(stack : Stack) : Nil
56+
@deque.push(stack) if stack.reusable?
5657
end
5758

5859
# Returns the approximated size of the pool. It may be equal or slightly

0 commit comments

Comments
 (0)