Skip to content

Commit

Permalink
monospace talk and code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sdether committed Jul 25, 2011
0 parents commit eafa246
Show file tree
Hide file tree
Showing 42 changed files with 19,622 additions and 0 deletions.
Binary file added Monospace-NoThreads.pdf
Binary file not shown.
19 changes: 19 additions & 0 deletions Monospace.NoThreads.6.0.ReSharper
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Configuration>
<Daemon.SolutionSettings>
<SkipFilesAndFolders>
<Item>70A4B77B-857D-4B2C-9F8C-E2BB97EDCE25/f:SharingWithAsync.cs</Item>
<Item>CF1FF2BD-BBAC-47A9-A03C-DC86226BA772/f:Consumer.cs</Item>
<Item>CF1FF2BD-BBAC-47A9-A03C-DC86226BA772/f:Exponentiator.cs</Item>
<Item>CF1FF2BD-BBAC-47A9-A03C-DC86226BA772/f:Exponentiator2.cs</Item>
<Item>CF1FF2BD-BBAC-47A9-A03C-DC86226BA772/f:Producer.cs</Item>
<Item>CF1FF2BD-BBAC-47A9-A03C-DC86226BA772/f:Producer2.cs</Item>
<Item>D330069B-4313-425F-A296-94181F47578B/d:AsyncAdapter/f:SharingWithAsync.cs</Item>
<Item>D330069B-4313-425F-A296-94181F47578B/d:AsyncProducerConsumer/f:Coroutines.cs</Item>
<Item>D330069B-4313-425F-A296-94181F47578B/d:CallingStyles/f:AsyncAwait.cs</Item>
<Item>D330069B-4313-425F-A296-94181F47578B/d:PlugAwait/f:AwaitablePlug.cs</Item>
<Item>D330069B-4313-425F-A296-94181F47578B/f:AsyncVisits.cs</Item>
<Item>FAA1E212-899C-4EF0-8C3B-809C281A7EA7/d:Async/f:AsyncDictionary.cs</Item>
<Item>FAA1E212-899C-4EF0-8C3B-809C281A7EA7/d:Async/f:AsyncVisitor.cs</Item>
</SkipFilesAndFolders>
</Daemon.SolutionSettings>
</Configuration>
40 changes: 40 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Consumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

namespace Monospace.NoThreads.AsyncCoroutines {
class Consumer {
public static async void Coroutine(int[,] destination, Coordinator<int[]> coordinator) {

Console.WriteLine("consumer started");
int i = 0, j = 0;
while(true) {

Console.WriteLine("awaiting producer");
await coordinator;
if(coordinator.State == null) {
continue;
}
if(coordinator.State.Length == 0) {

Console.WriteLine("consumer finished, end of input");
return;
}
foreach(var item in coordinator.State) {
destination[i, j] = item;

Console.WriteLine("wrote {0} to [{1},{2}]", item, i, j);
j++;
if(j != destination.GetLength(1)) {
continue;
}
j = 0;
i++;
if(i == destination.GetLength(0)) {

Console.WriteLine("consumer finished, output full");
return;
}
}
}
}
}
}
45 changes: 45 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Coordinator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;

