From 803d2c8bb9e27e4410c4016dfece3aa38fe0402e Mon Sep 17 00:00:00 2001 From: Paul Salame Date: Wed, 10 Jan 2024 13:00:27 +0100 Subject: [PATCH 1/2] optimise unessecary usleep when test ended before end of timeout --- utils/gnl.hpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/utils/gnl.hpp b/utils/gnl.hpp index cf33734..d20a31d 100644 --- a/utils/gnl.hpp +++ b/utils/gnl.hpp @@ -1,9 +1,30 @@ #ifndef GNL_HPP # define GNL_HPP # include "leaks.hpp" +# include //Don't do that at home -#define TEST(x) {int status = 0; int test = fork(); if (test == 0) {x showLeaks(); exit(EXIT_SUCCESS);} else {usleep(TIMEOUT_US); if (waitpid(test, &status, WNOHANG) == 0) {kill(test, 9); cout << FG_RED << "TIMEOUT";}}} +#define TEST(x) { \ + int status = 0; \ + int test = fork(); \ + if (test == 0) { \ + x showLeaks(); \ + exit(EXIT_SUCCESS); \ + } else { \ + int nb_sleep = 0; \ + const int usleep_time = TIMEOUT_US / 100; \ + while (waitpid(test, &status, WNOHANG) == 0 && nb_sleep * usleep_time <= TIMEOUT_US) \ + { \ + usleep(usleep_time); \ + nb_sleep++; \ + } \ + if (waitpid(test, &status, WNOHANG) == 0) \ + { \ + kill(test, 9); \ + cout << FG_RED << "TIMEOUT"; \ + } \ + } \ +} void gnl(int fd, char const * s); From cb88594b5acca44188b9f41433ff42466c7ec9fd Mon Sep 17 00:00:00 2001 From: Paul Salame Date: Wed, 10 Jan 2024 13:01:49 +0100 Subject: [PATCH 2/2] remove unecessary include --- utils/gnl.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/gnl.hpp b/utils/gnl.hpp index d20a31d..7dbb7f2 100644 --- a/utils/gnl.hpp +++ b/utils/gnl.hpp @@ -1,7 +1,6 @@ #ifndef GNL_HPP # define GNL_HPP # include "leaks.hpp" -# include //Don't do that at home #define TEST(x) { \