JAVA MEMORY MODEL AND ITS IMPLICATIONS Srikanth Seshadri

Slides:



Advertisements
Similar presentations
1 Chapter 4 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
Advertisements

Architecture-aware Analysis of Concurrent Software Rajeev Alur University of Pennsylvania Amir Pnueli Memorial Symposium New York University, May 2010.
Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock- Free Objects” Presentation Robert T. Bauer.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
Lecture 6-2 : Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Week 9, Class 3: Model-View-Controller Final Project Worth 2 labs Happens-Before ( SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Concurrent Data Structures in Architectures with Limited Shared Memory Support Ivan Walulya Yiannis Nikolakopoulos Marina Papatriantafilou Philippas Tsigas.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Atomic Instructions P&H Chapter 2.11.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
1 Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
1 Lecture 23: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
Simple, Fast, and Practical Non- Blocking and Blocking Concurrent Queue Algorithms Presenter: Jim Santmyer By: Maged M. Micheal Michael L. Scott Department.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking =
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Simple, Fast and Practical Non- Blocking and Blocking Concurrent Queue Algorithms Maged M. Michael & Michael L. Scott Presented by Ahmed Badran.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Josephus Problem: Build the Circular Linked List
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
CS510 Concurrent Systems Introduction to Concurrency.
Threads some important concepts Simon Lynch
November 15, 2007 A Java Implementation of a Lock- Free Concurrent Priority Queue Bart Verzijlenberg.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Maged M.Michael Michael L.Scott Department of Computer Science Univeristy of Rochester Presented by: Jun Miao.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Java Thread and Memory Model
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Advanced Locking Techniques
CS510 Concurrent Systems Jonathan Walpole. A Methodology for Implementing Highly Concurrent Data Objects.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Gal Milman Based on Chapter 10 (Concurrent Queues and the ABA Problem) in The Art of Multiprocessor Programming by Herlihy and Shavit Seminar 2 (236802)
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy Slides by Vincent Rayappa.
1 1 Nastaran Shafiei VERIFICATION OF A NON-BLOCKING ARRAY-BASED QUEUE ALGORITHM.
Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING,
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Slides on threads borrowed by Chase Landon Cox. Thread states BlockedReady Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock.
Week 8, Class 3: Model-View-Controller Final Project Worth 2 labs Cleanup of Ducks Reducing coupling Finishing FactoryMethod Cleanup of Singleton SE-2811.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Concurrency 2 CS 2110 – Spring 2016.
Checking nonblocking concurrent queue program
Department of Computer Science, University of Rochester
Atomicity CS 2110 – Fall 2017.
Priority Queue.
Anders Gidenstam Håkan Sundell Philippas Tsigas
Basic Data Types Queues
null, true, and false are also reserved.
Data Structures ADT List
Data Structures ADT List
Lock-Free concurrent algorithm for Linked lists: Implementation
The C++ Memory model Implementing synchronization)
Lecture 6: Transactions
ADT list.
Part 1: Concepts and Hardware- Based Approaches
IMPLEMENTATION OF A NON-BLOCKING QUEUE ALGORITHM
CSCI1600: Embedded and Real Time Software
Slides by Prof. Landon Cox and Vamsi Thummala
Queues CSC212.
Data Structures ADT List
Multicore programming
CS510 Advanced Topics in Concurrency
CSCI1600: Embedded and Real Time Software
CS510 Concurrent Systems Jonathan Walpole.
Problems with Locks Andrew Whitaker CSE451.
CPS110: Thread cooperation
Presentation transcript:

JAVA MEMORY MODEL AND ITS IMPLICATIONS Srikanth Seshadri

