[jmm-dev] VarHandle.safepoint() methods

Doug Lea dl at cs.oswego.edu
Fri Jan 6 00:28:26 UTC 2017

On 01/05/2017 11:37 AM, Andrew Haley wrote:
> On 05/01/17 16:34, Doug Lea wrote:
>>> I'm not proposing any change to the MappedByteBuffer API.  What
>>> would insert the call to safePointEpoch() ?
>> Some sort of annotation? I realize that this trades off one kind of
>> messiness for another, but users would otherwise need to know to
>> use your proposed getSafePoint() method vs plain get(). I can't
>> think of anything that automatically does either of these or
>> any of Gil's variants.
> But users of an unmappable MappedByteBuffer will use exactly the
> same API as any other kind of ByteBuffer.  They won't know anything
> about safepoints. That's the whole idea.  Any solution which changes
> the ByteBuffer API isn't going to fly.

I'm not sure which APIs you mean not to touch.
It seems possible to use it in your original example.
Something like:

public class CloseableMappedByteBuffer {

     // Actually unmaps a byte buffer.
     private native void unmap(MappedByteBuffer b);

      private volatile MappedByteBuffer _buf;
+    private static final VarHandle BUF = ...
+    private long epoch = 0; // no need to init?

     private VarHandle vh;

     private MappedByteBuffer buf() {
+ 	epoch = VarHandle.safePointCheck(epoch);
+ 	return _BUF.get(this);
-        return (MappedByteBuffer) v.getSafepoint();


More information about the jmm-dev mailing list