Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PR_WaitCondVar

11 views
Skip to first unread message

Sabyasachi Ruj

unread,
May 15, 2007, 8:16:43 AM5/15/07
to
Hello,
How can I determine that 'PR_WaitCondVar' returned because of timeout or
because it has received the notification?
In both cases that function returns PR_SUCCESS.

I've gone through the documentation but do not understand what it meant.

--
Regards,
Sabyasachi.

Wan-Teh Chang

unread,
May 15, 2007, 1:38:56 PM5/15/07
to dev-te...@lists.mozilla.org

It is a design decision that PR_WaitCondVar does not report a timeout.
If you care about timeout, you must call PR_IntervalNow() before and
after the PR_WaitCondVar call and see if the elapsed time is greater
than the timeout.

The rationale for the design decision is that it is hard for the
implementation to report a timeout accurately.

Wan-Teh

Wan-Teh Chang

unread,
May 15, 2007, 1:48:03 PM5/15/07
to dev-te...@lists.mozilla.org
Wan-Teh Chang wrote:
> It is a design decision that PR_WaitCondVar does not report a timeout.
> If you care about timeout, you must call PR_IntervalNow() before and
> after the PR_WaitCondVar call and see if the elapsed time is greater
> than the timeout.
>
> The rationale for the design decision is that it is hard for the
> implementation to report a timeout accurately.

I added a sentence at the end of the PR_WaitCondVar page to
clarify this, with a link to the PR_IntervalNow page:
http://developer.mozilla.org/en/docs/PR_WaitCondVar#Description

You can find sample code on the PR_IntervalNow page.

Let me know if the man page or the sample code is still not
clear.

Wan-Teh

ruj....@gmail.com

unread,
May 16, 2007, 4:03:55 AM5/16/07
to
> Wan-Teh- Hide quoted text -
>
> - Show quoted text -

Thanks for the reply.
The documentaion is clear enough now.

I have two doubts. Consider the code below.
Please read that comments in the code.

void start1 (void * arg)
{
int ret = 1;

while (1)
{
printf("\nHello this is thread: 1\n");

PR_Lock(mylock);

PRIntervalTime start = PR_IntervalNow();

PR_WaitCondVar(mycond, INTERVAL); /*Let INTERVAL is 1000 */
/* Suppose the above function returned because of recieving
signal afrer 999 milli sec*/

PRIntervalTime elapsed = PR_IntervalNow() - start;
/* The above function somehow took 1 milli sec to return*/
/* So now 'elapsed' is 1000*/

if (elapsed >= INTERVAL)
{
/* Then will it enter here? And report 'timed out'
instead of 'signaled' ?*/
printf("\nTimed Out\n");
}
else
{
printf("\nSignaled\n");
}

PR_Unlock(mylock);
}
}


Second doubt is - say the conditional variable is signaled just at
INTERVAL.
Then what will happen?

BTW:
I posted this same message half an hour ago with a MSVC project
attached for helping in testing. Till now that message did not appear.
May be the attachment was the problem. So poting this message again
without attachment.

--
Regards,
Sabyasachi.

Wan-Teh Chang

unread,
May 16, 2007, 1:03:32 PM5/16/07
to dev-te...@lists.mozilla.org
ruj....@gmail.com wrote:
>
> I have two doubts. Consider the code below.
> Please read that comments in the code.
>
> void start1 (void * arg)
> {
> int ret = 1;
>
> while (1)
> {
> printf("\nHello this is thread: 1\n");
>
> PR_Lock(mylock);
>
> PRIntervalTime start = PR_IntervalNow();
>
> PR_WaitCondVar(mycond, INTERVAL); /*Let INTERVAL is 1000 */
> /* Suppose the above function returned because of recieving
> signal afrer 999 milli sec*/
>
> PRIntervalTime elapsed = PR_IntervalNow() - start;
> /* The above function somehow took 1 milli sec to return*/
> /* So now 'elapsed' is 1000*/
>
> if (elapsed >= INTERVAL)
> {
> /* Then will it enter here? And report 'timed out'
> instead of 'signaled' ?*/
> printf("\nTimed Out\n");
> }
> else
> {
> printf("\nSignaled\n");
> }
>
> PR_Unlock(mylock);
> }
> }

The unit of PRIntervalTime is platform dependent. We call it a "tick".
A tick is not one millisecond on all platforms. So to specify a
1000 millisecond timeout in PRIntervalTime, you need to call
PR_MillisecondsToInterval(1000).

As your comments described, you will enter the "elapsed >= INTERVAL"
case and report "timed out".

Your code has a bug -- you must always call PR_WaitCondVar in a
while loop:

while (!condition_you_are_waiting_for) {
PR_WaitCondVar(...);
}

You cannot assume that when you return from PR_WaitCondVar, the
condition is true. There are three reasons why the condition
may not be true:
1. PR_WaitCondVar may return without being signaled. This is called
a spurious wakeup.
2. If there are other threads waiting on the same condition variable,
some other thread may wake up before you do, modify the shared data,
and make the condition false again. So when you wake up, the
condition may be false.
3. PR_WaitCondVar may return because of a timeout.

Note that these issues also apply to pthreads' condition
variables and Java objects' Wait and Notify/NotifyAll methods, so
you also need to wait in a while loop in pthreads and Java.

> Second doubt is - say the conditional variable is signaled just at
> INTERVAL.
> Then what will happen?

If that's the case, PR_WaitCondVar will return, and
"elapsed >= INTERVAL" will be true. Only you can
decide how you should handle this case. The wait timed
out, but the condition you're waiting for has become true.
Depending on the application, you should report a timeout
(because you waited too long) or you can go ahead and process
the data.

Wan-Teh

0 new messages