language support for lightweight transactions
play

Language Support for Lightweight Transactions (Tim Harris & - PowerPoint PPT Presentation

Language Support for Lightweight Transactions (Tim Harris & Keir Fraser, OOPSLA03) Patrik Persson, Nov. 14, 2013 Java monitors are tricky! public synchronized int get() { int result; while (items == 0) wait();


  1. Language Support for Lightweight Transactions (Tim Harris & Keir Fraser, OOPSLA’03) � � � � Patrik Persson, Nov. 14, 2013

  2. Java monitors are tricky! public synchronized int get() { int result; while (items == 0) wait(); items --; result = buffer[items]; notifyAll(); return result; }

  3. Back to the drawing board • Conditional critical regions 
 (Tony Hoare, 1972) public int get() { atomic (items != 0) { • Atomic execution wrt. items --; return buffer[items]; other atomic sections } accessing the same } data • Where did the lock go?

  4. Software Transactional Memory (STM) • Transactional memory: 
 a memory model that checks ordering of memory accesses • Optimistic access with recovery strategies, 
 rather than conservative locking • Limited support in modern CPUs, e.g., Load-link & Store- conditional (MIPS, ARMv6, …) • Software transactional memory: 
 software-based approaches with similar semantics • Still relies on some CPU support, e.g., Compare-and-Swap

  5. Tracking versions in memory

  6. Deadlock, be gone! atomic { atomic { synchronized(a) { synchronized(b) { synchronized(b) { synchronized(a) { � � ... ... � � } } � � } } � � } }

  7. Summary • Declarative monitor-like concept, 
 based on transactional memory • They call it non-blocking , but it’s really non-locking : blocking is possible (and intended) for boolean conditions • Claim to avoid deadlock & priority inversion • Fair performance, 
 scales better than locking wrt. contention

  8. Language Support for Lightweight Transactions Exercises

  9. STM exercises (1/2) class Fifo { public Fifo(int sz) { vals = new int[this.sz = sz]; } Consider the class Fifo . public synchronized int get() Assume multiple producers, throws InterruptedException multiple consumers. { if (r == w) wait(); int result = vals[r]; r = (r + 1) % sz; 1. There is (at least one) notifyAll(); return result; concurrency-related bug } � here. How can it be public synchronized void put(int val) detected during testing? throws InterruptedException { if (r == ((w + 1) % sz)) wait(); vals[w] = val; 2. Rewrite the Fifo class w = (w + 1) % sz; notifyAll(); using atomic . How does } this solution address the private final int[] vals; bug above? private final int sz; private int r = 0; private int w = 0; // empty when r == w }

  10. STM exercises (2/2) Now consider the class NumberSequence . The method someHeavyComputation() is class NumberSequence { … computationally intensive, and public synchronized void computeNext() { may have side effects. nbrs[pos++] = someHeavyComputation(); } … 3. This is thread-safe, but public synchronized int size() { return pos; inefficient. Why? } … private int pos; 4. If atomic is used, how might private int nbrs[]; } performance be affected? Explain the significance of transactions (STM) here.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend