Upload
stewart-small
View
228
Download
1
Tags:
Embed Size (px)
Citation preview
COMP 640 – CWB 2
Topics
Brief review of Concurrent Computing
Two language approaches to dealing with concurrent programming Java Occam – next week
COMP 640 – CWB 3
Concurrent Computing Concurrent program: 2 or more
execution contexts Parallel program: concurrent with
simultaneous execution Distributed program: on separate
processors Multiprocessing: in separate processes
(separate memory) Multi-threaded: same process (common
memory)
COMP 640 – CWB 6
Dijkstra's Semaphores
P(s): if(s>0) s--; else block(); V(s): if(isBlocked) wakeup(); else
s++; Uses:
Cooperative synchronization E.g., producer/consumer
Critical section locking
COMP 640 – CWB 7
Monitors Protect critical sections Condition variable represents a lock
List of waiting threads wait action: thread blocks until signaled signal action: running thread holding lock
releases a blocked thread to run Hoare monitor: signaling thread loses the lock
and released thread run immediately (no other thread can intervene)
Mesa monitor: signaling thread keeps the lock; signaled thread runs sometime after lock is released (another thread could potentially run instead)
COMP 640 – CWB 9
"Syntax" of Java Threads
new Thread(){
public void run() {
doSomeWork();
//thread is now terminating
}
}.start();No language
features
COMP 640 – CWB 10
Semantics of Java Threads
May be implemented on multiple processors and/or by time-slicing
Threads are created by a side effect of the creation of a Thread object
Become runnable when the start() method is called Running commences in the run() method Thread terminates when run() completes
COMP 640 – CWB 11
Semantics of Java Threads and Variables
All threads share main memory
A thread on a separate processor may have its own working memory
a = b;
Main
Memory
Thread
Execution
Engine
Working
Memory
load
read
write
assign
use
store
COMP 640 – CWB 12
Semantics of Java Threads – Ordering Rules for Working Memory Actions
a = b;
use and assign occur in order implied by program
A store must intervene between assign and load on a variable
An assign must intervene between a load/store and a subsequent store on a variable
Start of thread: an assign/load must precede a use/store
Creation of variable: an assign/load must precede a use/store
Otherwise, load or store may occur at any time
Main
Memory
Thread
Execution
Engine
Working
Memory
load
read
write
assign
use
store
COMP 640 – CWB 13
Semantics of Java Threads – Ordering Rules for Main Memory Actions
Every load must be preceded by a read
Every store must be followed by a write
The reads for 2 loads occur in the same order as the loads
The writes for 2 stores occur in the same order as the stores
Main
Memory
Thread
Execution
Engine
Working
Memory
load
read
write
assign
use
store
a = b;
COMP 640 – CWB 14
Example from Java Spec.
class Sample {
int a = 1, b = 2;
void hither() { a = b; }
void yon() { b = a; }
} Three orderings:
1. write a read a, read b write b
ah =2, hb=2, ma=2, mb=2, ya=2, yb=2
2. read a write a, write b read
ha=1, hb=1, ma=1, mb=1, ya=1, yb=1
3. read a write a, read b write b
ha=2, hb=2, ma=2, mb=1, ya=1, yb=1
precedes
COMP 640 – CWB 15
Amusing Statement
From the Java language spec:"In the absence of explicit
synchronization, an implementation is free to update the main memory in an order that may be surprising. Therefore the programmer who prefers to avoid surprises should use explicit synchronization. "
COMP 640 – CWB 16
Java Monitors: Syntax of Synchronized Blocks
SynchronizedStatement: synchronized ( Expression ) Block The type of Expression must be a reference
type, or a compile-time error occurs.
Source: Java Language Spec, Sect. 14.18
COMP 640 – CWB 17
Java Monitors: Syntax of Synchronized Methods
MethodModifier: one of public protected private abstract static final synchronized native strictfp
Source: Java Language Spec, Sect. 8.4.3
COMP 640 – CWB 18
Java Monitors: Semantics of Synchronized Methods
"A synchronized method acquires a lock (§17.1) before it executes. For a class (static) method, the lock associated with the Class object for the method's class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used."
Source: Java Language Spec, Sect. 8.4.3.6
COMP 640 – CWB 19
Java Synchronized Blocks - Semantics
1. Evaluate expression V2. If V is null, throw nullPointerException3. Lock the lock associated with V: may
block while another thread holds the lock4. Execute block5. When block completes (normally or
abnormally), unlock lock assoc. with V
Paraphrased from: Java Language Spec, Sect. 14.18
COMP 640 – CWB 20
Semantics of Java Threads – Ordering Rules for Locks
A lock may occur only if the numbers of locks and unlocks by another thread are equal.
An unlock may occur only if the number of unlocks by the same thread is less than the number of locks
A store (and its corresponding write) must intervene between an assign and an unlock.
An assign/load must intervene between a lock and a use/store. (I.e., a lock invalidates the working memory.)
lock
unlock
Main
Memory
Thread
Execution
Engine
Working
Memory
load
read
write
assign
use
store
COMP 640 – CWB 21
Example from Java Spec. with Locks
class SyncSample { int a = 1, b = 2; synchronized void hither() { a = b; } synchronized void yon() { b = a; } }
Three orderings?
1. write a read a, read b write b
ah =2, hb=2, ma=2, mb=2, ya=2, yb=2
2. read a write a, write b read
ha=1, hb=1, ma=1, mb=1, ya=1, yb=1
3. read a write a, read b write b
ha=2, hb=2, ma=2, mb=1, ya=1, yb=1
Two orderings:
COMP 640 – CWB 22
Semantics of Java Threads – Ordering Rules for Volatile Variables
A use must be immediately preceded by a load.
An assign must be immediately followed by a store.
In effect, the working memory cache does not exist.
lock
unlock
Main
Memory
Thread
Execution
Engine
Working
Memory
load
read
write
assign
use
store
COMP 640 – CWB 23
Java's wait() & notify()
Thread must hold lock on v before calling v.wait() or v.notify()
v.wait() add thread to the wait set of v release lock (i.e., if thread has done N more locks
than unlocks, do N unlocks) put thread in blocked state
v.notify() remove a thread, T, from wait set of v make T runnable T then locks the lock associated with v (which might
block) T then performs N-1 additional locks T then returns from the wait() method call
Mesa-style monitor
COMP 640 – CWB 24
Reference
The official description in the Java spec:
http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html#30206