wabbajack/Wabbajack.Common.CSP
2019-11-21 15:11:08 +01:00
..
Properties Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
AChannel.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
AsyncResult.cs Break the Channel interface into Read/Write/Close ports (#169) 2019-11-11 22:14:04 -07:00
Channel.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
EnumeratorBuffer.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
Extensions.cs Break the Channel interface into Read/Write/Close ports (#169) 2019-11-11 22:14:04 -07:00
FixedSizeBuffer.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
Handler.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
IBuffer.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
IChannel.cs Break the Channel interface into Read/Write/Close ports (#169) 2019-11-11 22:14:04 -07:00
ICloseable.cs Break the Channel interface into Read/Write/Close ports (#169) 2019-11-11 22:14:04 -07:00
IReadPort.cs Break the Channel interface into Read/Write/Close ports (#169) 2019-11-11 22:14:04 -07:00
IWritePort.cs Break the Channel interface into Read/Write/Close ports (#169) 2019-11-11 22:14:04 -07:00
ManyToManyChannel.cs Async VFS Implementation (#171) 2019-11-14 15:22:53 -07:00
packages.config Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
PIpelines.cs Async VFS Implementation (#171) 2019-11-14 15:22:53 -07:00
PutTaskHandler.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
Readme.md Created READMEs for most projects 2019-11-21 15:11:08 +01:00
RingBuffer.cs Async VFS Implementation (#171) 2019-11-14 15:22:53 -07:00
RxBuffer.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
TakeTaskHandler.cs Make BSA Routines async (#168) 2019-11-11 21:35:07 -07:00
Wabbajack.Common.CSP.csproj Make File Extractor async 2019-11-11 22:33:32 -07:00

Wabbajack.Common.CSP

Overview of CSP (Communicating Sequential Processes) for the C# programmer

What is CSP?

Communicating Sequential processes is a programming model invented in 1978 by Tony Hoare, who described a process of computation where hundreds or thousands of small processes communicate via channels. Think of this process like a assembly line. Each worker in the factory is a process, and the conveyor belts are the channels. The workers don't need to know where a part came from, or where it's going, they simply take one item off the belt, perform an operation and pass the item on to another belt. This analogy works quite well, and the following observations about a factory also apply to CSP:

  • Multiple workers can pull from the same belt (channel/queue)
  • Multiple workers can put work onto the belt
  • Belts can buffer items, for slow consumers, but at some point they backup and block the writer
  • A worker can pull/push to multiple belts.

What does this look like in C#?

The basic unit of CSP in this library is the channel:

var chan = Channel.Create()

Without any other parameters this creates a channel with a size of 0, so every pending put must be matched 1:1 with a take. This creates a syncronization point. Channels are fully async and thread-safe:

public async Task TestTakePutBlocking()
{
    var channel = Channel.Create<int>();
    // Channel size is 0, so we can't await, because we'd never complete
    var ptask = channel.Put(1);

    // Now the put is dispatched to the scheduler because we've taken the value
    var (open, val) = await channel.Take();

    Assert.AreEqual(1, val);
    Assert.IsTrue(open);
    Assert.IsTrue(await ptask);
}