Angel \”Java\” Lopez on Blog

December 28, 2009

Channels and GoRoutines in AjSharp (Part 1)

Filed under: .NET, AjSharp, Programming Languages — ajlopez @ 9:38 am

Two years ago, I played with Microsoft Robotics, and its Concurrency and Coordination library. It has the implementation of a port: you can send an object to it, and it could received by other piece of code. And using other features, you can run both part in different machines: a way to connect distributed applications. I wrote:

Distributed Agents using DSS/VPL
Genetic Algorithms with AjAgents and Concurrency and Coordination Runtime (CCR)
CCR posts

Then, I met the concept of channel: Channel programming (Wikipedia) and this year, I read about the new Google language, the Go language: http://golang.org. You can read about Go concurrency features (go routines, channels) at: http://golang.org/doc/go_tutorial.html#tmp_346

I also started to read about Axum Microsoft project: http://en.wikipedia.org/wiki/Axum_%28programming_language%29

With all this background, last saturday I added channel, goroutines to my AjSharp interpreter. You can download the current version from http://code.google.com/p/ajcodekatas at trunk/AjLanguage

First, I wrote a new native type, Channel (to improve: add support to Channel to send (and receive) from more than one thread):

public class Channel
{
    private AutoResetEvent sethandle = new AutoResetEvent(false);
    private AutoResetEvent gethandle = new AutoResetEvent(false);
    private object value;
    public void Send(object value)
    {
        this.gethandle.WaitOne();
        this.value = value;
        this.sethandle.Set();
    }
    public object Receive()
    {
        this.gethandle.Set();
        this.sethandle.WaitOne();
        object result = this.value;
        return result;
    }
}

When you send an object to a channel, the sending thread is blocked until another thread reads the channel (I plan to add a QueueChannel, so the send and receive objects use an intermediate queue, possibly fixed size one).

Following the idea of goroutines in Go language, I added a new command to AjSharp language: go <command> (ok, no creativity here… ;-), that launch the command in a separated thread, as in this example (AjSharp has access to .NET native object and types):

handle = new System.Threading.AutoResetEvent(false);
one = 0;
go { one = 1; handle.Set(); }
handle.WaitOne();
result = one;

All this is added to AjSharp machine, you can use it:

channel = new Channel();
go channel.Send(10);
result = channel.Receive();

After having these features running, it was easy to add syntax sugar extension to AjSharp parser. <- channel is equivalent to channel.Receive(), and channel <- value is equivalent to channel.Send(value). So, the above example can be written as:

channel = new Channel();
go channel <- 10;
result = <- channel;

In another post, I will describe some uses of these features, and implementation details. See:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage (current source code)

AjSharp posts

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] (Part 2) Filed under: .NET, AjSharp, Programming Languages — ajlopez @ 9:33 am In my previous post I described the implementation of channels and goroutines-alike in AjSharp, my scripting language [...]

    Pingback by Channels and GoRoutines in AjSharp (Part 2) « Angel “Java” Lopez on Blog — December 30, 2009 @ 9:33 am

  2. [...] Channels and Go Routines in AjSharp (Part 1) [...]

    Pingback by Memory transactions in AjSharp using References « Angel “Java” Lopez on Blog — June 7, 2010 @ 9:08 am


RSS feed for comments on this post. TrackBack URI

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 )

Google+ photo

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

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: