aboutsummaryrefslogtreecommitdiff
path: root/m4
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-03-22 00:06:41 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-03-22 00:07:36 +0300
commit9b2ca1e6e535e6da836b59f4c0b1ccc5001c74e1 (patch)
tree199ddedd1fd7e3fe392087d10edf62bc74be7ac3 /m4
parent0bc2fe6fe7442c4d15498e6867a1e58697e809c9 (diff)
downloadlibmicrohttpd-9b2ca1e6e535e6da836b59f4c0b1ccc5001c74e1.tar.gz
libmicrohttpd-9b2ca1e6e535e6da836b59f4c0b1ccc5001c74e1.zip
mhd_shutdown_socket_trigger.m4: used more reliable method if gettimeofday() is not available.
Fixed interpretation of very short time periods.
Diffstat (limited to 'm4')
-rw-r--r--m4/mhd_shutdown_socket_trigger.m4108
1 files changed, 74 insertions, 34 deletions
diff --git a/m4/mhd_shutdown_socket_trigger.m4 b/m4/mhd_shutdown_socket_trigger.m4
index d6dfd77a..a0a0acf1 100644
--- a/m4/mhd_shutdown_socket_trigger.m4
+++ b/m4/mhd_shutdown_socket_trigger.m4
@@ -18,7 +18,7 @@
18# and this notice are preserved. This file is offered as-is, without any 18# and this notice are preserved. This file is offered as-is, without any
19# warranty. 19# warranty.
20 20
21#serial 1 21#serial 2
22 22
23AC_DEFUN([MHD_CHECK_SOCKET_SHUTDOWN_TRIGGER],[dnl 23AC_DEFUN([MHD_CHECK_SOCKET_SHUTDOWN_TRIGGER],[dnl
24 AC_PREREQ([2.64])dnl 24 AC_PREREQ([2.64])dnl
@@ -141,11 +141,12 @@ AC_DEFUN([_MHD_RUN_CHECK_SOCKET_SHUTDOWN_TRIGGER],[dnl
141# endif 141# endif
142#endif 142#endif
143 143
144
144#ifdef HAVE_NANOSLEEP 145#ifdef HAVE_NANOSLEEP
145static const struct timespec sm_tmout = {0, 100000}; 146static const struct timespec sm_tmout = {0, 1000};
146# define short_sleep() nanosleep(&sm_tmout, NULL) 147# define short_sleep() nanosleep(&sm_tmout, NULL)
147#elif HAVE_USLEEP 148#elif defined(HAVE_USLEEP)
148# define short_sleep() usleep(100) 149# define short_sleep() usleep(1)
149#else 150#else
150# define short_sleep() (void)0 151# define short_sleep() (void)0
151#endif 152#endif
@@ -155,8 +156,24 @@ static volatile int select_ends = 0;
155static volatile int gerror = 0; 156static volatile int gerror = 0;
156static int timeout_mils; 157static int timeout_mils;
157 158
159#ifndef HAVE_GETTIMEOFDAY
160static volatile long long select_elapsed_time = 0;
161
162static long long time_chk(void)
163{
164 long long ret = time(NULL);
165 if (-1 == ret)
166 gerror = 4;
167 return ret;
168}
169#endif
170
171
158static void* select_thrd_func(void* param) 172static void* select_thrd_func(void* param)
159{ 173{
174#ifndef HAVE_GETTIMEOFDAY
175 long long start, stop;
176#endif
160 fd_set rs; 177 fd_set rs;
161 struct timeval tmot = {0, 0}; 178 struct timeval tmot = {0, 0};
162 MHD_socket fd = *((MHD_socket*)param); 179 MHD_socket fd = *((MHD_socket*)param);
@@ -164,18 +181,23 @@ static void* select_thrd_func(void* param)
164 FD_ZERO(&rs); 181 FD_ZERO(&rs);
165 FD_SET(fd, &rs); 182 FD_SET(fd, &rs);
166 tmot.tv_usec = timeout_mils * 1000; 183 tmot.tv_usec = timeout_mils * 1000;
184#ifndef HAVE_GETTIMEOFDAY
185 start = time_chk();
186#endif
167 going_select = 1; 187 going_select = 1;
168 if (0 > select ((int)(fd) + 1, &rs, NULL, NULL, &tmot)) 188 if (0 > select ((int)(fd) + 1, &rs, NULL, NULL, &tmot))
169 gerror = 1; 189 gerror = 5;
190#ifndef HAVE_GETTIMEOFDAY
191 stop = time_chk();
192 select_elapsed_time = stop - start;
193#endif
170 select_ends = 1; 194 select_ends = 1;
171 return NULL; 195 return NULL;
172} 196}
173 197
174 198
175static MHD_socket create_socket(void) 199static MHD_socket create_socket(void)
176{ 200{ return socket (AF_INET, SOCK_STREAM, 0); }
177 return socket (AF_INET, SOCK_STREAM, 0);
178}
179 201
180static void close_socket(MHD_socket fd) 202static void close_socket(MHD_socket fd)
181{ 203{
@@ -222,71 +244,89 @@ static long long test_run_select(int timeout_millsec, int use_shutdown, long lon
222#ifdef HAVE_GETTIMEOFDAY 244#ifdef HAVE_GETTIMEOFDAY
223 struct timeval start, stop; 245 struct timeval start, stop;
224#else 246#else
225 clock_t start, stop; 247 long long start;
226 if (-1 == clock())
227 return 0;
228#endif 248#endif
229 249
230 fd = create_socket_listen(0); 250 fd = create_socket_listen(0);
231 if (MHD_INVALID_SOCKET == fd) 251 if (MHD_INVALID_SOCKET == fd)
232 return 0; 252 return -7;
233 going_select = 0; 253 going_select = 0;
234 select_ends = 0; 254 select_ends = 0;
235 gerror = 0; 255 gerror = 0;
236 timeout_mils = timeout_millsec; 256 timeout_mils = timeout_millsec;
237 if (0 != pthread_create (&select_thrd, NULL, select_thrd_func, (void*)&fd)) 257 if (0 != pthread_create (&select_thrd, NULL, select_thrd_func, (void*)&fd))
238 return 0; 258 return -8;
239#ifdef HAVE_GETTIMEOFDAY
240 while (!going_select) {short_sleep();} 259 while (!going_select) {short_sleep();}
260#ifdef HAVE_GETTIMEOFDAY
241 gettimeofday (&start, NULL); 261 gettimeofday (&start, NULL);
242#else 262#else
243 while (!going_select) 263 start = time_chk();
244 { start = clock(); short_sleep(); }
245#endif 264#endif
246 if (use_shutdown) 265 if (use_shutdown)
247 { 266 {
248#ifdef HAVE_GETTIMEOFDAY 267#ifdef HAVE_GETTIMEOFDAY
249 struct timeval current; 268 struct timeval current;
250 do {gettimeofday(&current, NULL); short_sleep();} while (delay_before_shutdown > diff_time(current, start)); 269 do {short_sleep(); gettimeofday(&current, NULL); } while (delay_before_shutdown > diff_time(current, start));
251#else 270#else
252 while (delay_before_shutdown > clock() - start) {short_sleep();} 271 while (delay_before_shutdown > time_chk() - start) {short_sleep();}
253#endif 272#endif
254 shutdown(fd, SHUT_RDWR); 273 shutdown(fd, SHUT_RDWR);
255 } 274 }
256#ifdef HAVE_GETTIMEOFDAY 275#ifdef HAVE_GETTIMEOFDAY
257 while (!select_ends) {short_sleep();} 276 while (!select_ends) {short_sleep();}
258 gettimeofday (&stop, NULL); 277 gettimeofday (&stop, NULL);
259#else
260 while (!select_ends)
261 { stop = clock(); short_sleep();}
262#endif 278#endif
263 if (0 != pthread_join(select_thrd, NULL)) 279 if (0 != pthread_join(select_thrd, NULL))
264 return 0; 280 return -9;
265 close_socket(fd); 281 close_socket(fd);
266 if (gerror) 282 if (gerror)
267 return 0; 283 return -10;
284#ifdef HAVE_GETTIMEOFDAY
268 return (long long)diff_time(stop, start); 285 return (long long)diff_time(stop, start);
286#else
287 return select_elapsed_time;
288#endif
269} 289}
270 290
271static int test_it(void) 291static int test_it(void)
272{ 292{
273 clock_t duration1, duration2; 293 long long duration2;
294#ifdef HAVE_GETTIMEOFDAY
295 long long duration0, duration1;
296 duration0 = test_run_select(0, 0, 0);
297 if (0 > duration0)
298 return -duration0;
299
274 duration1 = test_run_select(50, 0, 0); 300 duration1 = test_run_select(50, 0, 0);
275 if (0 == duration1) 301 if (0 > duration1)
276 return 16; 302 return -duration1 + 20;
277 303
278 duration2 = test_run_select(500, 1, duration1 / 2); 304 duration2 = test_run_select(500, 1, (duration0 + duration1) / 2);
279 if (0 == duration2) 305 if (0 > duration2)
280 return 18; 306 return -duration2 + 40;
281 307
282 if (duration1 * 2 > duration2) 308 if (duration1 * 2 > duration2)
283 { /* Check second time to be sure. */ 309 { /* Check second time to be sure. */
284 duration2 = test_run_select(500, 1, duration1 / 2); 310 duration2 = test_run_select(500, 1, (duration0 + duration1) / 2);
285 if (0 == duration2) 311 if (0 > duration2)
286 return 20; 312 return -duration2 + 60;
287 if (duration1 * 2 > duration2) 313 if (duration1 * 2 > duration2)
288 return 0; 314 return 0;
289 } 315 }
316#else
317 duration2 = test_run_select(5000, 1, 2);
318 if (0 > duration2)
319 return -duration2 + 80;
320
321 if (4 > duration2)
322 { /* Check second time to be sure. */
323 duration2 = test_run_select(5000, 1, 2);
324 if (0 > duration2)
325 return -duration2 + 100;
326 if (4 > duration2)
327 return 0;
328 }
329#endif
290 return 1; 330 return 1;
291} 331}
292 332
@@ -316,13 +356,13 @@ int main(void)
316{ 356{
317 int res; 357 int res;
318 if (!init()) 358 if (!init())
319 return 10; 359 return 19;
320 360
321 res = test_it(); 361 res = test_it();
322 362
323 cleanup(); 363 cleanup();
324 if (gerror) 364 if (gerror)
325 return 40; 365 return gerror;
326 366
327 return res; 367 return res;
328} 368}