11

Given a code sample from Oracle docs https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/StampedLock.html

class Point {
   private double x, y;
   private final StampedLock sl = new StampedLock();

   void move(double deltaX, double deltaY) { // an exclusively locked method
     long stamp = sl.writeLock();
     try {
       x += deltaX;
       y += deltaY;
     } finally {
       sl.unlockWrite(stamp);
     }
   }

   double distanceFromOrigin() { // A read-only method
     long stamp = sl.tryOptimisticRead();
     double currentX = x, currentY = y;
     if (!sl.validate(stamp)) {
        stamp = sl.readLock();
        try {
          currentX = x;
          currentY = y;
        } finally {
           sl.unlockRead(stamp);
        }
     }
     return Math.sqrt(currentX * currentX + currentY * currentY);
   }

   void moveIfAtOrigin(double newX, double newY) { // upgrade
     // Could instead start with optimistic, not read mode
     long stamp = sl.readLock();
     try {
       while (x == 0.0 && y == 0.0) {
         long ws = sl.tryConvertToWriteLock(stamp);
         if (ws != 0L) {
           stamp = ws;
           x = newX;
           y = newY;
           break;
         }
         else {
           sl.unlockRead(stamp);
           stamp = sl.writeLock();
         }
       }
     } finally {
       sl.unlock(stamp);
     }
   }
 }

And provided that all methods of class Point might be called from different threads:

Why exactly do we not need fields x and y to be declared as volatile?

Is it guaranteed that the code executing the Point#moveIfAtOrigin method would always see the freshest changes to the x and y fields after acquiring StampedLock#readLock?

Is there any kind of memory barrier being established when we call StampedLock#writeLock, StampedLock#readLock?

Could anyone point to a quote from documentation regarding that?

4
  • 7
    Generally speaking acquiring and releasing locks does create a memory barrier, so any access to variables done inside locks (whether a StampedLock or something else) would ensure visibility.
    – Kayaman
    Aug 30, 2017 at 9:12
  • 5
    docs.oracle.com/javase/8/docs/api/index.html?java/util/… scroll down to "Memory Consistency Properties" for the guarantees and the important bit - "The methods of all classes in java.util.concurrent and its subpackages extend these guarantees to higher-level synchronization. In particular: [...]"
    – pvg
    Aug 30, 2017 at 9:52
  • @pvg Thanks for citing the docs, it should do for a formal proof. Aug 30, 2017 at 16:01
  • 5
    @DmitryGorbunov there's a bit of a meta-answer to this since there are many variants of this question on SO, all slightly different, involving different classes of java.util.concurrent - the full set of guarantees and references to the JMM/JLS are lengthy and they're not repeated in the docs of every class let alone every method but they are outlined in the package (and subpackage docs). So if you run across something like this, that's the first (not-entirely-obvious) place to check.
    – pvg
    Aug 30, 2017 at 16:06

2 Answers 2

2

I can't tell why that is not explicitly cited in the doc - may be because it is sort of implied, but internally that does a Unsafe.compareAndSwapLong which translates to LOCK CMPXCHG, which on x86 has full memory barrier (I assume something like this is done on other platforms); so there is no need for those to be volatile indeed.

Actually any instruction on x86 that has a lock will have a full memory barrier.

2
  • 2
    It is explicitly cited in the doc.
    – pvg
    Aug 30, 2017 at 9:53
  • @Eugene That should do for an explanation. Updated your answer to include citation from the page referenced by pvg. Thanks for your help! Aug 30, 2017 at 16:05
2

The Javadoc of the Lock interface states the following:

Memory Synchronization

All Lock implementations must enforce the same memory synchronization semantics as provided by the built-in monitor lock, as described in The Java Language Specification (17.4 Memory Model):

A successful lock operation has the same memory synchronization effects as a successful Lock action. A successful unlock operation has the same memory synchronization effects as a successful Unlock action. Unsuccessful locking and unlocking operations, and reentrant locking/unlocking operations, do not require any memory synchronization effects.

Even though StampedLock does not implement Lock, it has a method like asReadLock() that:

Returns a plain Lock view of this StampedLock in which the Lock.lock() method is mapped to readLock(), and similarly for other methods.

It returns an instance of StampedLock's inner class ReadLockView, which IS an actual implementation of Lock.

But because it's merely a delegator, that means the original methods must create memory barriers in order to comply with the Memory Synchronization enforcement of the Lock interface.

1
  • That wouldn't do for an answer, yet it's a correct and easily understandable conclusion. I'm not sure why somebody downvoted your post. It definitely adds value. Upvoted it. Aug 30, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.