Skip to content

Instantly share code, notes, and snippets.

@halorgium
Last active December 16, 2015 10:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halorgium/5992cec8eba0b4c53492 to your computer and use it in GitHub Desktop.
Save halorgium/5992cec8eba0b4c53492 to your computer and use it in GitHub Desktop.
Common Ruby additions to benefit Fibers

Currently the Thread APIs behave in a way which obscures the root Fiber from access. There have been several new APIs added to support accessing the root Thread local data.

If you consider that each Thread has a root Fiber, then accessing locals and the backtrace on that Fiber object could return locals and the backtrace for the Thread.

I have a WIP branch on top of Rubinius.

Thread#root_fiber or Fiber.rootfiber

This returns a Fiber wrapping the structures for the original Thread. Instead of using the new Thread APIs, it would be possible to simply call methods on this object.

Thread#fibersarray

This returns an Array of Fiber objects currently owned by the Thread. If a Fiber is moved to another Thread, the Fiber would no longer be returned in the previous Thread#fibers call.

Fiber.listarray

This returns an Array of all executing or suspended Fibers.

Fiber#[key]obj or nil

This returns the value associated with this key on the Fiber independent of which Fiber is executing on the Thread currently.

thread.thread_variable_get(key) should be equivalent to thread.root_fiber[key].

Fiber#[key] = objobj

This sets the value associated with this key on the Fiber independent of which Fiber is executing on the Thread currently.

thread.thread_variable_set(key, value) should be equivalent to thread.root_fiber[key] = value.

Fiber#keyskeys

This returns the keys associated with this Fiber independent of which Fiber is executing on the Thread currently.

thread.thread_variables should be equivalent to thread.root_fiber.keys.

Fiber#backtracearray

This returns the backtrace of this Fiber independent of which Fiber is executing on the Thread currently.

Fiber#backtrace_locations(*args)array or nil

This returns the execution stack of this Fiber independent of which Fiber is executing on the Thread currently. See Thread::Backtrace::Location for more information.

Fiber#alive?bool

This returns whether the Fiber is still alive.

Fiber#statusstring

This returns the status of the Fiber, whether it is executing or suspended.

Fiber#raise(exception)

This raises an exception inside the Fiber when the Fiber is next resumed.

Filed feature requests

@halorgium
Copy link
Author

@brixen
Copy link

brixen commented Jun 20, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment