> Hi,
> 
> This patch fixes the deadlock in `TestMemorySession#testAcquireCloseRace`. 
> The lock-step spin lock is implemented as with `lock` being an 
> `AtomicInteger`:
> 
>     // Keep the 2 threads operating on the same scope
>     int k = lock.getAndAdd(1) + 1;
>     while (k != i * 2) {
>         Thread.onSpinWait();
>         k = lock.get();
>     }
> 
> Given the initial condition:
> 
>     Thread 1: i = 0
>     Thread 2: i = 0
>     lock: -2
> 
> The `lock` then undergoes the following operations:
> 
> 
> 
>         Thread 1          Thread 2         lock value
>       getAndAdd(1)                            -1
>                         getAndAdd(1)           0 -> Thread 2 then continues 
> its next iteration, its i value is now 1
>                         getAndAdd(1)           1 -> Thread 2 reaches the next 
> iteration before thread 1 has a chance to read the value 0
>           get()                                1 -> Thread 1 now cannot 
> proceed because it missed the value 0
>                            get()               1 -> Thread 2 now cannot 
> proceed because lock can never reach 2
> 
> 
> The solution is to not rely on the exact value of the lock but instead 
> whether the lock has passed the expected value.
> 
> Testing: I have run this test several hundreds times and got no failure while 
> without this patch I encountered a timeout every approximately 30 times.
> 
> Please take a look, thanks a lot.

Quan Anh Mai has updated the pull request incrementally with one additional 
commit since the last revision:

  refactor the test

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/21976/files
  - new: https://git.openjdk.org/jdk/pull/21976/files/940b7d04..e77ce9e8

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=21976&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=21976&range=00-01

  Stats: 24 lines in 1 file changed: 10 ins; 6 del; 8 mod
  Patch: https://git.openjdk.org/jdk/pull/21976.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/21976/head:pull/21976

PR: https://git.openjdk.org/jdk/pull/21976

Reply via email to