Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JDK 21] Make synchronized code Loom-friendly #17045

Open
Tracked by #17017
wendigo opened this issue Apr 15, 2023 · 0 comments
Open
Tracked by #17017

[JDK 21] Make synchronized code Loom-friendly #17045

wendigo opened this issue Apr 15, 2023 · 0 comments

Comments

@wendigo
Copy link
Contributor

wendigo commented Apr 15, 2023

Background

According to https://openjdk.org/jeps/425:

There are two scenarios in which a virtual thread cannot be unmounted during blocking operations because it is pinned to its carrier:

  • When it executes code inside a synchronized block or method, or
  • When it executes a native method or a foreign function.

Pinning does not make an application incorrect, but it might hinder its scalability. If a virtual thread performs a blocking operation such as I/O or BlockingQueue.take() while it is pinned, then its carrier and the underlying OS thread are blocked for the duration of the operation. Frequent pinning for long durations can harm the scalability of an application by capturing carriers.

The scheduler does not compensate for pinning by expanding its parallelism. Instead, avoid frequent and long-lived pinning by revising synchronized blocks or methods that run frequently and guard potentially long I/O operations to use java.util.concurrent.locks.ReentrantLock instead. There is no need to replace synchronized blocks and methods that are used infrequently (e.g., only performed at startup) or that guard in-memory operations. As always, strive to keep locking policies simple and clear.

Other references (solutions & problems)

Discussion

I personally opt-in for the ReentrantLock usage pattern introduced by the pgjdbc team:

public final class ResourceLock 
		extends ReentrantLock 
		implements AutoCloseable
{
    @MustBeClosed
	public ResourceLock obtain() 
  	{
		lock();
	    return this;
  	}

  	@Override
  	public void close() 
	{
    	this.unlock();
  	}
}

This is then used in try-with-resource blocks like this:

	try (ResourceLock ignored = lock.obtain()) {
    		// critical section
    }

This plays nicely with existing tools/checks that we have (https://errorprone.info/bugpattern/MustBeClosedChecker)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant