evan.musing << current

life and tech stuff by Evan Phoenix

Task class in SVN

with 3 comments

Last night I finished up support for the Task class, which will form a core part of the system.

The Task class provides essentially the same functionality as getcontext/setcontext and setjmp/longjmp in C, with a little more icing to make them more useful. The main functional difference is that when a new Task is created, it gets a new stack to operate on. This allows Tasks to be switched in and out with any other Task at any time.

The other main feature is the ability to associate a Task with a block. When the block is activated, it will begin executing the block.

You can probably see where this is going… thats right, greenthreads. Tasks are going to form the basis for green threading. The main thing that Threads add on top of Tasks is a scheduler, ie, which Task should run when.

Now, A small example:


ts = Task.current

t2 = Task.new do
  puts "2: wee! new task! switching back.."
  t2.swap(ts)
  puts "2: you're returned!"
end

puts "1: activating new task.."
ts.swap(t2)
puts "1: back to the future!"
ts.swap(t2)
puts "1: and we're done."

Who’s output is:


1: activating new task..
2: wee! new task! switching back..
1: back to the future!
2: you're returned!
1: and we're done.

This is a trivial example, where Task#new creates a new Task and associates the given block with this new task. But you can see how as Tasks are swapped back and forth, execution of the task continues exactly where left off.

I’m going to begin work on the Thread scheduler shortly, and I’m expecting to be able to implement it and the Thread class itself directly in Ruby (ie, no C).

The biggest thing missing is synchronization mechanisms. My plan is to add a Channel class which will allow for objects to be passed between Tasks, with some synchronization built-in.

About these ads

Written by evanphx

April 5, 2007 at 6:28 pm

Posted in Uncategorized

3 Responses

Subscribe to comments with RSS.

  1. Looks neat, just hope there isn’t any confusion with Rake. :)

    Daniel Berger

    April 11, 2007 at 9:15 pm

  2. Why not use continuations to do this? They’re already available within Ruby itself, although I have no idea if they’re used for thread scheduling in the standard Ruby implementation.

    Dido Sevilla

    July 30, 2007 at 9:49 pm

  3. Dido: rubinius is a completely new codebase, we’re not building on top of existing ruby. That being said, the continuations in rubinius are going to be built on top of Tasks.

    Dan: Yeah, good point. Well if people are confused, I’ll move the class or change the name.

    evan

    August 13, 2007 at 11:22 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: