aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2021-12-12 19:12:25 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2021-12-12 20:05:11 +0300
commitb468b54f7cbe7bcc9354a9ce4cca644cd79e94ce (patch)
treeb8363c39f8a972472993a704ab55ceec184b9093
parent8f803e184ac702f31da3c457ef78da4c3b35219c (diff)
downloadlibmicrohttpd-b468b54f7cbe7bcc9354a9ce4cca644cd79e94ce.tar.gz
libmicrohttpd-b468b54f7cbe7bcc9354a9ce4cca644cd79e94ce.zip
test_timeout: avoid busy-waiting
-rw-r--r--src/testcurl/test_timeout.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/testcurl/test_timeout.c b/src/testcurl/test_timeout.c
index 8742b9ff..bcc1ad91 100644
--- a/src/testcurl/test_timeout.c
+++ b/src/testcurl/test_timeout.c
@@ -39,6 +39,47 @@
39#include <unistd.h> 39#include <unistd.h>
40#endif 40#endif
41 41
42
43/**
44 * Pause execution for specified number of milliseconds.
45 * @param ms the number of milliseconds to sleep
46 */
47void
48_MHD_sleep (uint32_t ms)
49{
50#if defined(_WIN32)
51 Sleep (ms);
52#elif defined(HAVE_NANOSLEEP)
53 struct timespec slp = {ms / 1000, (ms % 1000) * 1000000};
54 struct timespec rmn;
55 int num_retries = 0;
56 while (0 != nanosleep (&slp, &rmn))
57 {
58 if (EINTR != errno)
59 externalErrorExit ();
60 if (num_retries++ > 8)
61 break;
62 slp = rmn;
63 }
64#elif defined(HAVE_USLEEP)
65 uint64_t us = ms * 1000;
66 do
67 {
68 uint64_t this_sleep;
69 if (999999 < us)
70 this_sleep = 999999;
71 else
72 this_sleep = us;
73 /* Ignore return value as it could be void */
74 usleep (this_sleep);
75 us -= this_sleep;
76 } while (us > 0);
77#else
78 fprintf (stderr, "No sleep function available on this system.\n");
79#endif
80}
81
82
42static int oneone; 83static int oneone;
43 84
44static int withTimeout = 0; 85static int withTimeout = 0;
@@ -125,6 +166,7 @@ static size_t
125putBuffer_fail (void *stream, size_t size, size_t nmemb, void *ptr) 166putBuffer_fail (void *stream, size_t size, size_t nmemb, void *ptr)
126{ 167{
127 (void) stream; (void) size; (void) nmemb; (void) ptr; /* Unused. Silent compiler warning. */ 168 (void) stream; (void) size; (void) nmemb; (void) ptr; /* Unused. Silent compiler warning. */
169 _MHD_sleep (100); /* Avoid busy-waiting */
128 return 0; 170 return 0;
129} 171}
130 172