RFR: 8324972: (bf) Make DirectByteBuffer.Deallocator idempotent [v3]
Aleksey Shipilev
shade at openjdk.org
Thu Feb 8 10:05:56 UTC 2024
On Thu, 8 Feb 2024 09:40:02 GMT, Per Minborg <pminborg at openjdk.org> wrote:
>> src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template line 99:
>>
>>> 97: public void run() {
>>> 98: // Ensure idempotency (paranoia)
>>> 99: if (Unsafe.getUnsafe().compareAndSetInt(this, INVOKED_OFFSET, 0, 1)) {
>>
>> a question for my edification: is this compound statement suitably atomic to ensure synchronzied serial access to freeMemory block ? There's no danger of concurrent interleaved execution resulting in inconsistent view of the member variable via the reflective access ?
>
> The CAS operation establishes a happens-before relation between the CAS operation itself and any following operations (such as `freeMemory()`). Also, only one thread will succeed in changing the value from zero to one.
CAS block provides mutual exclusion here, as in, only one thread would ever execute the `freeMemory`/`unreserveMemory` block, which also does not modify any of the fields here. The only (?) thing we might be concerned with is that while one thread is running in the block, another thread would come and bypass the block. If anything relies on memory being actually freed after we exit `run()`, it might subtly misbehave -- accounting code, maybe? I guess a safer way to do this would be acquiring a lock, and checking `invoked` while holding it? Not sure if that is a problem.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/17647#discussion_r1482710683
More information about the nio-dev
mailing list