250 likes | 382 Views
Nesting Examples. Eliot Moss moss@cs.umass.edu University of Massachusetts, Amherst. From HashSet to THashSet. public class THashSet<K,V> extends HashSet<K,V> implements TSet<K,V> { private static TransactionObjectLocks locks = new TransactionObjectLocks();
E N D
Nesting Examples Eliot Moss moss@cs.umass.edu University of Massachusetts, Amherst Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet public class THashSet<K,V> extends HashSet<K,V> implements TSet<K,V> { private static TransactionObjectLocks locks = new TransactionObjectLocks(); // methods coming . . . } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet (2) public boolean add (E o) { // Note: does not speed concurrent adds openatomic { locks.acquire(o, LockModeSX.Exclusive); boolean result = super.add(o); if (result) { // if new, is a change to set locks.acquire(this, LockModePinChange.Change); } return result; } onabort { if (result) super.remove(o); } ondone { locks.release(); } } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet (3) public boolean contains (Object o) { openatomic { locks.acquire(o, LockModeSX.Share); return super.contains(o); } ondone { locks.release(); } } } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet (4) public boolean isEmpty () { openatomic { locks.acquire(this, LockModePinChange.Pin); return super.isEmpty(); } ondone { locks.release(); } } } public Iterator<E> iterator () { openatomic { locks.acquire(this, LockModePinChange.Pin); return super.iterator(); } ondone { locks.release(); } } } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet’ public class THashSet<K,V> extends HashSet<K,V> implements TSet<K,V> { // no explicit “locks” field // methods coming . . . } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet (2’) @openatomic( inverse=“if (@result) super.remove(o);”, locks={ “[o, SX.Exclusive]”, “if (@result) [this, PinChange.Change]” } ) public boolean add (E o) { return super.add(o); } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet (3’) @openatomic(locks={“[o,SX.Shared]”}) public boolean contains (Object o) { return super.contains(o); } Dagstuhl Atomicity Workshop 3/2006
From HashSet to THashSet (4’) @openatomic( locks={“[this,PinChange.Pin]”} ) public boolean isEmpty () { return super.isEmpty(); } @openatomic( locks={“[this,PinChange.Pin]”} ) public Iterator<E> iterator () { return super.iterator(); } Dagstuhl Atomicity Workshop 3/2006
Employee Using Closed Nesting public class Employee { private static TSet<Employee> emps = new THashSet<Employee>(); private static TMap<String,Employee> byName = new THashMap<String,Employee>(); private static TMap<String,Employee> byPhone = new THashMap<String,Employee>(); private String name; private String phone; private Float salary; private Employee(String n, String p, Float s){ name = n; phone = p; salary = s; } Dagstuhl Atomicity Workshop 3/2006
Employee Using Closed Nesting (2) public String getName () { return name; } public void setSalary (Float s) { salary = s; } public void setPhone (String p) { atomic { // closed nesting byPhone.remove(this.phone); this.phone = p; byPhone.put(p, this); } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Closed Nesting (3) public static Employee add ( String n, String p, Float s) { atomic { // closed nesting Employee emp = new Employee(n, p, s); emps.add(emp); byName .put(n, emp); byPhone.put(p, emp); return emp; } } public static Employee getByName (String n) { return byName.get(n); } Dagstuhl Atomicity Workshop 3/2006
Employee Using Closed Nesting (4) public static boolean remove (String n) { atomic { // closed nesting Employee emp = byName.get(n); boolean result = (emp != null); if (result) { emps .remove(emp); byName .remove(emp.name ); byPhone.remove(emp.phone); } return result; } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting public class Employee { private static TransactionObjectLocks locks = new TransactionObjectLocks(); private static TSet<Employee> emps = … private static TMap<String,Employee> byName = private static TMap<String,Employee> byPhone = private String name; private String phone; private Float salary; private Employee(String n, String p, Float s){ name = n; phone = p; salary = s; } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (2) public String getName () { openatomic { locks.acquire(name, LockModeSX.Share); return name; } ondone { locks.release(); } } public void setSalary (Float s) { openatomic { locks.acquire(name, LockModeSX.Exclusive); salary = s; } ondone { locks.release(); } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (3) public void setPhone (String p) { openatomic { locks.acquire(name, LockModeSX.Exclusive); String oldp = phone; byPhone.remove(phone); phone = p; byPhone.put(p, this); } onabort { byPhone.remove(phone); phone = oldp; byPhone.put(oldp, this); } ondone { locks.release(); } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (4) public static Employee add ( String n, String p, Float s) { openatomic { locks.acquire(n, LockModeSX.Exclusive); locks.acquire(emps, LockModePinChange.Change); Employee emp = new Employee(n, p, s); emps.add(emp); byName .put(n, emp); byPhone.put(p, emp); return emp; } onabort { emps.remove(emp); byName.remove(n); byPhone.remove(p); } ondone { locks.release(); } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (5) public static Employee getByName (String n) { openatomic { locks.acquire(n, LockModeSX.Share); return byName.get(n); } ondone { locks.release(); } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (6) public static boolean remove (String n) { openatomic { locks.acquire(n, LockModeSX.Exclusive); Employee emp = byName.get(n); boolean result = (emp != null); if (result) { locks.acquire(emps, LockModePinChange.Change); emps .remove(emp); byName .remove(emp.name ); byPhone.remove(emp.phone); } return result; } onabort { emps.add(emp); . . . } } ondone { locks.release(); } } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting’ public class Employee { // no explicit “locks” field private static TSet<Employee> emps = … private static TMap<String,Employee> byName = private static TMap<String,Employee> byPhone = private String name; private String phone; private Float salary; private Employee(String n, String p, Float s){ name = n; phone = p; salary = s; } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (2’) @openatomic(locks={“[name, XS.Shared]”}) public String getName () { return name; } @openatomic( locks={“[name,XS.Exclusive]”}, inverse=“salary = @old(salary);” ) public void setSalary (Float s) { salary = s; } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (3’) @openatomic( locks={“[name, SX.Exclusive]”}, inverse=“byPhone.remove(phone); “ + “phone = @old(phone); “ + “byPhone.put(phone),this);” ) public void setPhone (String p) { byPhone.remove(phone); phone = p; byPhone.put(p, this); } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (4’) @openatomic( locks={“[n , SX.Exclusive ]”, “[emps, PinChange.Change]”}, inverse=“emps .remove(emp) “ + “byName .remove(n); “ + “byPhone.remove(p);”) public static Employee add ( String n, String p, Float s) { Employee emp = new Employee(n, p, s); emps.add(emp); byName .put(n, emp); byPhone.put(p, emp); return emp; } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (5’) @openatomic(locks={“[n, SX.Shared]”}) public static Employee getByName (String n) { return byName.get(n); } Dagstuhl Atomicity Workshop 3/2006
Employee Using Open Nesting (6’) @openatomic( locks={“[n, XS.Exclusive]”, “if (result) “ + “[emps, PinChange.Change]”}, inverse=“if (result) { “ + “ emps .add(emp); “ + “ byName .put(emp.name , emp); “ + “ byPhone.put(emp.phone, emp); “ + “}”) public static boolean remove (String n) { Employee emp = byName.get(n); boolean result = (emp != null); if (result) { emps .remove(emp); byName .remove(emp.name ); byPhone.remove(emp.phone); } return result; } Dagstuhl Atomicity Workshop 3/2006