Skip to content

Instantly share code, notes, and snippets.

@tarnacious
Created March 13, 2011 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarnacious/868056 to your computer and use it in GitHub Desktop.
Save tarnacious/868056 to your computer and use it in GitHub Desktop.
Basic message based co-operative scheduling with trampolining.
using System;
using System.Collections.Generic;
public class Scheduler
{
public delegate Process Process(string message);
public static Dictionary<Guid, Process> Processes = new Dictionary<Guid,Process>();
public static Guid Spawn(Process process)
{
Guid key = Guid.NewGuid();
Processes.Add(key, process);
Console.WriteLine("Spawned: {0}", key);
return key;
}
public static void Message(Guid address, string message)
{
if (Processes.ContainsKey(address)) {
Process process = Processes[address](message);
Processes[address] = process;
}
}
}
public class SchedulerApp
{
public static Scheduler.Process BasicProcess (string message) {
Console.WriteLine("Received: {0}", message);
return (m) => Increment(m);
}
public static void Main()
{
var Pid = Scheduler.Spawn(BasicProcess);
Scheduler.Message(Pid, "Hello Process");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment