Fix empth_sleep() for argument in the past and pthreads
Calculation of sleep duration suffered integer underflow for unsigned time_t and arguments in the past. This made empth_sleep() sleep for "a few" years instead of not at all.
This commit is contained in:
parent
205206b3df
commit
9ee9eb3234
1 changed files with 4 additions and 2 deletions
|
@ -345,14 +345,16 @@ int
|
||||||
empth_sleep(time_t until)
|
empth_sleep(time_t until)
|
||||||
{
|
{
|
||||||
empth_t *ctx = pthread_getspecific(ctx_key);
|
empth_t *ctx = pthread_getspecific(ctx_key);
|
||||||
|
time_t now;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
empth_status("going to sleep %ld sec", until - time(0));
|
|
||||||
pthread_mutex_unlock(&mtx_ctxsw);
|
pthread_mutex_unlock(&mtx_ctxsw);
|
||||||
do {
|
do {
|
||||||
tv.tv_sec = until - time(NULL);
|
now = time(NULL);
|
||||||
|
tv.tv_sec = until >= now ? until - now : 0;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
|
empth_status("going to sleep %ld sec", (long)tv.tv_sec);
|
||||||
res = select(0, NULL, NULL, NULL, &tv);
|
res = select(0, NULL, NULL, NULL, &tv);
|
||||||
} while (res < 0 && !ctx->wakeup);
|
} while (res < 0 && !ctx->wakeup);
|
||||||
empth_status("sleep done. Waiting for lock");
|
empth_status("sleep done. Waiting for lock");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue