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

Virtual Thread Support: replace synchronized with ReentrantGuard #1089

Open
He-Pin opened this issue Jan 31, 2024 · 4 comments
Open

Virtual Thread Support: replace synchronized with ReentrantGuard #1089

He-Pin opened this issue Jan 31, 2024 · 4 comments
Labels

Comments

@He-Pin
Copy link
Member

He-Pin commented Jan 31, 2024

Motivation:
As there is onging work to make VT works well with monitor,not sure when , maybe in JDK 25, but we can do better to remove some synchronized block.

According to https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html#GUID-04C03FFC-066D-4857-85B9-E5A27A875AF9:

A current limitation of the implementation of virtual threads is that performing a blocking operation while inside a synchronized block or method causes the JDK's virtual thread scheduler to block a precious OS thread, whereas it wouldn't if the blocking operation were done outside of a synchronized block or method. We call that situation "pinning".

Pekko already has org.apache.pekko.util.ReentrantGuard (extends Java's ReentrantLock) which has a withGuard function.

   val lock = new ReentrantGuard()

   def fun(): T = {
     lock.withGuard {
       // ... function body returning an instance of T
     }
   }

ReentrantGuard.withGuard calls ReentrantLock lock() and waits until the lock is available and when it completes (successfully or not), it calls ReentrantLock unlock().

Running the JVM with -Djdk.tracePinnedThread=full is a useful way to be notified about pinned threads.

@He-Pin He-Pin added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 31, 2024
@pjfanning
Copy link
Contributor

I think this should be a discussion. I think we have 1 open already. An issue needs a reasonably concrete set of steps. Lazy Val's used synchronization under the hood. So finding a non synchronized alternative is a good place to start.

@He-Pin
Copy link
Member Author

He-Pin commented Jan 31, 2024

refs: vavr-io/vavr#2760 , yes I just found this.

@pjfanning
Copy link
Contributor

@He-Pin can we rework this issue to focus on replacing the use of this.synchronized with ReentrantLocks? That is a more achievable issue than 'Virtual thread friendly'. Someone would be more inclined to contribute the ReentrantLock solution.

@He-Pin
Copy link
Member Author

He-Pin commented Feb 1, 2024

@pjfanning YES, PLEASE.

@pjfanning pjfanning changed the title Feature request: Virtual thread friendly Virtual Thread Support: replace synchronized with ReentrantLock Feb 1, 2024
@pjfanning pjfanning changed the title Virtual Thread Support: replace synchronized with ReentrantLock Virtual Thread Support: replace synchronized with ReentrantGuard Feb 1, 2024
@He-Pin He-Pin added nice-to-have (low-prio) and removed good first issue Good for newcomers labels Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants