Go-style concurrency

New 30May2025 – from actual date 26Mar2014. In group TECH NOTES and My Go (golang) notes.

Go-style concurrency. Read as PDF  (2014) (at Embedded.TRD 26Mar2014)

I have pasted the text below, to make it searchable in WordPress. Then read in the PDF.


Go-style concurrency

Øyvind Teig
@embedded.TRD 26. March 2014

25March2014: 19:51

1

CSP and process-oriented programming

Even after a long history of channel-based concurrency it is not mainstream

In the light of Google’s new programming language Go, this lecture will search
for shores around the Channel Islands

3

* Autronica Fire and Security AS (AFS)
* Owned by United Technologies Corporation (UTC)
* Part of UTC Building and Industrial Systems
* Employs more than 380
* 25 R&D (Trondheim)
* Considering some internationalisation
* Headquarter in Trondheim
* Mainly fire detection
* NOK 825 mill

4

Disclaimers

In this lecture I present my own interests and views (But the themes would be relevant to some reality at Autronica)

This lecture does not reveal any Autronica-sensitive information

5

// an embedded programmer (since 1978)
// not a computer scientist// not a computer scientist
// a blogging «coder» @ https://www.teigfam.net/oyvind/home/
I am..

6

// an embedded a (since 1978)
// not a computer scientist// not a computer scientist
// a blogging «coder» @ https://www.teigfam.net/oyvind/home/

I am..
// NTH 1976, Autronica 1976-, engine systems, fluid level, fire detection, HW, SW,
// Asm, MPP Pascal, PL/M, Modula 2, occam, (Java, Perl), C, small RTX systems, published, blogging

Goal

* To make you curious about how CSP-based multi-threaded
systems with synchronous blocking communication
can be an alternative to

* single-threaded systems with asynchronous non-blocking calls,
registering of callbacks and non-local event-loop to pick up the
result of an off-line handling

8

On the iron

Pieter Schelte (2013)
Disney Fantasy (2012)
++

AutroKeeper
(BNA-180)

Safe Return to Port

