Port mq_get() to MinGW.

This commit is contained in:
Jason Evans 2015-07-21 16:45:35 -07:00
parent 00632609df
commit 1b0e4abbfd
3 changed files with 39 additions and 13 deletions

View File

@ -111,9 +111,9 @@ DOCS_MAN3 := $(DOCS_XML:$(objroot)%.xml=$(objroot)%.3)
DOCS := $(DOCS_HTML) $(DOCS_MAN3) DOCS := $(DOCS_HTML) $(DOCS_MAN3)
C_TESTLIB_SRCS := $(srcroot)test/src/btalloc.c $(srcroot)test/src/btalloc_0.c \ 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/btalloc_1.c $(srcroot)test/src/math.c \
$(srcroot)test/src/mtx.c $(srcroot)test/src/SFMT.c \ $(srcroot)test/src/mtx.c $(srcroot)test/src/mq.c \
$(srcroot)test/src/test.c $(srcroot)test/src/thd.c \ $(srcroot)test/src/SFMT.c $(srcroot)test/src/test.c \
$(srcroot)test/src/timer.c $(srcroot)test/src/thd.c $(srcroot)test/src/timer.c
C_UTIL_INTEGRATION_SRCS := $(srcroot)src/util.c C_UTIL_INTEGRATION_SRCS := $(srcroot)src/util.c
TESTS_UNIT := $(srcroot)test/unit/atomic.c \ TESTS_UNIT := $(srcroot)test/unit/atomic.c \
$(srcroot)test/unit/bitmap.c \ $(srcroot)test/unit/bitmap.c \

View File

@ -1,3 +1,5 @@
void mq_nanosleep(unsigned ns);
/* /*
* Simple templated message queue implementation that relies on only mutexes for * Simple templated message queue implementation that relies on only mutexes for
* synchronization (which reduces portability issues). Given the following * 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_prefix##get(a_mq_type *mq) \
{ \ { \
a_mq_msg_type *msg; \ a_mq_msg_type *msg; \
struct timespec timeout; \ unsigned ns; \
\ \
msg = a_prefix##tryget(mq); \ msg = a_prefix##tryget(mq); \
if (msg != NULL) \ if (msg != NULL) \
return (msg); \ return (msg); \
\ \
timeout.tv_sec = 0; \ ns = 1; \
timeout.tv_nsec = 1; \
while (true) { \ while (true) { \
nanosleep(&timeout, NULL); \ mq_nanosleep(ns); \
msg = a_prefix##tryget(mq); \ msg = a_prefix##tryget(mq); \
if (msg != NULL) \ if (msg != NULL) \
return (msg); \ return (msg); \
if (timeout.tv_sec == 0) { \ if (ns < 1000*1000*1000) { \
/* Double sleep time, up to max 1 second. */ \ /* Double sleep time, up to max 1 second. */ \
timeout.tv_nsec <<= 1; \ ns <<= 1; \
if (timeout.tv_nsec >= 1000*1000*1000) { \ if (ns > 1000*1000*1000) \
timeout.tv_sec = 1; \ ns = 1000*1000*1000; \
timeout.tv_nsec = 0; \
} \
} \ } \
} \ } \
} \ } \

27
test/src/mq.c Normal file
View File

@ -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
}