Ruby 1.9 - equivalent of Fiber.current ?

S

Suraj Kurapati

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:
=> [: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
 
S

Suraj Kurapati

Suraj said:
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:
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 Kurapati

ts said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top