On Fri, 25 Oct 2024 18:36:50 GMT, Patricio Chilano Mateo 
<pchilanom...@openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/VirtualThread.java line 952:
>> 
>>> 950:         for (;;) {
>>> 951:             boolean unblocked = false;
>>> 952:             synchronized (timedWaitLock()) {
>> 
>> Where is the overall design of the timed-wait protocol and it use of 
>> synchronization described?
>
> When we unmount on a timed-wait call we schedule a wakeup task at the end of 
> `afterYield`. There are two mechanisms that avoid the scheduled task to run 
> and wake up the virtual thread on a future timed-wait call, since in this 
> call the virtual thread could have been already notified before the scheduled 
> task runs. The first one is to cancel the scheduled task once we return from 
> the wait call (see `Object.wait(long timeoutMillis)`). Since the task could 
> have been already started though, we also use `timedWaitSeqNo`, which the 
> wake up task checks here to make sure it is not an old one. Since we 
> synchronize on `timedWaitLock` to increment `timedWaitSeqNo` and change state 
> to `TIMED_WAIT` before scheduling the wake up task in `afterYield`, here 
> either a wrong `timedWaitSeqNo` or a state different than `TIMED_WAIT` means 
> there is nothing to do. The only exception is checking for `SUSPENDED` state, 
> in which case we just loop to retry.

Thanks for the explanation but that needs to be documented somewhere.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/21565#discussion_r1818228510

Reply via email to