diff --git a/Makefile.in b/Makefile.in index 02f44248..25c2d5a0 100644 --- a/Makefile.in +++ b/Makefile.in @@ -111,9 +111,9 @@ DOCS_MAN3 := $(DOCS_XML:$(objroot)%.xml=$(objroot)%.3) DOCS := $(DOCS_HTML) $(DOCS_MAN3) C_TESTLIB_SRCS := $(srcroot)test/src/btalloc.c $(srcroot)test/src/btalloc_0.c \ $(srcroot)test/src/btalloc_1.c $(srcroot)test/src/math.c \ - $(srcroot)test/src/mtx.c $(srcroot)test/src/SFMT.c \ - $(srcroot)test/src/test.c $(srcroot)test/src/thd.c \ - $(srcroot)test/src/timer.c + $(srcroot)test/src/mtx.c $(srcroot)test/src/mq.c \ + $(srcroot)test/src/SFMT.c $(srcroot)test/src/test.c \ + $(srcroot)test/src/thd.c $(srcroot)test/src/timer.c C_UTIL_INTEGRATION_SRCS := $(srcroot)src/util.c TESTS_UNIT := $(srcroot)test/unit/atomic.c \ $(srcroot)test/unit/bitmap.c \ diff --git a/test/include/test/mq.h b/test/include/test/mq.h index 11188653..7c4df493 100644 --- a/test/include/test/mq.h +++ b/test/include/test/mq.h @@ -1,3 +1,5 @@ +void mq_nanosleep(unsigned ns); + /* * Simple templated message queue implementation that relies on only mutexes for * synchronization (which reduces portability issues). Given the following @@ -75,26 +77,23 @@ a_attr a_mq_msg_type * \ a_prefix##get(a_mq_type *mq) \ { \ a_mq_msg_type *msg; \ - struct timespec timeout; \ + unsigned ns; \ \ msg = a_prefix##tryget(mq); \ if (msg != NULL) \ return (msg); \ \ - timeout.tv_sec = 0; \ - timeout.tv_nsec = 1; \ + ns = 1; \ while (true) { \ - nanosleep(&timeout, NULL); \ + mq_nanosleep(ns); \ msg = a_prefix##tryget(mq); \ if (msg != NULL) \ return (msg); \ - if (timeout.tv_sec == 0) { \ + if (ns < 1000*1000*1000) { \ /* Double sleep time, up to max 1 second. */ \ - timeout.tv_nsec <<= 1; \ - if (timeout.tv_nsec >= 1000*1000*1000) { \ - timeout.tv_sec = 1; \ - timeout.tv_nsec = 0; \ - } \ + ns <<= 1; \ + if (ns > 1000*1000*1000) \ + ns = 1000*1000*1000; \ } \ } \ } \ diff --git a/test/src/mq.c b/test/src/mq.c new file mode 100644 index 00000000..d3377245 --- /dev/null +++ b/test/src/mq.c @@ -0,0 +1,27 @@ +#include "test/jemalloc_test.h" + +/* + * Sleep for approximately ns nanoseconds. No lower *nor* upper bound on sleep + * time is guaranteed. + */ +void +mq_nanosleep(unsigned ns) +{ + + assert(ns <= 1000*1000*1000); + +#ifdef _WIN32 + Sleep(ns / 1000); +#else + struct timespec timeout; + + if (ns < 1000*1000*1000) { + timeout.tv_sec = 0; + timeout.tv_nsec = ns; + } else { + timeout.tv_sec = 1; + timeout.tv_nsec = 0; + } + nanosleep(&timeout, NULL); +#endif +}