Skip to content

Commit 1f8a78d

Browse files
authored
posix iwasm: Make the timeout logic a bit more robust (#3478)
Use an absolute timeout calculation to avoid being affected much by CPU scheduling.
1 parent 115a476 commit 1f8a78d

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

  • product-mini/platforms/posix

product-mini/platforms/posix/main.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -535,21 +535,23 @@ void *
535535
timeout_thread(void *vp)
536536
{
537537
const struct timeout_arg *arg = vp;
538-
uint32 left = arg->timeout_ms;
538+
const uint64 end_time =
539+
os_time_get_boot_us() + (uint64)arg->timeout_ms * 1000;
539540
while (!arg->cancel) {
540-
uint32 ms;
541-
if (left >= 100) {
542-
ms = 100;
543-
}
544-
else {
545-
ms = left;
546-
}
547-
os_usleep((uint64)ms * 1000);
548-
left -= ms;
549-
if (left == 0) {
541+
const uint64 now = os_time_get_boot_us();
542+
if ((int64)(now - end_time) > 0) {
550543
wasm_runtime_terminate(arg->inst);
551544
break;
552545
}
546+
const uint64 left_us = end_time - now;
547+
uint32 us;
548+
if (left_us >= 100 * 1000) {
549+
us = 100 * 1000;
550+
}
551+
else {
552+
us = left_us;
553+
}
554+
os_usleep(us);
553555
}
554556
return NULL;
555557
}

0 commit comments

Comments
 (0)