[Note that I present a simplified native function- treat it as pseudocode]
native void setMemory(long address, int size, byte value) {
memset(address, size, value);
}
long p = Unsafe.allocateMemory(1024);
Thread #1 | Thread #2
setMemory(p, 1024, 1); | long x = Unsafe.getLong(p);long p = Unsafe.allocateMemory(8);
setMemory(p, 8, 2);
long x = 0;
x = Unsafe.getLong(p);
assert x != 0long p1 = Unsafe.allocateMemory(8);
long p2 = Unsafe.allocateMemory(8);
Thread#1 | Thread#2
Unsafe.putLong(p1, 1); | if(Unsafe.getLongVolatile(p2) == 1)
Unsafe.putLongVolatile(p2, 1);| assert Unsafe.getLong(p1) == 1since nothing is actually defined for Unsafe
Gil,thanks for letting me know about VarHandles. I will check it.I see that you wrote:since nothing is actually defined for UnsafeWhat does it mean exactly that nothing is defined? From what I know Unsafe is being applying in specific solutions.What is a purpose of a function, for example Unsafe.putLongVolatile, since nothing is defined? Perhpaps, just remember that it is unsafe ;).
Anyway, could someone try to response to my second issue, annotated by II in the first post? It is a black hole in my head.Especially, I have a sense that my considerations are wild and potentially incorrect.
Since many people use Unsafe in "practical" ways, it is tempting to talk about definitions of behavior for it. But Usafe is a JDK-internal implementation detail, and not part of Java SE or any specification. Since the use of Unsafe is commonplace inside the JDK classes themselves, and JDK class implementations tend to be shated across JDK implementations, most JDKs tend to present "similar" internal behavior for Unsafe, but this similarity is mostly achieved by mimicking implementation, rather than by following some specification or definition.Being JDK-internal, literally nothing is actually defined about the behavior of Unsafe calls made from outside of the JDK's own trusted code, and about the behavior (or even the Class signature) that can be expected to exist in the next version or update. JDK classes themselves do not need to worry about such things because they ship with a specific implementation of the Unsafe. You can also think of it as "Unsafe in FooJDK version w.x.y.z is only 'defined' to do what the specific source code in that w.x.y.z version of FooJDK says it does."If Unsafe.putLongVolatile changed its behavior in some implementation of Java 12 such that when it was called from non-JDK-internal code on odd days of even months it did non-volatile puts at an address that is 224 bytes away from where it seems to be told to store things, and on Feb 29 on leap years it also overwrote ~/.ssh/id_rsa with "0xcafebabee was here", it would be acting well within its defined behavior. It is likely to upset people if that happened, but it would be because of not following "convention" or "tradition". Not because of some non-existent definition was not adhered to.The is certainly some common wisdom about what Unsafe methods seem to do, and much of that wisdom is anchored in actual reading of the internal implementation code in actual current JDK implementation versions, but that all that wisdom *can* evaporate at any moment with the next JDK version or update.
For your case II: Intra-thread consistency is trivially guaranteed by the JVM's compilers when calling native code: they simply avoid reordering loads and stores across calls to opaque methods (methods for which it was unable to reason about limitations on possible memory accesses). Avoiding such reordering guarantees the thread will experience program-order.
By weak memory model I meant a very weak, I didn't say that. So, I meant a model that doesn't ensures about data dependency, for example. But, it is not sane CPU ;). I just try to prove myself that JVM must be careful when a native funcion was called because of the fact it was executed directly on CPU.
De: "Gil Tene" <g...@azul.com>
À: "mechanical-sympathy" <mechanica...@googlegroups.com>
Envoyé: Jeudi 21 Juin 2018 07:12:40
Objet: Re: Sharing natively allocated memory between Java program and JNI calls.
On Wednesday, June 20, 2018 at 2:26:12 PM UTC-7, John Hening wrote:Gil,thanks for letting me know about VarHandles. I will check it.I see that you wrote:since nothing is actually defined for UnsafeWhat does it mean exactly that nothing is defined? From what I know Unsafe is being applying in specific solutions.What is a purpose of a function, for example Unsafe.putLongVolatile, since nothing is defined? Perhpaps, just remember that it is unsafe ;).Since many people use Unsafe in "practical" ways, it is tempting to talk about definitions of behavior for it. But Usafe is a JDK-internal implementation detail, and not part of Java SE or any specification. Since the use of Unsafe is commonplace inside the JDK classes themselves, and JDK class implementations tend to be shated across JDK implementations, most JDKs tend to present "similar" internal behavior for Unsafe, but this similarity is mostly achieved by mimicking implementation, rather than by following some specification or definition.Being JDK-internal, literally nothing is actually defined about the behavior of Unsafe calls made from outside of the JDK's own trusted code, and about the behavior (or even the Class signature) that can be expected to exist in the next version or update. JDK classes themselves do not need to worry about such things because they ship with a specific implementation of the Unsafe. You can also think of it as "Unsafe in FooJDK version w.x.y.z is only 'defined' to do what the specific source code in that w.x.y.z version of FooJDK says it does."If Unsafe.putLongVolatile changed its behavior in some implementation of Java 12 such that when it was called from non-JDK-internal code on odd days of even months it did non-volatile puts at an address that is 224 bytes away from where it seems to be told to store things, and on Feb 29 on leap years it also overwrote ~/.ssh/id_rsa with "0xcafebabee was here", it would be acting well within its defined behavior. It is likely to upset people if that happened, but it would be because of not following "convention" or "tradition". Not because of some non-existent definition was not adhered to.The is certainly some common wisdom about what Unsafe methods seem to do, and much of that wisdom is anchored in actual reading of the internal implementation code in actual current JDK implementation versions, but that all that wisdom *can* evaporate at any moment with the next JDK version or update.