Skip to content

Commit 75925a7

Browse files
committed
Cleanup: Review: Fix timeout
1 parent 9dfe1e2 commit 75925a7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/rtapi/uspace_rtapi_main.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ static int get_fifo_path_to_addr(struct sockaddr_un *addr) {
483483
return len;
484484
}
485485

486+
static double diff_timespec(const struct timespec *time1, const struct timespec *time0) {
487+
return (time1->tv_sec - time0->tv_sec) + (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
488+
}
489+
486490
int main(int argc, char **argv) {
487491
if (getuid() == 0) {
488492
char *fallback_uid_str = getenv("RTAPI_UID");
@@ -555,17 +559,17 @@ int main(int argc, char **argv) {
555559
result = master(fd, args);
556560
return result;
557561
} else if (errno == EADDRINUSE) {
558-
struct timeval t0, t1;
559-
gettimeofday(&t0, NULL);
560-
gettimeofday(&t1, NULL);
561-
for (int i = 0; i < 3 || (t1.tv_sec < 3 + t0.tv_sec); i++) {
562+
struct timespec start, now;
563+
clock_gettime(CLOCK_MONOTONIC, &start);
564+
clock_gettime(CLOCK_MONOTONIC, &now);
565+
srand48(start.tv_sec ^ start.tv_nsec);
566+
while(diff_timespec(&now, &start) < 3.0) {
562567
result = connect(fd, (sockaddr *)&addr, sizeof(addr));
563568
if (result == 0)
564569
break;
565-
if (i == 0)
566-
srand48(t0.tv_sec ^ t0.tv_usec);
567-
usleep(lrand48() % 100000);
568-
gettimeofday(&t1, NULL);
570+
571+
usleep(lrand48() % 100000 + 100); //Random sleep min 100us max 100100us
572+
clock_gettime(CLOCK_MONOTONIC, &now);
569573
}
570574
if (result < 0 && errno == ECONNREFUSED) {
571575
fprintf(stderr, "Waited 3 seconds for master. giving up.\n");

0 commit comments

Comments
 (0)