JAVA MEMORY MODEL public class NoVisibility { private static boolean ready; private static int number; private static class ReaderThread extends Thread { public void run() { while (!ready) Thread.yield(); System.out.println(number); } public static void main(String[] args) { new ReaderThread().start(); number = 42; ready = true; }

DOUBLE CHECKED LOCKING if(!ready){ synchronized(lock){ if(!ready){ //create and initialize singleton ready=true; } if(!ready){ synchronized(lock){ if(!ready){ //create and initialize singleton ready=true; } The "Double-Checked Locking is Broken" Declaration The "Double-Checked Locking is Broken" Declaration

HAPPEN-BEFORE To guarantee that the thread executing action B can see the results of action A there must be happens-before relationship between A and B. 1.Program order rule. Each action in a thread happens-before every action in that thread that comes later in the program order. 2.Monitor lock rule. An unlock on a monitor lock happens- before every subsequent lock on that same monitor lock. 3.Volatile variable rule. A write to a volatile field happens-before every subsequent read of that same field.

VOLATILE volatile ready=true; If(ready) Initialize all the data Skip initialization Thread-1 Thread-2 if(!ready){ synchronized(lock){ if(!ready){ //create and initialize singleton ready=true; }

MEMORY BARRIERS - PAUL E. MCKENNEY Memory Barriers: a Hardware View For Software Hackers Memory Barriers: a Hardware View For Software Hackers Memory Ordering in Modern Microprocessors Memory Ordering in Modern Microprocessors

CACHE COHERENCE PROTOCOL

MEMORY BARRIERS AMD – lfence/sfence/mfence Intel – lock addl Volatile – Status Flag – Read-Write Lock Trick Managing Volatility Managing Volatility

READ-WRITE LOCK TRICK public class Counter { private volatile int value; public int getValue() { return value; } public synchronized int increment() { return value++; } }

LL - SC Load Linked (LL) And Store Conditional (SC) –DEC Alpha - ldl_l/stl_c –Power PC - lwarx/stwcx –MIPS –ll/sc –Intel- lock cmpxchg Non-Block Alogorithms –Lock-Free –Wait-Free

NON-BLOCKING QUEUE structure node_t {value: data type, next: pointer} structure queue_t {Head: pointer, Tail: pointer} initialize(Q: pointer to queue_t) node = new_node() node->next = NULL Q->Head = Q->Tail = node A B C D Head Tail -

NON-BLOCKING ENQUEUE enqueue(Q: pointer to queue_t, value: data type) E1: node = new_node() E2: node->value = value E3: node->next = NULL E4: loop E5: tail = Q->Tail E6: next = tail->next E7: if tail == Q->Tail// Are tail and next consistent? E8: if next == NULL E9: if CAS(&tail->next, next, node) E10: break// Enqueue is done. Exit loop E11: endif E12: else // Try to swing Tail to the next node E13: CAS(&Q->Tail, tail, next) E14: endif E15: endif E16: endloop // Enqueue is done. Try to swing Tail to the inserted node E17: CAS(&Q->Tail, tail, node)

NON BLOCKING DEQUE dequeue(Q: pointer to queue_t, pvalue: pointer to data type): boolean D1: loop D2: head = Q->Head D3: tail = Q->Tail D4: next = head->next D5: if head == Q->Head // Are head, tail, and next consistent? D6: if head == tail // Is queue empty or Tail falling behind? D7: if next == NULL // Is queue empty? D8: return FALSE // Queue is empty, couldn't dequeue D9: endif // Tail is falling behind. Try to advance it D10: CAS(&Q->Tail, tail, next) D11: else // No need to deal with Tail // Read value before CAS // Otherwise, another dequeue might free the next node D12: *pvalue = next->value // Try to swing Head to the next node D13: if CAS(&Q->Head, head, next) D14: break // Dequeue is done. Exit loop D15: endif D16: endif D17: endif D18: endloop D19: free(head)

ATOMICS j.u.c atomic Operations –get –set –lazySet –compareAndSet –weakCompareAndSet ABA Problem

INTERESTED IN CONCURRENCY Follow –Doug Lea –Brian Goetz Concurrency Interest Forums

REFERENCES References –Java Memory Model –Double Checked Locking –Hotspot –Memory Barriers &type=pdfhttp://citeseerx.ist.psu.edu/viewdoc/download?doi= &rep=rep1 &type=pdf / pdf&sa=X&ei=gTdeTOG1Esmwcf6jlNoO&ved=0CBkQzgQoADAA&usg=AFQjCNH4oEO TrvSbSltVaQequTdhmxD-pQhttp:// / pdf&sa=X&ei=gTdeTOG1Esmwcf6jlNoO&ved=0CBkQzgQoADAA&usg=AFQjCNH4oEO TrvSbSltVaQequTdhmxD-pQ –Atomics

QUESTIONS