+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
cccc41d
master
1import java .util .concurrent .locks .Lock ; 2import java .util .concurrent .locks .ReentrantLock ; 3public class ConcLockList {//Adapted from Herlihy's Book 4private Node head ; 5public ConcLockList () { 6head =new Node (Integer .MIN_VALUE ); 7head .next =new Node (Integer .MAX_VALUE ); 8 } 9public boolean insert (Object item ) { 10int key =item .hashCode (); 11head .lock (); 12Node pred =head ; 13try { 14Node curr =pred .next ; 15curr .lock (); 16try { 17while (curr .key <key ) { 18pred .unlock (); 19pred =curr ; 20curr =curr .next ; 21curr .lock (); 22 } 23if (curr .key ==key ) { 24return false ; 25 } 26Node newNode =new Node (item ); 27newNode .next =curr ; 28pred .next =newNode ; 29return true ; 30 }finally { 31curr .unlock (); 32 } 33 }finally { 34pred .unlock (); 35 } 36 } 37public boolean remove (Object item ) { 38Node pred =null ,curr =null ; 39int key =item .hashCode (); 40head .lock (); 41try { 42pred =head ; 43curr =pred .next ; 44curr .lock (); 45try { 46while (curr .key <key ) { 47pred .unlock (); 48pred =curr ; 49curr =curr .next ; 50curr .lock (); 51 } 52if (curr .key ==key ) { 53pred .next =curr .next ; 54return true ; 55 } 56return false ; 57 }finally { 58curr .unlock (); 59 } 60 }finally { 61pred .unlock (); 62 } 63 } 64public boolean contains (Object item ) { 65Node last =null ,pred =null ,curr =null ; 66int key =item .hashCode (); 67head .lock (); 68try { 69pred =head ; 70curr =pred .next ; 71curr .lock (); 72try { 73while (curr .key <key ) { 74pred .unlock (); 75pred =curr ; 76curr =curr .next ; 77curr .lock (); 78 } 79return (curr .key ==key ); 80 }finally { 81curr .unlock (); 82 } 83 }finally { 84pred .unlock (); 85 } 86 } 87private class Node { 88Object item ; 89int key ; 90Node next ; 91Lock lock ; 92Node (Object item ) { 93this .item =item ; 94this .key =item .hashCode (); 95this .lock =new ReentrantLock (); 96 } 97Node (int key ) { 98this .item =null ; 99this .key =key ; 100this .lock =new ReentrantLock (); 101 } 102void lock () {lock .lock ();} 103void unlock () {lock .unlock ();} 104 } 105} 106