namespace Monospace.NoThreads.AsyncCoroutines {

public class Coordinator<T> {
private readonly Queue<Action> _actions = new Queue<Action>();

public Coordinator(IEnumerable<Func<Coordinator<T>, Action>> coroutines) {

// prime continuation queue with each coroutine's entrypoint
foreach(var coroutine in coroutines) {
_actions.Enqueue(coroutine(this));
}
}

public void Execute() {

// iterate over continuations
while(_actions.Count > 0) {
_actions.Dequeue().Invoke();
}
}

// the shared state for all coroutines
public T State { get; set; }

// required to make Coordinator "await"able
public Coordinator<T> GetAwaiter() { return this; }

// has the awaitable finished?
public bool IsCompleted { get { return false; } }

// if it hasn't finished here's the continuation to pick up execution
public void OnCompleted(Action continuation) {

// put the continuation into the queue, so we can pick it back up when we've run the other coroutine continuations in line
_actions.Enqueue(continuation);
}

// the value of the awaitable (void for the coordinator)
public void GetResult() { }
}

}
23 changes: 23 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Exponentiator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace Monospace.NoThreads.AsyncCoroutines {
class Exponentiator {

public static async void Coroutine(Coordinator<int[]> coordinator) {
while(true) {
if(coordinator.State == null) {
continue;
}
if(coordinator.State.Length == 0) {
Console.WriteLine("consumer finished, end of input");
return;
}
for(int i = 0; i < coordinator.State.Length; i++) {
var item = coordinator.State[i];
coordinator.State[i] = item * item;
}
await coordinator;
}
}
}
}
9 changes: 9 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Exponentiator2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Monospace.NoThreads.AsyncCoroutines {
internal class Exponentiator2 {
public static async Task<int> AsyncMethod(int input) {
return input * input;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CF1FF2BD-BBAC-47A9-A03C-DC86226BA772}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Monospace.NoThreads.AsyncCoroutines</RootNamespace>
<AssemblyName>Monospace.NoThreads.AsyncCoroutines</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AsyncCtpLibrary">
<HintPath>..\..\..\Users\Administrator\Documents\Microsoft Visual Studio Async CTP\Samples\AsyncCtpLibrary.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\redist\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Consumer.cs" />
<Compile Include="Coordinator.cs" />
<Compile Include="Exponentiator.cs" />
<Compile Include="Exponentiator2.cs" />
<Compile Include="Producer.cs" />
<Compile Include="Producer2.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="README.rst" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
31 changes: 31 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Producer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Monospace.NoThreads.AsyncCoroutines {
class Producer {

public static async void Coroutine(int[,] source, Coordinator<int[]> coordinator) {

Console.WriteLine("producer started");
for(var i = 0; i < source.GetLength(0); i++) {

coordinator.State = new int[source.GetLength(1)];
for(var j = 0; j < source.GetLength(1); j++) {

var item = source[i, j];
coordinator.State[j] = item;

Console.WriteLine("read {0} from [{1},{2}]", item, i, j);
}

Console.WriteLine("awaiting consumer");
await coordinator;
}

coordinator.State = new int[0];
Console.WriteLine("producer finished");
}
}
}
32 changes: 32 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Producer2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace Monospace.NoThreads.AsyncCoroutines {
internal class Producer2 {

public static async void Coroutine(int[,] source, Coordinator<int[]> coordinator) {

Console.WriteLine("producer started");
for(var i = 0; i < source.GetLength(0); i++) {

coordinator.State = new int[source.GetLength(1)];
for(var j = 0; j < source.GetLength(1); j++) {

var item = source[i, j];

// awaiting a regular async method while in a coordinator controlled coroutine (not possible with Iterator based coroutines)
var exponentiated = await Exponentiator2.AsyncMethod(item);
coordinator.State[j] = exponentiated;

Console.WriteLine("read {0} from [{1},{2}] and processed to {3}", item, i, j, exponentiated);
}

Console.WriteLine("awaiting consumer");
await coordinator;
}

coordinator.State = new int[0];
Console.WriteLine("producer finished");
}

}
}
83 changes: 83 additions & 0 deletions Monospace.NoThreads.AsyncCoroutines/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Monospace.NoThreads.AsyncCoroutines {

[TestFixture]
public class Program {

static void Main(string[] args) {
var program = new Program();
Run("Transpose", program.Transpose);
Run("Transpose and Exponentiate - I", program.Transpose_and_exponentiate1);
Run("Transpose and Exponentiate - II", program.Transpose_and_exponentiate2);
}

[Test]
public void Transpose() {
var source = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
var destination = new int[6, 2];
var coordinator = new Coordinator<int[]>(
new Func<Coordinator<int[]>, Action>[] {
c => () => Consumer.Coroutine(destination, c),
c => () => Producer.Coroutine(source, c),
}
);
Print("input", source);
coordinator.Execute();
Print("output", destination);
}

[Test]
public void Transpose_and_exponentiate1() {
var source = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
var destination = new int[6, 2];
var coordinator = new Coordinator<int[]>(
new Func<Coordinator<int[]>, Action>[] {
c => () => Consumer.Coroutine(destination, c),
c => () => Producer.Coroutine(source, c),
c => () => Exponentiator.Coroutine(c)
}
);
Print("input", source);
coordinator.Execute();
Print("output", destination);
}

[Test]
public void Transpose_and_exponentiate2() {
var source = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
var destination = new int[6, 2];
var coordinator = new Coordinator<int[]>(
new Func<Coordinator<int[]>, Action>[] {
c => () => Consumer.Coroutine(destination, c),
c => () => Producer2.Coroutine(source, c),
}
);
Print("input", source);
coordinator.Execute();
Print("output", destination);
}

private static void Run(string title, Action action) {
Console.WriteLine("=== {0} ===", title);
action();
}

private void Print(string title, int[,] array) {
Console.WriteLine("{0}:", title);
Console.WriteLine("[");
for(var i = 0; i < array.GetLength(0); i++) {
Console.Write(" [ ");
for(var j = 0; j < array.GetLength(1); j++) {
Console.Write("{0},", array[i, j]);
}
Console.WriteLine(" ]");
}
Console.WriteLine("]");
}
}
}
Loading

0 comments on commit eafa246

Please sign in to comment.