Channels and GoRoutines in AjSharp (Part 1)

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: You can read about Go concurrency features (go routines, channels) at:

I also started to read about Axum Microsoft project:

With all this background, last saturday I added channel, goroutines to my AjSharp interpreter. You can download the current version from 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.value = value;
    public object Receive()
        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(); }
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: (current source code)

AjSharp posts

Angel “Java” Lopez

2 thoughts on “Channels and GoRoutines in AjSharp (Part 1)

  1. Pingback: Channels and GoRoutines in AjSharp (Part 2) « Angel “Java” Lopez on Blog

  2. Pingback: Memory transactions in AjSharp using References « Angel “Java” Lopez on Blog

Leave a Reply

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

You are commenting using your 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