Friday, September 03, 2010

C# Go'ing Channelled


Go is one of the new languages I came across and found interesting from a few of different angles. First of all it is compiled and statically typed somewhat comparable to Java and C#. Second interesting aspect is it's syntax; Although it is basically a C-like syntax there is one thing would catch your eyes first is the variable declarations - they are wrong way round! Well, "that's weird" was my first reaction but when you kind of get used to it, variable name first, type last notation starts to makes (arguable) more sense. After all variable name is more important than it's type when it comes to thinking about your programs logic. Last but not least, the approach to concurrency is very sensible; it has beautiful concurrent constructs like channels and Go Routines. This concurrency aspect interests me most and decided to do a little experiment in C#, implementing a very simple 'Channel'. (Hence this post and the cheese title)

As far as I understand, the basic idea is quite simple; while achieving concurrency instead of sharing state, pass the state through multiple queues avoiding hard-to-get-right synchronisation mechanisms. Possibly, like what you might have guessed (if you are coming from a computer science background, or read a few articles about how increasingly parallel we need to start making our programs, because the Moore's Law is not going to last forever and so on) the idea is not new at all. Actually in computer technology terms it's ancient history. I have come across a Communications of the ACM article which had a section about Determinate Computation explaining the idea of using blocking tasks communicating through FIFO queues rather than shared memory.

I have been working with C# quite some time now and since I have used blocking queues (which I find very similar to Go Channels) for similar concurrency purposes before. It was a natural flow of thinking that I needed to implement some generic functionality and make it available to my applications in .Net. Although I must stress that; this is just an experiment and it is far from being a complete implementation. There are other crucial features missing to make it really useful and it is not test much and it is not used in production at all.

The interface is very simple; there are only to methods, Put(item) and Get() : item. And there are two very simple behaviours; 'put' blocks when the capacity is full, 'get' blocks when there is nothing in the channel. For example in client code you can create your threads and share IChannel instances where you can use a set of threads as producers and consumers or you can use them for simple notifications.

var c = new Channel<int>(5);

var t1 = new Thread(() =>
{
for (int i = 0; i < 10; i++)
c.Put(i);
});

var t2 = new Thread(() =>
{
for (int i = 0; i < 10; i++)
Console.WriteLine(c.Get());
});

t1.Start(); t2.Start();
t1.Join(); t2.Join();

In the above simple example you would always have sequential numbers printed in ascending order e.g. 0, 1, 2, 3, ... This might not sound very interesting but notice I did't have to use any synchronisation mechanisms to achieve a deterministic output. Same example code can be used in several threads with many channels working together.

Having a quick look at the implementation actually reveals all the tricky synchronisation is handled so I don't have to in my client code. I am basically using a Queue to store items and two Semaphores to block Gets and Puts as the queue runs out and capacity is reached respectively. Here is the constructor:

private readonly Queue<T> _q = new Queue<T>();
private readonly Semaphore _sPut;
private readonly Semaphore _sGet;

public Channel(int capacity)
{
_sPut = new Semaphore(capacity, capacity);
_sGet = new Semaphore(0, capacity);
}

There is a bit more to it in the code repository (to handle clean exits and so on) but above snippet shows the guts of it. Similarly Put and Get methods are fairly simple without the details:

public void Put(T item)
{

lock (_guard)
{
_sGet.Release();
_q.Enqueue(item);
}
}

public T Get()
{
lock (_guard)
{
_sPut.Release();
return _q.Dequeue();
}
}

As you can see this isn't much different from a standard blocking queue or producer/consumer implementation (only difference is I am blocking on Puts as well -when the queue is full). The real challenge with this implementation would be implementing the Channel class (or Get and Put return types) in such a way so it behaves like a WaitHandle and can be used with multiple channels blocking on Gets or Puts to support more useful scenarios. I suspect this would require some low level unmanaged code. In the example implementation you can see I took the initial steps towards this direction by returning a result object from Get and Put methods.


Sunday, February 07, 2010

Introduction to Android Development

Here are my notes about developing for Android:

Before anything I must say most of the meterial is very good quality and available at http://developer.android.com/

Installation

First and foremost make sure you have the latest Java SDK installed.

You need the SDK first. This is actually almost a download manager for your various versions of Android framework. And it includes an emulator too (a nice QEMU application). Download 1.5 - thats what I will be using.

Grab the latest Eclipse 'Classic' and install. Then install the Android Development plug-in from the update site.

The last thing you need to do is creating a 'device' for yourself. Just use the default settings with no other options.

Developing Hello World

Let's have a quick go at the standard Hello World.


A Little Theory

Activities

Services

Broadcasters

Intents

UI

Views and ViewGroups, Layouts

Animation

Service RPC

Threads

Event Driven programming

Tips & tricks

adb logcat

adb shell

Create packages using Eclipse Export

Eclipse shortcut-keys

Ctrl-1 Quick Fix

Ctrl-SpaceAuto-complete

Crtl-Shift-LList of shortcuts

Sunday, July 12, 2009

Ruby Notes

Here are my quick notes about some Ruby tips for beginners.

Installation and Gems



Gems is Ruby's CPAN ;) If you are installing on Windows make sure to include gems in the installation. By default its turned off for 'Ruby 1.8.6 One-Click Installer'. You can get it from http://www.ruby-lang.org/en/downloads/. You can use the 'irb' tool to use Ruby on the fly (aka REPL)



Rspec


Rspec is Ruby's testing framework. You can install it using 'gem install rspec' on the command line. http://rspec.info/ is the main documentation site for it. Just type 'spec -h' on the command line for more information about how to run it. Cucumber is another functional testing framework which is becoming popular - apparently, I don't even know the official URL to it, so just search for it!



Some Language Syntax



Variable naming conventions:

local @instance @@class $global CONSTANT Constant



Class and method declarations using optionally accessors:


class MyClass

attr_accessor :name
attr_reader :someother1
attr_writer :someother2

def name
return @name
end

def name=(name)
@name = name
end

end

Arrays and hashes:

{:a => 1}.each { |k, v|
puts k + ": " +v
}

[1,2,3].each do |i|
puts i
end

The colon notation creates unique identifiers eg.
:name



Mix-ins are another way of extending your classes. They are defined and used as


Module A
end

class X
include A
end



Yield and Blocks



Block are anonymous functions
You can use yield keyword to execute the block which is passed to a function.


def some_func
yield
end

some_func do
puts "yield is running me!"
end

You can use 'block_given?' special variable to check whether a block passed to a function or not.



Useful Ruby for Ruby



You can use the ruby2ruby module to interpret ruby code within a Ruby script.

gem install ruby2ruby
Another useful gem is ruby_degug (
gem install ruby_debug
)