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