Add VirtualThreadScheduler.currentVirtualThreadTask() default method#224
Add VirtualThreadScheduler.currentVirtualThreadTask() default method#224franz1981 wants to merge 1 commit into
Conversation
Returns the VirtualThreadTask for the current virtual thread, allowing custom scheduler implementations to read the task's attachment from within a running VT. Returns null for platform threads. Guarded with the same pattern as newThread(): throws UnsupportedOperationException when called on the built-in scheduler.
|
👋 Welcome back franz1981! A progress list of the required criteria for merging this PR into |
|
@franz1981 This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been no new commits pushed to the As you do not have Committer status in this project an existing Committer must agree to sponsor your change. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
Can you provide a summary on what you are looking for? Is this to avoid the scheduler/framework from maintaining its own map? If the scheduler has its own map then it would avoid the issues you listed. There may be merit in the prototype exposing a virtual Thread to VirtualThreadTask lookup but I think we need good reason to do that first. |
|
The attachment already keeps the scheduler context where it belongs: per-task, no shared state. The scheduler needs this in With Additionally, with this API the scheduler interface could potentially be simplified to a single |
|
I don't think currentVirtualThreadTask is the right API, can you experiment with
As you might remember, we had to split this in order to deal with some cases (that was last year). |
I think your proposal is just better! ❤️
You're right; I remember there was a precise reason. I'll search my issues and report back here once I find it. Looking at the code, I believe the split was related to recognizing per-carrier read pollers ( With
In both cases, a single Do you want me to send a new PR for |
I think the starting point need s a clear explanation as why/how this would be used. You said "VT currently has no way to read its own attachment" but something isn't right if a virtual thread is interacting with the scheduler managed attachment outside the context of the scheduler code. The custom scheduler should already have the vthread <-> task mapping for all started virtual threads so it shouldn't need this, if you see what I mean. |
|
Just to clarify — this is entirely within the scheduler code, not user code. The attachment is never accessed outside the scheduler. The concrete case: inside I tried the CHM approach: I keep a I benchmarked it on CHM loses 21% to the factory path. perfasm shows where it goes: 26% of cycles on CHM ops — nearly double the actual workload. perf c2c confirms it: 2.8x more Remote HITMs (cross-NUMA coherence) on the CHM path vs the factory path.
Benchmark code: |
Can you confirm that this only arises when using two virtual thread schedulers and not one? (by two I mean some some virtual threads are scheduled with your scheduler, others with the built-in). Asking because normally internal is the current thread is virtual thread, external otherwise. |
Yes, this is exactly that use case: I've introduced https://github.com/franz1981/Netty-VirtualThread-Scheduler#replace-built-in-scheduler-experimental in my custom scheduler as it now has a decent work-stealing implementation which makes it possible. |
|
Another idea, if possible, is to augment onStart to have two parameters—the VirtualThreadTask of the parent, and the VirtualThreadTask of the child? |
|
That could work @AlanBateman but it won't still allow to access from within the "runnable" VT, to its own scheduling context:
I have another idea which could help on this too, and would cover the other gap related per carrier pollers detection. In any case (your proposal and the draft pr I will send you soon) still cause some duplication of info which doesn't feel right (the scoped value) as:
|
That would be a better way to fit it into the current prototype API. |
|
Sorry @AlanBateman and @viktorklang-ora - my comment at #224 (comment) was actually for @viktorklang-ora ^^ So far, related the scheduling gaps:
Now, related, but not solved:
|
I have worked on this change in order to test the custom scheduler impl at https://github.com/franz1981/Netty-VirtualThread-Scheduler to verify how it will behave while fully replacing the built-in scheduler, since I've implemented go-like work stealing on it.
The relevant custom scheduler impl change which shows the (unique) benefits of this API are at franz1981/Netty-VirtualThread-Scheduler@3eae86b , which are:
no need for a non-inheritable
ScopedValueto know the currentVirtualThread's running scheduler (if any)to distinguish "internal" vs "external" submissions from
VirtualThreads created viaThread.ofVirtual.startI confirm that I make this contribution in accordance with the OpenJDK Interim AI Policy.
Progress
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/loom.git pull/224/head:pull/224$ git checkout pull/224Update a local copy of the PR:
$ git checkout pull/224$ git pull https://git.openjdk.org/loom.git pull/224/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 224View PR using the GUI difftool:
$ git pr show -t 224Using diff file
Download this PR as a diff file:
https://git.openjdk.org/loom/pull/224.diff
Using Webrev
Link to Webrev Comment