2013-12-13 06:41:02 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NSENDERS 3
|
|
|
|
#define NMSGS 100000
|
2013-12-13 06:41:02 +08:00
|
|
|
|
|
|
|
typedef struct mq_msg_s mq_msg_t;
|
|
|
|
struct mq_msg_s {
|
|
|
|
mq_msg(mq_msg_t) link;
|
|
|
|
};
|
|
|
|
mq_gen(static, mq_, mq_t, mq_msg_t, link)
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_mq_basic) {
|
2013-12-13 06:41:02 +08:00
|
|
|
mq_t mq;
|
|
|
|
mq_msg_t msg;
|
|
|
|
|
|
|
|
assert_false(mq_init(&mq), "Unexpected mq_init() failure");
|
|
|
|
assert_u_eq(mq_count(&mq), 0, "mq should be empty");
|
|
|
|
assert_ptr_null(mq_tryget(&mq),
|
|
|
|
"mq_tryget() should fail when the queue is empty");
|
|
|
|
|
|
|
|
mq_put(&mq, &msg);
|
|
|
|
assert_u_eq(mq_count(&mq), 1, "mq should contain one message");
|
|
|
|
assert_ptr_eq(mq_tryget(&mq), &msg, "mq_tryget() should return msg");
|
|
|
|
|
|
|
|
mq_put(&mq, &msg);
|
|
|
|
assert_ptr_eq(mq_get(&mq), &msg, "mq_get() should return msg");
|
|
|
|
|
|
|
|
mq_fini(&mq);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
thd_receiver_start(void *arg) {
|
2013-12-13 06:41:02 +08:00
|
|
|
mq_t *mq = (mq_t *)arg;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < (NSENDERS * NMSGS); i++) {
|
|
|
|
mq_msg_t *msg = mq_get(mq);
|
|
|
|
assert_ptr_not_null(msg, "mq_get() should never return NULL");
|
2013-12-14 07:07:43 +08:00
|
|
|
dallocx(msg, 0);
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
thd_sender_start(void *arg) {
|
2013-12-13 06:41:02 +08:00
|
|
|
mq_t *mq = (mq_t *)arg;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < NMSGS; i++) {
|
|
|
|
mq_msg_t *msg;
|
2013-12-13 07:20:49 +08:00
|
|
|
void *p;
|
2013-12-14 07:07:43 +08:00
|
|
|
p = mallocx(sizeof(mq_msg_t), 0);
|
2014-04-15 13:32:31 +08:00
|
|
|
assert_ptr_not_null(p, "Unexpected mallocx() failure");
|
2013-12-13 07:20:49 +08:00
|
|
|
msg = (mq_msg_t *)p;
|
2013-12-13 06:41:02 +08:00
|
|
|
mq_put(mq, msg);
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_mq_threaded) {
|
2013-12-13 06:41:02 +08:00
|
|
|
mq_t mq;
|
|
|
|
thd_t receiver;
|
|
|
|
thd_t senders[NSENDERS];
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
assert_false(mq_init(&mq), "Unexpected mq_init() failure");
|
|
|
|
|
|
|
|
thd_create(&receiver, thd_receiver_start, (void *)&mq);
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < NSENDERS; i++) {
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_create(&senders[i], thd_sender_start, (void *)&mq);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2013-12-13 06:41:02 +08:00
|
|
|
|
|
|
|
thd_join(receiver, NULL);
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < NSENDERS; i++) {
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_join(senders[i], NULL);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2013-12-13 06:41:02 +08:00
|
|
|
|
|
|
|
mq_fini(&mq);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2013-12-13 06:41:02 +08:00
|
|
|
test_mq_basic,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_mq_threaded);
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
|
|
|
|