Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building CSP-Style Concurrent Systems in Go

jnwhiteh
March 26, 2012

Building CSP-Style Concurrent Systems in Go

A talk briefly introducing the relation between Communicating Sequential Processes (CSP) and the Go programming language, and introducing some work examining the patterns that emerge from the combination of the two.

jnwhiteh

March 26, 2012
Tweet

Other Decks in Technology

Transcript

  1. Building CSP-style Concurrent Systems in Go James Whitehead II University

    of Oxford [email protected] March 20, 2012 Copyright 2012, James Whitehead II, All Rights Reserved. 1 / 17
  2. About me • Doctoral student • World of Warcraft addon

    developer • Hacking World of Warcraft • World of Warcraft Programming: a Guide and Reference for Creating WoW Addons • Open source contributor • Go (golang, chunkymonkey) • Lua (sputnik, WoW addons) Copyright 2012, James Whitehead II, All Rights Reserved. 2 / 17
  3. Introduction to Go • Compiled (quickly) • Statically typed •

    Garbage collected • Interface types • Object-oriented, with no classes or hierarchy • Concurrency primitives Copyright 2012, James Whitehead II, All Rights Reserved. 3 / 17
  4. Methods on types package main import "fmt" type IntMonoOp func(int)

    int type IntBinOp func(int, int) int func (fn IntBinOp) Curry(x int) IntMonoOp { return func(y int) int { return fn(x, y) } } var Add IntBinOp = func(x, y int) int { return x+y } func main() { addtwo := Add.Curry(2) fmt.Printf( "%d, %d, %d\n" , addtwo(1), addtwo(2), addtwo(3)) } Copyright 2012, James Whitehead II, All Rights Reserved. 4 / 17
  5. Communicating Sequential Processes A way to reason about and prove

    properties of concurrent systems. • Construct simple sequential programs • Run them in parallel • Allow them to communicate • explicit channels • Prove properties about the result • Specification and implementation Copyright 2012, James Whitehead II, All Rights Reserved. 5 / 17
  6. Concurrency family.. network? occam 1 occam 2 occam 3 occam

    2.1 Handel Alef occam-π XC Handel-C Sing# JCSP π-calculus Actor Model ... Erlang Scala Rust CSP78 CSP85 NewSqueak Limbo Go Story by Rob Pike: [http://goo.gl/KmYvA] Copyright 2012, James Whitehead II, All Rights Reserved. 6 / 17
  7. Concurrency in Go Goroutines The execution of a method or

    function call executing concurrently in the same address space as other goroutines. go doSomething( "http://golang.org" ) • anonymous and decoupled • no hierarchy Copyright 2012, James Whitehead II, All Rights Reserved. 7 / 17
  8. Concurrency in Go “Do not communicate by sharing memory. Instead,

    share memory by communicating.” Channels • message passing pipe • send waits for receive (and vice versa) • statically typed • first-class values • any-to-any communication ch <- 5 // send a message on the channel val := <-ch // receive a value from the channel Copyright 2012, James Whitehead II, All Rights Reserved. 8 / 17
  9. Signalling completion // A function that performs some computation func

    doSomething(url string, done chan bool) { // perform some work done <- true } // The main entry point of the program func main() { done := make(chan bool) go doSomething( "http://golang.org" , done) <-done // without this, the program would exit immediately! } Copyright 2012, James Whitehead II, All Rights Reserved. 9 / 17
  10. Uses for channels Channels are the basis for communication between

    goroutines: • Semaphores (buffered channel) • Resource mutex • Asynchronous results (futures/promises) • Fork/join • Client registration/callbacks • Processing pipelines Copyright 2012, James Whitehead II, All Rights Reserved. 10 / 17
  11. Why choose Go? • Message passing over explicit channels •

    Considered: • CSP libraries for C, C++, Java, Python, etc. • Occam-π • Erlang, Scala • C-level language (systems) • Rich standard libraries (networking, http) • Static types with a dynamic “feel” • Simple, powerful concurrency Go is worth considering for its unique collection of features. Copyright 2012, James Whitehead II, All Rights Reserved. 11 / 17
  12. Case Study: Webpipes • Compositional web server toolkit • Components

    are simple functions • Sources, filters, and sinks • Pipelines • Process networks Published in [Communicating Process Architectures 2011]. Copyright 2012, James Whitehead II, All Rights Reserved. 12 / 17
  13. Case Study: MINIX file system • Notoriously sequential • Shared

    resources • Caching and coherency • Avoid explicit locking • Model using CSP Takes the microkernel principle and pushes it into the file server. Copyright 2012, James Whitehead II, All Rights Reserved. 13 / 17
  14. A “blocking” block device // Interface for a block device

    type BlockDevice interface { // Read a block into 'buf' from position 'pos' Read(buf interface{}, pos int64) error // Write the data from 'buf' to position 'pos' Write(buf interface{}, pos int64) error // Close the device Close() error } Copyright 2012, James Whitehead II, All Rights Reserved. 14 / 17
  15. A “blocking” block device // Turn a working BlockDevice into

    a faulty one type FaultyDevice struct { Real BlockDevice Blocked chan int64 Unblock chan bool } func (dev *FaultyDevice) Read(buf interface{}, pos int64) error { // Signal that we are blocked dev.Blocked <- pos // Wait for someone to unblock us <-dev.Unblock return dev.Real.Read(buf, pos) } Copyright 2012, James Whitehead II, All Rights Reserved. 15 / 17
  16. Conclusions • Go is digestible; it has a small set

    of orthogonal, powerful features • Concurrency is built into the language • Different perspective • Novel solutions to problems Want to learn more? • Communicating Sequential Processes • Tour the language: [http://tour.golang.org/] Copyright 2012, James Whitehead II, All Rights Reserved. 16 / 17