Project

cfiber

0.0
No commit activity in last 3 years
No release in over 3 years
Continuations used to implement Fibers as provided by Ruby 1.9. Works in 1.8 as well.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

CFiber is Fibers implemented using continuations, using the native Ruby Continuation class.

Experimental! It is based on tmm1's work.

Synopsis

Continuations in Ruby are like time-machine engineers. They let you take snapshots of your runtime environment, and jump (back) to a particular "save point" at any time. Continuation objects, which you may store, are like an object representing a frozen point in time where one may jump back and start a new life. Fibers are a way to pause and resume an execution frame by hand, scheduling its execution within the wrapping thread (which may be another Fiber!).

All in all, it is all about jumping back and forth between several states, so with some smart design, continuations should let us achieve the Fiber pattern! This project is an attempt at implementing the Ruby 1.9 Fiber API using only continuations. The Continuation API is made of only two methods:

  • #callcc, which stands for Call With Current Continuation; it allows for capturing the runtime state and associate a pending block to it. The "current continuation" is a way to talk about the "remaining/pending code left to run" at the time the continuation is captured. In a way, capturing a new continuation sets a checkpoint which allows you to jump back to a particular state, so you effectively gain access to "the remaining code at this time" for you now have a way to jump back in the past to reach what was the current state (that's for the "past/current" confusion);
  • #call, which have us jump back in time and trigger the block in the context of the current runtime, where "current" really means "the captured state" (again).

Those are the basis for CFiber. See the Interesting readings section for more information.

Installation

Not available as a gem yet. You should clone this repository.

You may build and install a local gem running gem build cfiber.gemspec && gem install cfiber*.gem though.

Usage

Run ruby spec/cfiber_spec.rb. Run and read ruby examples/use_cases.rb. Then play with Fiber.

In a Ruby console, require ./lib/cfiber.rb (when inside cfiber's root directory) or require 'cfiber' (if you built and installed a local gem).

By installing the Logg gem, you'll gain debugging output. Pass the -d flag: ruby examples/use_cases.rb.

TODO

  • coroutines using #transfer do not work atm.
  • #alive? implementation is challenging

Interesting readings

Doc

Continuations

Fibers