Created
March 13, 2011 12:37
-
-
Save tarnacious/868056 to your computer and use it in GitHub Desktop.
Basic message based co-operative scheduling with trampolining.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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