[foreign-jextract] RFR: Minor change for possible race in shared resource list
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Sat Apr 24 10:54:35 UTC 2021
On Sat, 24 Apr 2021 01:40:40 GMT, Radoslaw Smogura <github.com+7535718+rsmogura at openjdk.org> wrote:
> (1) One or more threads can see "prev" as CLOSED_LIST. Even those CAS will be performed
> and list head will be set to new cleanup. Than exception is going to be thrown.
>
> (2) Than cleanup will continue (maybe skipping just added cleanup).
>
> If in addition, other thread will join in parallel to (1) with cleanup2, than
> queue could look like this HEAD = cleanup2 -> list end marker.
To be clear, what the logic behind the code is supposed to do is this:
* read the first element of the resource list
* set that element as the next in the cleanup to be added
* keep trying replacing the first element with the new one; there are two cases here:
- the returned value from compareAndExchange will be CLOSED_LIST; in this case it means the list was already closed; no add happened (as the compareAndExchangeFailed) and an exception is thrown. We could, for completeness reset the `next` field in the cleanup action to be added before throwing.
- the returned value is the new cleanup action; this means the compare and exchange succeeded - the cleanup is added to the list.
- the returned value is something else - means that some thread has sneaked in, and added some other cleanup to the list; keep trying from the beginning.
Apart from the fact that cleanup.next is left to the `prev` value when an exception is thrown, I see no other issue - and even then, it's not clear to me what the impact of that issue is?
OK, I now get what you mean. The scope is already close. And the first element is already CLOSED_LIST. This means you can happily replace CLOSED_LIST with something else in the CAS.
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/519
More information about the panama-dev
mailing list