Skip to content

Commit 45f125a

Browse files
committed
Initialize Fiber with an explicit stack
Doesn't change the public API (the stack is still taken from the current scheduler's stack pool), but introduces an undocumented initializer that takes the stack and stack_bottom pointers. This will allow a few scenarios, mostly for RFC 2: - start fibers with a fake stack when we don't need to run the fibers, for example during specs that only need fiber objects (and would leak memory since we only release stacks after the fiber has run); - during a cross execution context spawn, we can pick a stack from the destination context instead of the current context (so we can recycle stacks from fibers that terminated in the desination context).
1 parent 204b6a9 commit 45f125a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/fiber.cr

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,19 @@ class Fiber
9797
# When the fiber is executed, it runs *proc* in its context.
9898
#
9999
# *name* is an optional and used only as an internal reference.
100-
def initialize(@name : String? = nil, &@proc : ->)
101-
@context = Context.new
102-
@stack, @stack_bottom =
100+
def self.new(name : String? = nil, &proc : ->)
101+
stack, stack_bottom =
103102
{% if flag?(:interpreted) %}
104103
{Pointer(Void).null, Pointer(Void).null}
105104
{% else %}
106105
Crystal::Scheduler.stack_pool.checkout
107106
{% end %}
107+
new(name, stack, stack_bottom, &proc)
108+
end
109+
110+
# :nodoc:
111+
def initialize(@name : String?, @stack : Void*, @stack_bottom : Void*, &@proc : ->)
112+
@context = Context.new
108113

109114
fiber_main = ->(f : Fiber) { f.run }
110115

0 commit comments

Comments
 (0)