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: 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


  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: 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

The Shocking Blue Green Theme. Blog at


Get every new post delivered to your Inbox.

Join 70 other followers

%d bloggers like this: