Condition.awaitNanos

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Condition.awaitNanos

alarmnummer
I have a question about the Condition.awaitNanos function. The
documentation says that if a 0 or less is returned if a timeout has
occured.
"A value less than or equal to zero if the wait has timed out; "

It also says that a value less than the nanosTimeout is returned, when
it is signalled or interrupted:
"otherwise an estimate, that is strictly less than the nanosTimeout
argument, of the time still remaining when this method returned."

I have 3 questions:
-what happens if the Condition.awaitNanos is called with a value less
than or equal to zero? I guess that the function returns immediately
without any waiting.
-what happens if the Condition.awaitNanos is called with a value x,
and the the wait for signalling/interrupt also took time x. So the
remaining time is 0. 0 indicates that it received a timeout, but zero
also could be a signal to indicate that the call was succesfull.
-what happens if the Condition.awaitNanos is called with a value 1,
the wait took 0, so the remaining time is 1 (so it should be decreased
to zero so the value is less than the original timeout).

So I don`t have a clear understanding what happens with the
Condition.awaitNanos if the remaining timeout is around zero.

The reason I`m asking this is that I`m making a function that does the
same for locks:

long tryLockNanos(Lock lock, nanosTimeout){....}

And I don`t have a good understanding how I can deal with (almost)
zero timeouts.

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: Condition.awaitNanos

David Holmes-3
Peter,

The behaviour is logically as follows, given a call to awaitNanos(x):

  if (x <= 0) return x; // or any value <=0
  if (interrupted) throw InterruptedException
  long startTime = now();
  do_block(x);
  if (interrupted) throw InterruptedException
  else if (signalled) return (x-1);
  else return (x - (now() - startTime));

So to answer your questions:

> -what happens if the Condition.awaitNanos is called with a value less
> than or equal to zero? I guess that the function returns immediately
> without any waiting.

Yes it "immediately" returns a value <= 0 (though it might check
interruption first).

> -what happens if the Condition.awaitNanos is called with a value x,
> and the the wait for signalling/interrupt also took time x. So the
> remaining time is 0. 0 indicates that it received a timeout, but zero
> also could be a signal to indicate that the call was succesfull.

The time left need only be checked if there was no signal (the
implementation should know this). So it need only return x-1 to meet the
spec. (That said I think we have a bug in this area with our use of
AbstractQueuedSynchronizer as it might return a negative value after a
signal when a small timeout is used!)

> -what happens if the Condition.awaitNanos is called with a value 1,
> the wait took 0, so the remaining time is 1 (so it should be decreased
> to zero so the value is less than the original timeout).

Ultimately do_block uses some mechanism that will either ignore really small
timeouts, or else cause an immediate return. In either case the thread has
not been signalled so the timeleft is calculated. Doing all this takes far
longer than one nanosecond so you return a negative value that indicates a
timeout.

Hope that clarifies things.

David Holmes



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Condition.awaitNanos

Doug Lea
In reply to this post by alarmnummer
Peter Veentjer wrote:

> I have a question about the Condition.awaitNanos function. The
> documentation says that if a 0 or less is returned if a timeout has
> occured.
> "A value less than or equal to zero if the wait has timed out; "
>
> It also says that a value less than the nanosTimeout is returned, when
> it is signalled or interrupted:
> "otherwise an estimate, that is strictly less than the nanosTimeout
> argument, of the time still remaining when this method returned."
>

Notice that on interrupt, InterruptException is always thrown,
so no value is returned.

Also note that the "otherwise" case return value can also be <= 0.
(which is the basis of two of your other questions.)
While possibly ambiguous, for condition waits, callers will not care since
they will be checking the predicates they are looking for anyway.
The conventions here wouldn't work for something like your tryLockNanos
where you need a return value that tells you both about success
and about remaining time. I think you will need to come up with
some other way to do that.

> -what happens if the Condition.awaitNanos is called with a value less
> than or equal to zero? I guess that the function returns immediately
> without any waiting.

Yes.

> -what happens if the Condition.awaitNanos is called with a value x,
> and the the wait for signalling/interrupt also took time x. So the
> remaining time is 0. 0 indicates that it received a timeout, but zero
> also could be a signal to indicate that the call was succesfull.

Right. (Except not so for interrupts). The intent here is that
zero means "whether or not the predicate you are checking holds,
you need not call awaitNanos again if you are waiting in a loop because
the timeout has now elapsed". This information is what
most timed wait-loops want to know, which is why we return this value.

-Doug

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: Condition.awaitNanos

David Holmes-3
In reply to this post by David Holmes-3
Nope that was  totally wrong sorry.

>   else if (signalled) return (x-1);
>
> The time left need only be checked if there was no signal (the
> implementation should know this). So it need only return x-1 to meet the
> spec. (That said I think we have a bug in this area with our use of
> AbstractQueuedSynchronizer as it might return a negative value after a
> signal when a small timeout is used!)

Totally wrong. You always return the time left even if signalled - this is
precisely why this method exists: in case after being signalled you still
have to wait.

In which case, I have to agree that there is a potential problem if the
elapsed time as calculated is greater than the requested timeout. You would
get a negative value, indicating a timeout even though you didn't time out.

David Holmes

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: Condition.awaitNanos

David Holmes-3
I wrote:
> In which case, I have to agree that there is a potential problem if the
> elapsed time as calculated is greater than the requested timeout.
> You would get a negative value, indicating a timeout even though you
didn't
> time out.

As Doug indicated for Conditions this isn't a problem because you (nearly
always) recheck the condition. But as specified there is an ambiguity when
you get a negative return value.

David Holmes

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest