150 likes | 326 Vues
Chien-Hua Shann, Ting-Lu Huang and Cheng Chen National Chiao Tung University. IMPLEMENTATION OF A NON-BLOCKING QUEUE ALGORITHM. Nastaran Shafiei. 1. Outline. Algorithm Description Class Diagram Algorithm Implementation Results. 2. Finite array, Q Counters, FRONT and REAR
E N D
Chien-Hua Shann, Ting-Lu Huang and Cheng Chen National Chiao Tung University IMPLEMENTATION OF A NON-BLOCKING QUEUE ALGORITHM Nastaran Shafiei 1
Outline • Algorithm Description • Class Diagram • Algorithm Implementation • Results 2
Finite array, Q Counters, FRONT and REAR Operations: enqueue, dequeue Elements of the queue The Algorithm 3
NonblockingQueue L: int Queue: AtomicLongArray REAR: AtomicLong FRONT: AtomicLong getValPart(long item) getRefPart(long item) getQueueItem(int val, int ref) main(String[] args) Enqueue Dequeue val: int Enqueue(int value) void run() void run() Thread … …
Shared Variables public 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() 5
Implementation of Array Elements An Array Element: getCounter(long element) ; 32 bits 32 bits int val int ref 64 bits val ref << ref >> ref
Implementation of Array Elements getQueueValue(long element) getQueueEntryType(int val, int ref) val ref >> val val << val + ref = val ref
Make private copies using get(): rear = REAR.get(); front = FRONT.get(); x = Queue.get ((int)rear % L); Check conditions: rear == REAR.get() rear != FRONT.get() + L Implementation of Enqueue.run() 8
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() 9
Algorithm Behaviours • Point contention Number of simultaneously active processes Number of threads: 2 Number of total operations: 200 Thread1 Thread2 # op: 100 # op: 100 11
Algorithm Behaviours • Point contention Number of simultaneously active processes Number of threads: 4 Number of total operations: 200 Thread1 Thread2 Thread3 Thread4 # op: 50 # op: 50 # op: 50 # op: 50 12
Algorithm Behaviours • Point contention Number of simultaneously active processes Number of threads: 8 Number of total operations: 200 Thread5 Thread6 Thread7 Thread8 Thread1 Thread2 Thread3 Thread4 # op: 25 # op: 25 # op: 25 # op: 25 # op: 25 # op: 25 # op: 25 # op: 25 13
Questions? 15