Skip to content

Commit 922885a

Browse files
jktjktmichalvasko
authored andcommitted
refactor: compat: simplify waiting
This code looked a bit fishy: - There's no need to check up front whether we're dividing a zero or non-zero number by some constant. - The conversion to milliseconds is actually not needed at all because it gets immediately converted to back to nanoseconds.
1 parent 55898de commit 922885a

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

compat/compat.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ int
3333
pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
3434
{
3535
int64_t nsec_diff;
36-
int32_t diff;
3736
struct timespec cur, dur;
3837
int rc;
3938

@@ -46,15 +45,14 @@ pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
4645
nsec_diff = 0;
4746
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
4847
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
49-
diff = (nsec_diff ? nsec_diff / 1000000L : 0);
5048

51-
if (diff < 1) {
49+
if (nsec_diff <= 0) {
5250
/* timeout */
5351
break;
54-
} else if (diff < 5) {
52+
} else if (nsec_diff < 5000000) {
5553
/* sleep until timeout */
5654
dur.tv_sec = 0;
57-
dur.tv_nsec = (long)diff * 1000000;
55+
dur.tv_nsec = nsec_diff;
5856
} else {
5957
/* sleep 5 ms */
6058
dur.tv_sec = 0;

0 commit comments

Comments
 (0)