Add mq (message queue) to test infrastructure.
Add mtx (mutex) to test infrastructure, in order to avoid bootstrapping complications that would result from directly using malloc_mutex. Rename test infrastructure's thread abstraction from je_thread to thd. Fix some header ordering issues.
This commit is contained in:
91
test/unit/mq.c
Normal file
91
test/unit/mq.c
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define NSENDERS 3
|
||||
#define NMSGS 100000
|
||||
|
||||
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)
|
||||
|
||||
TEST_BEGIN(test_mq_basic)
|
||||
{
|
||||
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 *
|
||||
thd_receiver_start(void *arg)
|
||||
{
|
||||
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");
|
||||
assert_d_eq(jet_dallocm(msg, 0), ALLOCM_SUCCESS,
|
||||
"Unexpected dallocm() failure");
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static void *
|
||||
thd_sender_start(void *arg)
|
||||
{
|
||||
mq_t *mq = (mq_t *)arg;
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < NMSGS; i++) {
|
||||
mq_msg_t *msg;
|
||||
assert_d_eq(jet_allocm((void **)&msg, NULL, sizeof(mq_msg_t),
|
||||
0), ALLOCM_SUCCESS, "Unexpected allocm() failure");
|
||||
mq_put(mq, msg);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
TEST_BEGIN(test_mq_threaded)
|
||||
{
|
||||
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);
|
||||
for (i = 0; i < NSENDERS; i++)
|
||||
thd_create(&senders[i], thd_sender_start, (void *)&mq);
|
||||
|
||||
thd_join(receiver, NULL);
|
||||
for (i = 0; i < NSENDERS; i++)
|
||||
thd_join(senders[i], NULL);
|
||||
|
||||
mq_fini(&mq);
|
||||
}
|
||||
TEST_END
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
return (test(
|
||||
test_mq_basic,
|
||||
test_mq_threaded));
|
||||
}
|
||||
|
60
test/unit/mtx.c
Normal file
60
test/unit/mtx.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#define NTHREADS 2
|
||||
#define NINCRS 2000000
|
||||
|
||||
TEST_BEGIN(test_mtx_basic)
|
||||
{
|
||||
mtx_t mtx;
|
||||
|
||||
assert_false(mtx_init(&mtx), "Unexpected mtx_init() failure");
|
||||
mtx_lock(&mtx);
|
||||
mtx_unlock(&mtx);
|
||||
mtx_fini(&mtx);
|
||||
}
|
||||
TEST_END
|
||||
|
||||
typedef struct {
|
||||
mtx_t mtx;
|
||||
unsigned x;
|
||||
} thd_start_arg_t;
|
||||
|
||||
static void *
|
||||
thd_start(void *varg)
|
||||
{
|
||||
thd_start_arg_t *arg = (thd_start_arg_t *)varg;
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < NINCRS; i++) {
|
||||
mtx_lock(&arg->mtx);
|
||||
arg->x++;
|
||||
mtx_unlock(&arg->mtx);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
TEST_BEGIN(test_mtx_race)
|
||||
{
|
||||
thd_start_arg_t arg;
|
||||
thd_t thds[NTHREADS];
|
||||
unsigned i;
|
||||
|
||||
assert_false(mtx_init(&arg.mtx), "Unexpected mtx_init() failure");
|
||||
arg.x = 0;
|
||||
for (i = 0; i < NTHREADS; i++)
|
||||
thd_create(&thds[i], thd_start, (void *)&arg);
|
||||
for (i = 0; i < NTHREADS; i++)
|
||||
thd_join(thds[i], NULL);
|
||||
assert_u_eq(arg.x, NTHREADS * NINCRS,
|
||||
"Race-related counter corruption");
|
||||
}
|
||||
TEST_END
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
|
||||
return (test(
|
||||
test_mtx_basic,
|
||||
test_mtx_race));
|
||||
}
|
@@ -22,8 +22,8 @@ malloc_tsd_externs(data, data_t)
|
||||
malloc_tsd_data(, data, data_t, DATA_INIT)
|
||||
malloc_tsd_funcs(, data, data_t, DATA_INIT, data_cleanup)
|
||||
|
||||
void *
|
||||
je_thread_start(void *arg)
|
||||
static void *
|
||||
thd_start(void *arg)
|
||||
{
|
||||
data_t d = (data_t)(uintptr_t)arg;
|
||||
assert_x_eq(*data_tsd_get(), DATA_INIT,
|
||||
@@ -37,23 +37,23 @@ je_thread_start(void *arg)
|
||||
assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
|
||||
"Resetting local data should have no effect on tsd");
|
||||
|
||||
return NULL;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
TEST_BEGIN(test_tsd_main_thread)
|
||||
{
|
||||
|
||||
je_thread_start((void *) 0xa5f3e329);
|
||||
thd_start((void *) 0xa5f3e329);
|
||||
}
|
||||
TEST_END
|
||||
|
||||
TEST_BEGIN(test_tsd_sub_thread)
|
||||
{
|
||||
je_thread_t thread;
|
||||
thd_t thd;
|
||||
|
||||
data_cleanup_executed = false;
|
||||
je_thread_create(&thread, je_thread_start, (void *) THREAD_DATA);
|
||||
je_thread_join(thread, NULL);
|
||||
thd_create(&thd, thd_start, (void *)THREAD_DATA);
|
||||
thd_join(thd, NULL);
|
||||
assert_true(data_cleanup_executed,
|
||||
"Cleanup function should have executed");
|
||||
}
|
||||
|
Reference in New Issue
Block a user