Re: Are there real use cases with the Java access modes?
Benjamin Manes via Concurrency-interest <[email protected]> Mon, 19 Jul 2021 00:56:56 -0700
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <CAGu0=MNbz3Smnah2tQoy_+Bhof=QBaooFaw9TxPU23fxsC9J-Q@mail.gmail.com> |
Another avenue to explore are usages of the older Unsafe's putOrdered and Atomic's delegate of lazySet. While these were introduced in JDK5, the naming and meaning was confusing and are not widely used. It is now explicitly acquire/release ordering in JDK9. JCTools is a good example of such usages. When reviewing Caffeine, there are more cases that I had forgotten about which use weaker ordering, such as when populating an element in an MPSC ring buffer. Similarly, it seems that LMAX disruptor is also using acquire/release for its ring buffer logic. On Sat, Jul 17, 2021 at 4:50 PM Shuyang Liu <[email protected]> wrote: > Thank you for the detailed explanation! I'll take a look at the code in > Caffine. > > > There probably are not many valid use-cases outside of the JDK, and all > others (including below) should probably be considered suspect. > This is why we are asking for help here :) We couldn't find many > interesting use cases after a basic search. Your example is a good one. > I'll look into how JDK uses it. > > Best, > Shuyang > > ------------------------------ > *From: *"Benjamin Manes" <[email protected]> > *To: *"Shuyang Liu" <[email protected]> > *Cc: *"concurrency-interest" <[email protected]> > *Sent: *Saturday, July 17, 2021 3:18:22 PM > *Subject: *Re: [concurrency-interest] Are there real use cases with the > Java access modes? > > There is one usage of acquire / release in Caffeine for a weak / soft > values cache. It looks something like the below snippet. It has the > following characteristics: > > 1. During construction, a plain set is used as the entry is not yet > visible to other threads. That can piggyback on the lock release. > 2. Reading the value can usually use a plain read to piggyback on the > map's volatile read of the entry, of which this class is the entry's value > object. > 3. When the referent is null, it may either be due to the garbage > collector or a cache.put(k,v) setting a new reference and nulling out the > old one. An acquire is used to check for a stale read. > 4. When writing, a release is used to ensure that clearing the reference > is ordered after the value is set. This way an intermediate null is not > read due to a compiler reordering. > > In general, though, most developers would prefer to use stronger orderings > than necessary as simpler to reason about with equivalent performance. > There probably are not many valid use-cases outside of the JDK, and all > others (including below) should probably be considered suspect. > > ---- > > static final VarHandle VALUE; > > Node(Object keyReference, V value, ReferenceQueue<V> referenceQueue) { > VALUE.set(this, new WeakValueReference<V>(keyReference, value, > referenceQueue)); > } > > public final V getValue() { > for (;;) { > Reference<V> ref = (Reference<V>) VALUE.get(this); > V referent = ref.get(); > if ((referent != null) || (ref == VALUE.getAcquire(this))) { > return referent; > } > } > } > > public final void setValue(V value, ReferenceQueue<V> referenceQueue) { > Reference<V> ref = (Reference<V>) VALUE.get(this); > VALUE.setRelease(this, new WeakValueReference<V>(getKeyReference(), > value, referenceQueue)); > ref.clear(); > } > > > On Sat, Jul 17, 2021 at 1:32 PM Shuyang Liu via Concurrency-interest < > [email protected]> wrote: > >> Hello, >> >> My colleagues and I have been working on a formal model for the Java >> access modes that is added since Java 9 [1]. Are there any popular use >> cases of access modes in real world applications/frameworks/libraries? We >> would like to see how our current formal model works in real examples. >> >> Thanks you, >> Shuyang Liu >> >> [1]. Using JDK 9 Memory Order Modes >> http://gee.cs.oswego.edu/dl/html/j9mm.html >> _______________________________________________ >> Concurrency-interest mailing list >> [email protected] >> http://cs.oswego.edu/mailman/listinfo/concurrency-interest >> > > _______________________________________________ Concurrency-interest mailing list [email protected] http://cs.oswego.edu/mailman/listinfo/concurrency-interest