Code

utils channel: Update timeout in channel_select when needed.
authorSebastian Harl <sh@tokkee.org>
Sun, 20 Oct 2013 19:56:44 +0000 (21:56 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 20 Oct 2013 19:56:44 +0000 (21:56 +0200)
src/utils/channel.c

index afce6f5d9cc9aa883afb9569aef9a384c433eb01..6db9b9512310b8d626ea12f73e33e90a5ff3d34e 100644 (file)
@@ -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);
        }