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)
Angel “Java” Lopez
http://www.ajlopez.com
[...] (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
[...] 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