Port mq_get() to MinGW.
This commit is contained in:
parent
00632609df
commit
1b0e4abbfd
@ -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 \
|
||||
|
@ -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; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
|
27
test/src/mq.c
Normal file
27
test/src/mq.c
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user