Dual Safety
AutroKeeper: patented 329859 in Norge, PCT/NO2009/000319 EU (granted as #2353255)

9

Mind map?

* This lecture is a Mind Map (like brainstorming?)
* of what I have learnt lately (the odd matter)
* in the light of my personal experience (biased)
* and the background I read about you (rather mixed?)

10

Go-style concurrency, based on

* CSP = Communicating Sequential Processes
* CSP is a process algebra (Hoare, 1978, 1985)
* Influential (3rd most cited paper at some time)
* Model with CSPm and formally verify with FDR2 / FDR3
* Also PAT (Process Analysis Toolkit) from Singapore

11
Single-threading sometimes is more multi-threaded than one might think

12

Multi-threading shown with an (almost) single-threaded language

node-csp
«Communicating sequential processes for Node.js»
«Go style concurrency with channels»
(an experiment)

13
node-csp

* Uses the asynchronous event system of Node.js
* and «generators» of EcmaScript6 (ES6)
* Written in JavaScript, just to explore any potential

Blog:https://www.teigfam.net/oyvind/home/technology/084-csp-on-node-js-and-clojurescript-by-javascript/node-csp (example interleave.js)

Code removed
By:
Ola Holmström & David Nolen

15
node-csp (example interleave.js)

Code removed
Src:
https://github.com/olahol/node-csp/blob/master/examples/interleave.js#L1

Concurrency: doing more than one thing

* «Single-threaded»
* Multi-threaded
* Concurrent
* Parallel

17

Concurrency is all over

* Web pages (in browsers)
* Applications (on Linux, Windows, OS X)
* Embedded systems
* Integrity, VxWorks, Linux, some run-time system and scheduler, even main+interrupts
* Languages: occam (was), Go (at least not yet), Ada, Erlang, ..?
* «Synchronous system» with Rate Monotonic Scheduling etc. not in this lecture

18

«A person’s mental model is probably multithreaded, although this is rarely conscious»
Trygve Reenskaug (in a mail)

19

Unit of simultaneousness

* “Process model”
* By a ready tool (language, pattern) or built from a kit (library, pattern, stereotype)?
* Traditional objects in OO are only half the way:
* Java: «class MyThread implements runnable» (interface)

20

Unit of locality

* Concurrency + multi-core = parallel
* Shared or distributed memory
* Go has a shared memory model,
occam has shared/distributed; both use channels
Ada?
Erlang?

21

Single-threaded

* How can you do simultaneous things non-spaghetti’ish?
* Single-threaded + non-blocking = register callback function and treat it in a (global, local) event loop = asynchronous
* Single-threaded + many independent jobs = communicating callbacked functions = probably not..

22
Single-threaded?

* This not as “single-threaded” as you’d like to think
* “Callback hell”(?)
* Even W3C’s DOM has mutexes (DOM = Document Object Model) It’s a «storage mutex»

23

Process model

* Many languages add concurrency or parallelism as an afterthought
* First real language was Concurrent Pascal, 1975 (Per Brinch Hansen)
* C11: thread C++11: thread, futures etc.
* Process: not only for concurrent activities
* Also as abstraction of a different type of “object” (=”process”)
* Encapsulate state

24

Process-oriented

occam
PAR
P(c)
C(c)»
Go

go P(c)
go C(c)
// Some join

25

pyCSP

import sys
from pycsp.parallel import *

Code removed

https://code.google.com/p/pycsp/

26

Communication

* Often the concurrent units need to communicate
* Shared memory and/or messages
* Contracts: protocols, typed (with language support?)
* This is not a pipe
* It’s a channel (or a rendezvous/Ada)
* “First class” = like sending a channel over a channel

27

Messages

* Channels carry typed messages (as said, some even send channels), zero buffered (synchronous) or buffered (asynchronous).
* When blocking, synch and comm is the same event
* SDL (Spec & Design Lang) (and UML(?)) do send-and-forget ofmessages into infinite buffers: asynchronous
* I have blogged a lot about the pros and cons, no repeat here
* Both may be used to build communication FSM (Finite State Machine)s

28

try (to say in 1990)

«Processes and synchronous, blocking channels»

29

catch (the late response in 2000)

* “nobody else does it”
* “the problem you say it solves isn’t ours”
* «use send-and-forget»

30

(digest) until (2009)

* Google delivers Go
* It enforces “occam thinking”
* in a wonderfully different, old & new way
* “Go-style concurrency”
* Bell Labs had now also been doing it for 30 years
* (Now they dared say it) “Bell Labs and CSP Threads” by Russ Cox see http://swtch.com/~rsc/thread/

31

«Why build concurrency on the ideas of CSP?»

* From Google Go language’s authors:
* Mutexes 🙁
* Condition variables 🙁
* Memory barriers 🙁
* Higher-level interfaces enable much simpler code 🙂

http://golang.org/doc/faq#csp

32

«Why build concurrency on the ideas of CSP?»

* Successful: CSP 🙂
* Occam and Erlang 🙂
* Go’s channels as first class objects 🙂
* Fits well into a procedural language framework 🙂

33
Nothing really «blocks»

* Processor cycles seldom used to spin around waiting for a single resource
* So “blocking” entails being able to do something else in the meantime
* Like sleep, or callback, directly or hidden in a language mechanism
* This is the basic problem
* If there is no good mechanism to handle this, then “blocking is evil” is a valid fear of spaghetti concurrency
* And at some (but not all) levels timeout must catch it
* Then even the “I program single-threaded” statement is a valid response

34

“Blocking is evil”

* Asynchronous and non-blocking
* Synchronous and blocking
* Neither is «evil» if both mechanisms are easily available
* But in a safety-critical system buffered must have control of max buffer need

35

Buffer where?

* Buffer in a pipe-type untyped buffer, «max 1500 bytes»?
* Or in a typed buffered channel?, «message»
* Or inside a process?, «flush, prioritise»
* Why buffer in the tube and not in a tank?
* Worse: a tube is not an expansion tank (overflow?)

36

Rich interface component modelling

* A component also has dynamic properties not only a standard API
* Can be modelled in several languages CSP, UML..
* To make verified connectable sw components: Less testing(?)
* I have tried to understand this:

Rich interface component modeling

* I am looking forwards to hearing BitReactive (here now?) talk about their solution

37
Autronica (C.V.)

* Autronica shipped several products programmed in occam in the nineties
* …
* Diesel engine data acquisition and computation unit (NK-200). With transputers
* Radar-based fluid level gauging (GL-100, now Kongsberg product). With signal processor and occam to C-with-scheduler translator
* Transputers and occam were “CSP engines”

38

Autronica (C.V.)
* Some CSP-based run-time schedulers used in ARM, Atmel ATmega, XMEGA
* One built on top of an asynchronous SDL-based kernel (channel ready then run process converted run-«messages»)
* A «naked», with synchronisation points generated by a tool in ANSI C (much like the yield we started with) (Channel ready is synchronisation only)
* Published papers and discussion of the pros and cons of each of these
* (But we mostly seem to use the SDL-based systems for fire loop units)

39

Abstracts removed

43

* There are other stories from Autronica
* To learn more Go, register on: golang-nuts
* This lecture will be on my home page / blog:

https://www.teigfam.net/oyvind/home/

44

* There are
* To learn more Go, register on: golang-nuts
* This lecture will be on my home page / blog:

https://www.teigfam.net/oyvind/home/

* There are
* To learn more Go, register on: golang-nuts
* This lecture will be on my home page / blog:

https://www.teigfam.net/oyvind/home/
Thank you