Skip to content

Commit

Permalink
Replace timed waits by normal waits in threadqueue
Browse files Browse the repository at this point in the history
Replaces calls to pthread_cond_timedwait with pthread_cond_wait in
threadqueue.c. Simplifies code, as there should be no need for the
timeout.
  • Loading branch information
aryla committed Feb 11, 2017
1 parent fd05749 commit a5a925f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 58 deletions.
21 changes: 2 additions & 19 deletions src/threadqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,17 +458,8 @@ int kvz_threadqueue_flush(threadqueue_queue_t * const threadqueue) {
notdone = threadqueue->queue_waiting_execution + threadqueue->queue_waiting_dependency + threadqueue->queue_running;

if (notdone > 0) {
int ret;
PTHREAD_COND_BROADCAST(&(threadqueue->cond));

struct timespec wait_moment;
ms_from_now_timespec(&wait_moment, 100);
ret = pthread_cond_timedwait(&threadqueue->cb_cond, &threadqueue->lock, &wait_moment);
if (ret != 0 && ret != ETIMEDOUT) {
fprintf(stderr, "pthread_cond_timedwait failed!\n");
assert(0);
return 0;
}
PTHREAD_COND_WAIT(&threadqueue->cb_cond, &threadqueue->lock);
}
} while (notdone > 0);

Expand Down Expand Up @@ -496,16 +487,8 @@ int kvz_threadqueue_waitfor(threadqueue_queue_t * const threadqueue, threadqueue
PTHREAD_UNLOCK(&job->lock);

if (!job_done) {
int ret;
PTHREAD_COND_BROADCAST(&(threadqueue->cond));
struct timespec wait_moment;
ms_from_now_timespec(&wait_moment, 100);
ret = pthread_cond_timedwait(&threadqueue->cb_cond, &threadqueue->lock, &wait_moment);
if (ret != 0 && ret != ETIMEDOUT) {
fprintf(stderr, "pthread_cond_timedwait failed!\n");
assert(0);
return 0;
}
PTHREAD_COND_WAIT(&threadqueue->cb_cond, &threadqueue->lock);
}
} while (!job_done);

Expand Down
39 changes: 0 additions & 39 deletions src/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,6 @@
#define KVZ_CLOCK_T_AS_DOUBLE(ts) ((double)((ts).tv_sec) + (double)((ts).tv_nsec) / 1e9)
#define KVZ_CLOCK_T_DIFF(start, stop) ((double)((stop).tv_sec - (start).tv_sec) + (double)((stop).tv_nsec - (start).tv_nsec) / 1e9)

static INLINE struct timespec * ms_from_now_timespec(struct timespec * result, int wait_ms)
{
KVZ_GET_TIME(result);
int64_t secs = result->tv_sec + wait_ms / E3;
int64_t nsecs = result->tv_nsec + (wait_ms % E3) * (E9 / E3);

if (nsecs >= E9) {
secs += 1;
nsecs -= E9;
}

result->tv_sec = secs;
result->tv_nsec = nsecs;

return result;
}

#define KVZ_ATOMIC_INC(ptr) __sync_add_and_fetch((volatile int32_t*)ptr, 1)
#define KVZ_ATOMIC_DEC(ptr) __sync_add_and_fetch((volatile int32_t*)ptr, -1)

Expand All @@ -88,28 +71,6 @@ static INLINE struct timespec * ms_from_now_timespec(struct timespec * result, i
#define KVZ_CLOCK_T_DIFF(start, stop) ((double)((((uint64_t)(stop).dwHighDateTime)<<32 | (uint64_t)(stop).dwLowDateTime) - \
(((uint64_t)(start).dwHighDateTime)<<32 | (uint64_t)(start).dwLowDateTime)) / 1e7)

static INLINE struct timespec * ms_from_now_timespec(struct timespec * result, int wait_ms)
{
KVZ_CLOCK_T now;
KVZ_GET_TIME(&now);

int64_t moment_100ns = (int64_t)now.dwHighDateTime << 32 | (int64_t)now.dwLowDateTime;
moment_100ns -= (int64_t)FILETIME_TO_EPOCH;

int64_t secs = moment_100ns / (E9 / 100) + (wait_ms / E3);
int64_t nsecs = (moment_100ns % (E9 / 100))*100 + ((wait_ms % E3) * (E9 / E3));

if (nsecs >= E9) {
secs += 1;
nsecs -= E9;
}

result->tv_sec = secs;
result->tv_nsec = nsecs;

return result;
}

#define KVZ_ATOMIC_INC(ptr) InterlockedIncrement((volatile LONG*)ptr)
#define KVZ_ATOMIC_DEC(ptr) InterlockedDecrement((volatile LONG*)ptr)

Expand Down

0 comments on commit a5a925f

Please sign in to comment.