A Whirlwind Tour Through Concurrency: Kedar Namjoshi Bell Labs
A Whirlwind Tour Through Concurrency: Kedar Namjoshi Bell Labs
Concurrency
Kedar Namjoshi
Bell Labs
Columbia University, Jan. 2008
1
Outline
Concurrency
Common Concurrency Patterns
The Plan9 Thread Model
Reasoning about Concurrency
2
Concurrency
3
Concurrency in programming
For a long time, concurrency has been a concern
mainly for designers and implementers of
– operating systems
– supercomputers
– processor chips
– database systems
– and phone networks …
4
Multi-Core Processors
(from http://www.amd.com)
5
Shared-Memory Concurrency
initially, account x holds $0
6
Shared-Memory II
Let’s take a closer look
P1: t1 := x; t1 := t1+50; x := t1;
P2: t2 := x; t2 := t2+100; x := t2;
7
Shared-Memory III
Atomicity must be enforced.
8
Shared Memory IV
Compilers can get in the way, too!
P1: P2:
x := 0; y := 0;
y := 1; x := 1;
P1’: P2’:
y := 1; x := 1;
x := 0; y := 0;
9
CSP [Hoare 1978]
CSP: Communicating Sequential Processes
Three key concepts
No shared variables
Processes communicate through synchronized send-receive
actions
A process may respond to one of a set of communication
actions
10
Communication Patterns
Often, the most natural description of a concurrent
program is through its communication pattern.
11
1. The knock on the door …
… also known as a “real-time interrupt”
C + Unix : signals Java: Thread.interrupt()
“As captain of the crew I had had extensive experience (dating back to 1958) in making
basic software dealing with real time interrupts and I knew by bitter experience that as a
result of the irreproducibility of the interrupt moments, a program error could present
itself misleadingly like an occasional machine malfunctioning. As a result I was terribly
afraid. Having fears regarding the possibility of debugging we decided to be as careful
as possible and —prevention is better than cure!— to try to prevent bugs from entering
the construction.”
12
How does one prevent bugs?
The non-determinism introduced by the real-time
interrupt implies that a program execution may be
stopped at any point, in any intermediate state.
13
2. Copy
process copy(channel in, channel out)
var x;
*[in?x out!x]
14
3. Pipeline
A chain of N (N >= 1) copy processes:
copy(c0,c1) || copy(c1,c2) || … || copy(c(N-1),c(N))
15
4. Delegation: Taxi Rank
All taxis follow the same protocol.
process Taxi[i]:
taxi-rank! i; *[dispatch[i]?p drive(p); taxi-rank! i]
process Dispatcher:
*[queue?p (taxi-rank?i dispatch[i]!p)]
16
5. Delegation: Futures
process A:
toB ! (“x+2”,x:10);
…. stuff ….
fromB ? y;
Process B:
*[toB? (expr, binding) fromB! eval(expr,binding)]
17
6. Co-authoring a paper
process Kedar:
toDennis! token;
*[fromDennis? token write; toDennis! token]
process Dennis:
*[toDennis? token write; fromDennis! token]
18
7. Callback
K:
C! “let me know if you can’t pick up the kids today”;
[work interrupt (C? “can’t do it” pick up kids)]
19
8. Multiplexing
Merge messages from two channels into a single channel
process mux:
*[
| channel1? x out! x
| channel2? y out! y
]
The operator “|” defines alternatives. If both alternatives are available,
the choice is made non-deterministically.
20
9. De-Multiplexing
Copy input from one channel into two
process demux:
*[ input? x out1! x; out2! x]
Or, perhaps:
process demux:
*[input? X if (x>0) out1!x; else out2!x]
21
10. On Friday …
K:
C! “let me know if you can’t pick up the kids today”;
[work interrupt
(
| C? “can’t do it” pick up kids
| Al? “would you like to teach a class?” chat
| Dennis? “lunch?” go to lunch
|…
)
]
22
The Plan9 Thread Model
Plan9 is an operating system developed at Bell Labs from about 1990 onwards.
Lots of interesting features.
In particular, the OS thread model influenced by CSP.
23
What is the Plan9 thread model?
Two kinds of entities: processes and threads.
24
How is it used?
Typically, shared data is held in one process, and
accessed through commands sent through a
channel (e.g., read, write)
Hence, no locks. Synchronization is enforced
through channels. Hence, no race conditions
Clean fit to the design
25
Programming Languages: Shared-
variable concurrency
High-Performance Fortran (HPF), OpenMP, X10,
C*: designed for high-performance, scientific
computing. “forall” construct, data layout
specification
Java: Threads, synchronized methods, external
locks. Also, lock-free implementations of basic data
structures in java.util.concurrent
C/C++: concurrency provided by external thread
libraries (e.g., pthreads). C++ will include language
constructs for concurrency in its next version
OCaml/F#: OCaml and F# use external thread
libraries. F# has a concurrent garbage collector
26
Programming Languages: Message-
passing concurrency
Ada: processes (called tasks) communicating
through “rendezvous” operations (like CSP ! and ?)
Erlang: processes communicating though
unbounded message queues. Used by Ericsson for
programming telephony switches
MPI: Message-passing interface. Used for wide-
area and supercomputer applications
27
Bell Labs Research
29
The “impedance mismatch”
Device OS’s
and
platforms
Devices
Service
Eclipse-based SCE Developer/ SIP, SIP
SIP App Blender Servlets, SB
Server Steplets, …
(including
SB)
Web
Services Web Services,
App SOAP, WSDL,
Server …
Other?
...
Programming of converged services: programmers still
think in multiple paradigms
– Web: HTTP, SOAP, WSDL, XML
– All-IP Telecom: SIP, IMS, Parlay/ParlayX, AIN, PacketTV,
XMPP, …
30
SDT Model
Three basic building blocks
Party: e.g., kedar@cellphone, dennis@IMserver, aho@laptop
Bubble: a group of parties in conversation. The group could be
empty, or have a single person in it (usually a temporary situation)
Session: a group of bubbles, related in some way
31
SDT Model
Session Manager
Session 1
Bubble 1 Bubble 2 Global Address
Book /
Bob Social Network
[ people,
Alice devices,
friends ]
Deborah
Charles
32
“Hello World” in SDT
// register as a client for a session manger
ISessionManager sm = SessionManager.register(“config.properties”,
this);
// …… talk ……
System.out.println(“Hello World!”);
Thread.sleep(whatever);
33
A bit more …
// set up a handler for pickup actions
public class pickupHandler implements IHandler {
public Response run(TriggerBinding[] b) {
num_pickup++;
return Response.Continue;
}
}
34
And that’s it!
The session/bubble model is simple, yet very flexible, thanks to
the notify-respond mechanism.
35
What we have done
We have an implementation of this model, running on
an externally available server
We have written sample applications, including:
A customizable, network-hosted, speed-conference
Through a web interface, set your phone buttons to perform
SDT actions. As the service is network-hosted, these
bindings work from anywhere. [subject to some fine print]
A conference system with the ability to create
“whisper” sub-sessions
36
What you might do
Design a scripting language that hides some of the
Java intricacies
Design a graphical, web-based language to
implement your own view of speed dial
Create a conferencing application which you can
distribute through Facebook
… or something far more interesting!
37