Multiverse: STM Peter Veentjer Founder of Multiverse  Software Transactional Memory http://multiverse.googlecode.com
Who am I Java Developer currently working for Qiy.
8 Years Professional Experience
Projects I worked on (and I'm proud of) Expert systems / Prolog compiler/interpreters
Batch processing systems
Enterprisy backend systems Main intererrests Concurrency control (Java, Database, STM)
Distributed computing
Software architecture
Patterns
Don't wait with your questions! We still know the context
There is enough time
I like the discussion
Agenda What is wrong with classic concurrency control?
Software Transactional Memory (Multiverse) as alternative concurrency control implementation
What is wrong? Operations not composable
public class Account{ private int balance; public synchronized int getBalance(){ Return balance; } public synchronized void setBalance(int newBalance){ this.balance = newBalance; } public inc(int delta){ setBalance(getBalance()+delta); } } Not Composable
What is wrong? Operations not composable
Expose Implementation details
public class Account{ private int balance; public synchronized int getBalance(){ Return balance; } public synchronized inc(int delta){ this.balance += balance; } } static void transfer(Account from, Account to, int amount){ synchronized(from){ synchronized(to){ from.inc(-amount); to.inc(amount); } } } Expose implementation details
public class Account{ private int balance; private Lock lock = new ReentrantLock(); public int getBalance(){ ... } public inc(int delta){ lock.acquire() try{ balance+=delta; }finally{ lock.release() } } } void transfer(Account from, Account to, int amount{ synchronized(from){ synchronized(to){ from.inc(-amount); to.inc(amount); } } } Expose implementation details
What is wrong? Operations not composable
Expose Implementation details
Deadlock prone
static void transfer(Account from, Account to, int amount){ synchronized(from){ synchronized(to){ from.inc(-amount); to.inc(amount); } } } Deadlocks
What is wrong? Operations not composable
Expose Implementation details
Deadlock prone
Operation is not atomic
public class Account{ private int balance; public int getBalance(){ Return balance; } public inc(int delta){ if(balance + delta<0) Throw new NotEnoughCashException() this.balance += balance; } } static void transfer(Account from, Account to, int amount){ to.inc(amount); from.inc(-amount); } Not atomic
Questions? It this the right way to deal with the increasing number of cores?
Could concurrency control be just as easy as gc?
STM: What? Concurrency control through database like transactions on (Java) Memory (failure) Atomicity
Consistency (task of the Java Objects)
Isolation
Durability (not provided)
Multiverse: What? STM Framework for the JVM Main engine is Alpha STM Engine: TL2 Based Working on it for more than 1 year
Multiple Programming Models POJO based icw Annotations Object granularity
Instrumentation (very time consuming!!)
@AtomicObject public class Account{ private int balance; public int getBalance(){ Return balance; } public void inc(int delta){ this.balance += balance; } } @AtomicMethod static void transfer(Account from, Account to, int amount){ from.inc(-amount); to.inc(amount); } Using STM: POJO based
@AtomicObject public class Account{ private int balance; @Exclude private int ignore; public int getBalance(){ Return balance; } public void inc(int delta){ this.balance += balance; } } Excluding fields
Multiverse: What? STM Framework for the JVM Main engine is Alpha STM Engine: TL2 Based Working on it for more than 1 year
Multiple Programming Models POJO based icw Annotations Object granularity
Instrumentation (very time consuming!!) Managed reference based Field granularity
@AtomicObject public class Account{ final Ref<Integer> balance = new Ref<Integer>(); public int getBalance(){ Return balance.get(); } public void inc(int delta){ balance.set(balance.get()+delta); } } @AtomicMethod static void transfer(Account from, Account to, int amount){ from.inc(-amount); to.inc(amount); } Using STM : Managed Ref based
Multiverse: What? STM Framework for the JVM Main engine is Alpha STM Engine: TL2 Based Working on it for more than 1 year
Multiple Programming Models POJO based icw Annotations Object granularity
Instrumentation (very time consuming!!) Managed reference based Field granularity Akka Project of Jonas Boner
Multiverse: How? Seperate State from Object identity Atomic Object (Object identity)
Tranlocal (State)
@AtomicObject public class Account{ private int balance; public int getBalance(){ Return balance; } public inc(int delta){ this.balance += balance; } } Multiverse: How
@AtomicObject public class Account implements AtomicObject{ AtomicReference<Account__Tranlocal> current =  New AtomicReference(); public int getBalance(){ Account__Tranlocal a = getTransaction().load(this); return a.balance; } public void inc(int delta){ Account__Tranlocal a = getTransaction().load(this); a.balance+=delta; } public Account__Tranlocal load(long version){..} public boolean lock(Transaction t){..} public void store(Account__Tranlocal tranlocal){...} ... } public class Account__Tranlocal{ long version; Account atomicObject; int balance; } Multiverse: How

Software Transactioneel Geheugen