summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.in6
-rw-r--r--test/include/test/mq.h19
-rw-r--r--test/src/mq.c27
3 files changed, 39 insertions, 13 deletions
diff --git a/Makefile.in b/Makefile.in
index 02f4424..25c2d5a 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 1118865..7c4df49 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 0000000..d337724
--- /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
+}