1a0698ed9Schristos #include "test/jemalloc_test.h" 2a0698ed9Schristos 3a0698ed9Schristos #define NSENDERS 3 4a0698ed9Schristos #define NMSGS 100000 5a0698ed9Schristos 6a0698ed9Schristos typedef struct mq_msg_s mq_msg_t; 7a0698ed9Schristos struct mq_msg_s { 8a0698ed9Schristos mq_msg(mq_msg_t) link; 9a0698ed9Schristos }; 10a0698ed9Schristos mq_gen(static, mq_, mq_t, mq_msg_t, link) 11a0698ed9Schristos 12a0698ed9Schristos TEST_BEGIN(test_mq_basic) { 13a0698ed9Schristos mq_t mq; 14a0698ed9Schristos mq_msg_t msg; 15a0698ed9Schristos 16*7bdf38e5Schristos expect_false(mq_init(&mq), "Unexpected mq_init() failure"); 17*7bdf38e5Schristos expect_u_eq(mq_count(&mq), 0, "mq should be empty"); 18*7bdf38e5Schristos expect_ptr_null(mq_tryget(&mq), 19a0698ed9Schristos "mq_tryget() should fail when the queue is empty"); 20a0698ed9Schristos 21a0698ed9Schristos mq_put(&mq, &msg); 22*7bdf38e5Schristos expect_u_eq(mq_count(&mq), 1, "mq should contain one message"); 23*7bdf38e5Schristos expect_ptr_eq(mq_tryget(&mq), &msg, "mq_tryget() should return msg"); 24a0698ed9Schristos 25a0698ed9Schristos mq_put(&mq, &msg); 26*7bdf38e5Schristos expect_ptr_eq(mq_get(&mq), &msg, "mq_get() should return msg"); 27a0698ed9Schristos 28a0698ed9Schristos mq_fini(&mq); 29a0698ed9Schristos } 30a0698ed9Schristos TEST_END 31a0698ed9Schristos 32a0698ed9Schristos static void * 33a0698ed9Schristos thd_receiver_start(void *arg) { 34a0698ed9Schristos mq_t *mq = (mq_t *)arg; 35a0698ed9Schristos unsigned i; 36a0698ed9Schristos 37a0698ed9Schristos for (i = 0; i < (NSENDERS * NMSGS); i++) { 38a0698ed9Schristos mq_msg_t *msg = mq_get(mq); 39*7bdf38e5Schristos expect_ptr_not_null(msg, "mq_get() should never return NULL"); 40a0698ed9Schristos dallocx(msg, 0); 41a0698ed9Schristos } 42a0698ed9Schristos return NULL; 43a0698ed9Schristos } 44a0698ed9Schristos 45a0698ed9Schristos static void * 46a0698ed9Schristos thd_sender_start(void *arg) { 47a0698ed9Schristos mq_t *mq = (mq_t *)arg; 48a0698ed9Schristos unsigned i; 49a0698ed9Schristos 50a0698ed9Schristos for (i = 0; i < NMSGS; i++) { 51a0698ed9Schristos mq_msg_t *msg; 52a0698ed9Schristos void *p; 53a0698ed9Schristos p = mallocx(sizeof(mq_msg_t), 0); 54*7bdf38e5Schristos expect_ptr_not_null(p, "Unexpected mallocx() failure"); 55a0698ed9Schristos msg = (mq_msg_t *)p; 56a0698ed9Schristos mq_put(mq, msg); 57a0698ed9Schristos } 58a0698ed9Schristos return NULL; 59a0698ed9Schristos } 60a0698ed9Schristos 61a0698ed9Schristos TEST_BEGIN(test_mq_threaded) { 62a0698ed9Schristos mq_t mq; 63a0698ed9Schristos thd_t receiver; 64a0698ed9Schristos thd_t senders[NSENDERS]; 65a0698ed9Schristos unsigned i; 66a0698ed9Schristos 67*7bdf38e5Schristos expect_false(mq_init(&mq), "Unexpected mq_init() failure"); 68a0698ed9Schristos 69a0698ed9Schristos thd_create(&receiver, thd_receiver_start, (void *)&mq); 70a0698ed9Schristos for (i = 0; i < NSENDERS; i++) { 71a0698ed9Schristos thd_create(&senders[i], thd_sender_start, (void *)&mq); 72a0698ed9Schristos } 73a0698ed9Schristos 74a0698ed9Schristos thd_join(receiver, NULL); 75a0698ed9Schristos for (i = 0; i < NSENDERS; i++) { 76a0698ed9Schristos thd_join(senders[i], NULL); 77a0698ed9Schristos } 78a0698ed9Schristos 79a0698ed9Schristos mq_fini(&mq); 80a0698ed9Schristos } 81a0698ed9Schristos TEST_END 82a0698ed9Schristos 83a0698ed9Schristos int 84a0698ed9Schristos main(void) { 85a0698ed9Schristos return test( 86a0698ed9Schristos test_mq_basic, 87a0698ed9Schristos test_mq_threaded); 88a0698ed9Schristos } 89a0698ed9Schristos 90