Title: A study of waitfree queue algorithms for interthread communication for Java
1A study of wait-free queue algorithms for
inter-thread communication for Java
- by
- Daniel Cederman and Tord Dellsén
- Supervisor
- Philippas Tsigas
2Plan
- Real-Time and synchronization
- Real-Time Java
- Implementations
- Test Results
- Conclusion
3 Real-Time
4Real-Time
- Predictable and Consistent
- Speed is second priority
- Tasks have cost, deadline...
5Threads
- Different parts have different criticality
- Threads more appropriate than sequential
programming - Given different priorities
- Need to be synchronized
6Enabled-Late-Write
Shared Variables Counter 0
Thread As Variables i undefined
Thread Bs Variables i undefined
void incCounter() int i counter i i
1 counter i
7Enabled-Late-Write
Shared Variables Counter 0
Thread As Variables i 0
Thread Bs Variables i undefined
void incCounter() int i counter i i
1 counter i
8Enabled-Late-Write
Shared Variables Counter 0
Thread As Variables i 1
Thread Bs Variables i undefined
void incCounter() int i counter i i
1 counter i
9Enabled-Late-Write
Shared Variables Counter 0
Thread As Variables i 1
Thread Bs Variables i 0
void incCounter() int i counter i i
1 counter i
10Enabled-Late-Write
Shared Variables Counter 0
Thread As Variables i 1
Thread Bs Variables i 1
void incCounter() int i counter i i
1 counter i
11Enabled-Late-Write
Shared Variables Counter 1
Thread As Variables i 1
Thread Bs Variables i 1
void incCounter() int i counter i i
1 counter i
12Enabled-Late-Write
Shared Variables Counter 1
Thread As Variables i 1
Thread Bs Variables i 1
void incCounter() int i counter i i
1 counter i
13Enabled-Late-Write
Shared Variables Counter 1
Thread As Variables i 1
Thread Bs Variables i 1
void incCounter() int i counter i i
1 counter i
14Locking
- Use mutual exclusion allow only one thread
access at a time - Easy just add the keyword synchronized in Java
- Disadvantages
- Deadlock
- Priority Inversion
15Deadlock
R1
T1
R2
T2
16Priority Inversion
17Priority Inversion
Locks R
18Priority Inversion
Waits for R
Locks R
19Priority Inversion
Waits for R
Runs
Locks R
Unlocks R
20Priority Ceiling Emulation Protocol
- Priority Inversion can be solved using PCEP
- Whenever a thread acquires a lock it increases
its priority to the maximum priority, the ceiling - No other thread can now interrupt the thread with
the lock - When the thread releases the lock it lowers its
priority to its original value
21Lock-free
- Most of the time there is no conflict
- and when there is, we can just try again
- but we need to know when a conflict has occured
22Compare-And-Swap (CAS)
- Atomic CPU instruction
- CAS takes three arguments value (shared
variable), oldvalue (local) and newvalue (local) - It compares oldvalue with value and if they
match, updates value to newvalue
23Compare-And-Swap
while(!written) oldValue value newValue
doStuff(oldValue) written CAS(value,
oldValue, newValue)
24Compare-And-Swap
while(!written) oldValue value newValue
doStuff(oldValue) written CAS(value,
oldValue, newValue)
25Problem
- Unbounded
- A thread might have to retry an indefinite amount
of times - Real-time systems must be predictable
26Wait-Free
- Like lock-free, but more suitable for real-time
because of predictability - Bounded
- More complex
- Uses helping More on this later
- Increase priority every time to convert lock-free
to wait-free
27 RTSJ
28RTSJ
- Real-Time Specification for Java
- The Real-time for Java Expert Group
- Implementation created by TimeSys
29RTSJ
- Real-Time threads
- Schedulers
- High resolution timing
- Special memory types
- Wait-free queues for inter-thread communication
30Wait-Free Queues
- Used for one-way communication between threads
- Java classes WaitFreeWriteQueue and
WaitFreeReadQueue - It has a read and a write function
- One thread writes to the queue and the other one
reads from the queue - Only one side is wait-free either the read or
the write function the other side is blocking
31Example using the queue
32Thread B writes to the queue
33Thread C writes to the queue
34Thread C writes to the queue
35Thread Z reads from the queue
36 Implementations
37The Reference Implementation
- Made by TimeSys
- Works as a blocking queue
- Uses PCEP
- This is allowed by the specification
38The TimeSys algorithm
39The Anderson algorithm
40James Anderson et al.'s algorithm
- Uses a linked list with two sentinels
Head
Tail
A
B
41Writing to the queue
- First the object to be written, C, is stored in a
node
Head
Tail
A
B
C
42Writing to the queue
- The new node is made to point at the same node
that the head node is pointing at using CAS
Head
Tail
A
B
C
43Writing to the queue
- Finally the head nodes pointer is changed to
point to the new node using CAS
Head
Tail
A
B
C
44Reading from the queue
- First the third last node is found by traversing
the queue from the head
Head
Tail
A
B
45Reading from the queue
- Its pointer is changed so that it points to the
tail node using CAS
Head
Tail
A
B
46Reading from the queue
- Node B is no longer in the linked list and can be
read
Head
Tail
A
47Compare-and-swap
- CAS is not yet available in Java.
- We emulate CAS using an algorithm described by
Rammamurthy et. al. - This emulation is unfortunately very slow and
memory intensive
48Wait-free?
- Using CAS could perhaps be enough to make the
queue lock-free, but how to make it wait-free? - We make use of helping using a method described
by Anderson et. al. called incremental helping
49Incremental Helping
- Have we interrupted another write or read?
- If so, perform the other threads read or write
first - Announce that we are perfoming a read or a write
- Perform the read or write
- Remove the announcement
50Incremental Helping
- If we should be interrupted our operation will be
done by the interrupting thread. - This means that our operation is guaranteed to
succeed whenever we call the write or read
function - Either we do it ourselves or get helped by
another thread
51Incremental Helping
- Task C announces its read operation but is
interrupted before it could complete it
52Incremental Helping
- Task B sees that Cs operation isnt completed so
it begins helping it, but is interrupted by A
53Incremental Helping
- A helps C and does its own operation. Since B
hasnt announced its operation it is not helped
54Incremental Helping
- When A is finished B notices that C has already
been helped so it performs its own operation
55Incremental Helping
- When B finishes task C sees that it has already
been helped so its done
56The ptyz algorithm
57Philippas Tsigas and Yi Zhangs Algorithm
- We call it ptyz
- It also uses linked lists
- Only one side is wait-free as in the
specification - We only describe the wait-free write queue, the
wait-free read queue works in a similar way - Helps all, not just one (not incremental helping
like anderson)
58Wait-Free Write Queue
- Instead of only one tail it has a tail for each
priority, but only one is the actual tail of the
linked list
Head
C
1
B
A
D
2
3
59Reading from the queue
- Reading is simple since its blocking, just make
the head variable point to the next node in the
list
Head
C
1
B
A
D
2
3
60Reading from the queue
A
C
1
B
D
2
Head
3
61Writing to the queue
- First we need to find the actual tail.
Head
C
1
B
A
D
2
3
62Writing to the queue
- We go through the tail array looking for the node
that doesnt point to another node
Head
C
1
B
A
D
2
3
63Writing to the queue
- We make it point to the new node
Head
C
1
B
A
D
2
E
3
64Writing to the queue
- Then we store it in the tail array at the threads
priority, in this case 1
Head
E
1
B
A
D
2
C
3
65Writing to the queue
- By doing this we avoid the enabled-late-write
that could have occured when changing the tail
Head
E
1
B
A
D
2
C
3
66 Test Results
67Test Setup
68Test Results
- Software emulated CAS
- Has to help reads as well as writes
- Read has to go through the whole list
69Ptyz vs TimeSys
- TimeSys uses PCEP
- Cant interrupt lower priority threads
70Memory usage
- Timesys uses an array
- Anderson
- Ptyz
71Conclusions
- Anderson is slow (software emulated CAS, etc)
- Timesys has better memory performance than ptyz
- Ptyz is faster on high priorities
- Ptyz is wait-free
72Finally
- The Real-Time Java Specification is available on
www.rtj.org (no s) - The thesis (and some other material including
this presentation) www.dtek.chalmers.se/d00ceder
/exjobb/ - Questions?