Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize unnecessary usleep when test ended before end of timeout #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion utils/gnl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@
# include "leaks.hpp"

//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);

Expand Down