From: Sebastian Harl Date: Sun, 20 Oct 2013 19:56:44 +0000 (+0200) Subject: utils channel: Update timeout in channel_select when needed. X-Git-Tag: sysdb-0.1.0~336^2~49 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=dd9a22a959e81398c7d8c513d36a3e7f08b47d21;p=sysdb.git utils channel: Update timeout in channel_select when needed. --- diff --git a/src/utils/channel.c b/src/utils/channel.c index afce6f5..6db9b95 100644 --- a/src/utils/channel.c +++ b/src/utils/channel.c @@ -169,7 +169,6 @@ int sdb_channel_select(sdb_channel_t *chan, int *wantread, void *read_data, int *wantwrite, void *write_data, const struct timespec *timeout) { - struct timespec abstime; int status = 0; if (! chan) { @@ -182,14 +181,6 @@ sdb_channel_select(sdb_channel_t *chan, int *wantread, void *read_data, return -1; } - if (timeout) { - if (clock_gettime(CLOCK_REALTIME, &abstime)) - return -1; - - abstime.tv_sec += timeout->tv_sec; - abstime.tv_nsec += timeout->tv_nsec; - } - pthread_mutex_lock(&chan->lock); while (! status) { int read_status, write_status; @@ -208,9 +199,18 @@ sdb_channel_select(sdb_channel_t *chan, int *wantread, void *read_data, break; } - if (timeout) + if (timeout) { + struct timespec abstime; + + if (clock_gettime(CLOCK_REALTIME, &abstime)) + return -1; + + abstime.tv_sec += timeout->tv_sec; + abstime.tv_nsec += timeout->tv_nsec; + status = pthread_cond_timedwait(&chan->cond, &chan->lock, &abstime); + } else status = pthread_cond_wait(&chan->cond, &chan->lock); }