Ruby 1.9 - equivalent of Fiber.current?

Hello,

Is it possible to detect whether a method is being called by a Fiber in
Ruby 1.9?

I hoped that there would be a Fiber.current method (just like
Thread.current), but that does not exist:

Fiber.methods(false).sort
=> [:new, :yield]

I need to know this information for a method that’s part of an
application-level thread/fiber/task scheduler for my Ruby-VPI
project[1]. This method must either (1) run Fiber.yield (if called by a
Fiber) or (2) relay control from the Ruby interpreter to a C extension.

Thanks for your consideration.

[1] http://ruby-vpi.rubyforge.org

Suraj K. wrote:

Is it possible to detect whether a method
is being called by a Fiber in Ruby 1.9?

Heh, I should have just tried it:

Fiber.yield
FiberError: can’t yield from root fiber

So the solution is:

begin

inside a fiber

Fiber.yield
rescue FiberError

not inside a fiber…

end

But I suppose Fiber.yield could raise FiberError for other reasons…
is there a better way to do this detection?

“S” == Suraj K. [email protected] writes:

Fiber.methods(false).sort

vgs% ruby -rfiber -e ‘p Fiber.methods(false)’
[:new, :yield, :current]
vgs%

Guy Decoux

ts wrote:

vgs% ruby -rfiber -e ‘p Fiber.methods(false)’
[:new, :yield, :current]
vgs%

Aha! We need to require ‘fiber’ in order
to get the Fiber.current method. Thanks!