Chien-Hua Shann, Ting-Lu Huang and Cheng Chen National Chiao Tung University
description
Transcript of Chien-Hua Shann, Ting-Lu Huang and Cheng Chen National Chiao Tung University
11
Chien-Hua Shann, Ting-Lu Huang and Cheng Chen National Chiao Tung University
Nastaran Shafiei
IMPLEMENTATION OF A NON-BLOCKING QUEUE ALGORITHM
22
Algorithm Description Class Diagram Algorithm Implementation Results
Outline
33
Finite array, Q Counters, FRONT and REAR Operations: enqueue, dequeue Elements of the queue
The Algorithm
4
NonblockingQueue
L: int Queue: AtomicLongArray REAR: AtomicLong FRONT: AtomicLong
Enqueue
val: int Enqueue(int value) void run()
Dequeue
void run()
Thread
…
…
getValPart(long item)getRefPart(long item) getQueueItem(int val, int ref)main(String[] args)
55
Shared Variablespublic static AtomicLongArray Queue;public static AtomicLong RAER;public static AtomicLong FRONT;
Atomic variables java.util.concurrent.atomic get() and set() Extend the concept of volatile variables compareAndSet()
6
Implementation of Array ElementsAn Array Element:
getCounter(long element) ;
val ref ref ref<< >>
int ref int val
32 bits
64 bits
32 bits
7
Implementation of Array Elements getQueueValue(long element)
getQueueEntryType(int val, int ref)
val ref val>>
val ref<< + val = val ref
88
1. Make private copies using get(): rear = REAR.get(); front = FRONT.get(); x = Queue.get ((int)rear % L);
2. Check conditions: rear == REAR.get()
rear != FRONT.get() + L
Implementation of Enqueue.run()
99
3. Check the content of the array element: Empty - Attempt to store an item and increment
REAR using CAS
if (Queue.compareAndSet((int) (rear % L) , x , newValue ))
REAR.compareAndSet( rear , rear+1);
Full - Help the other process
if( getQueueValue(Queue.get((int)rear % L)) != 0)
REAR.compareAndSet( rear , rear+1);
Implementation of Enqueue.run()
1010
11
Algorithm Behaviours
Point contentionNumber of simultaneously active processes
Number of threads: 2Number of total operations: 200
11
Thread1
# op: 100
Thread2
# op: 100
12
Algorithm Behaviours
Point contentionNumber of simultaneously active processes
Number of threads: 4Number of total operations: 200
12
Thread1
# op: 50
Thread3
# op: 50
Thread2
# op: 50
Thread4
# op: 50
13
Algorithm Behaviours
Point contentionNumber of simultaneously active processes
Number of threads: 8Number of total operations: 200
13
Thread1
# op: 25
Thread3
# op: 25
Thread2
# op: 25
Thread4
# op: 25
Thread5
# op: 25
Thread7
# op: 25
Thread6
# op: 25
Thread8
# op: 25
1414
1515
Questions?