tests: Add tests for checking race conditions
[urcu.git] / tests / unit / test_wfqueue.c
CommitLineData
59fdcc3a
OD
1/*
2 * test_wfqueue.c
3 *
4 * Userspace RCU library - test wfqueue race conditions
5 *
6 * Copyright 2023 - Olivier Dion <odion@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#define _LGPL_SOURCE
24
25#include <stdlib.h>
26
27#include <pthread.h>
28
29#define CDS_WFQ_DEPRECATED
30#include <urcu/wfqueue.h>
31
32#include "tap.h"
33
34#define NR_TESTS 1
35#define NR_PRODUCERS 4
36#define LOOP 100
37
38static void async_run(struct cds_wfq_queue *queue)
39{
40 struct cds_wfq_node *node = malloc(sizeof(*node));
41
42 cds_wfq_node_init(node);
43
44 cds_wfq_enqueue(queue, node);
45}
46
47static void *async_loop(void *queue)
48{
49 size_t k = 0;
50
51 while (k < LOOP * NR_PRODUCERS) {
52 free(cds_wfq_dequeue_blocking(queue));
53 ++k;
54 }
55
56 return NULL;
57}
58
59static void *spawn_jobs(void *queue)
60{
61 for (size_t k = 0; k < LOOP; ++k) {
62 async_run(queue);
63 }
64
65 return 0;
66}
67
68int main(void)
69{
70 pthread_t consumer;
71 pthread_t producers[NR_PRODUCERS];
72 struct cds_wfq_queue queue;
73
74 plan_tests(NR_TESTS);
75
76 cds_wfq_init(&queue);
77 pthread_create(&consumer, NULL, async_loop, &queue);
78
79 for (size_t k = 0; k < NR_PRODUCERS; ++k) {
80 pthread_create(&producers[k], NULL, spawn_jobs, &queue);
81 }
82
83 pthread_join(consumer, NULL);
84 for (size_t k = 0; k < NR_PRODUCERS; ++k) {
85 pthread_join(producers[k], NULL);
86 }
87
88 ok1("No race conditions");
89
90 return exit_status();
91}
This page took 0.025492 seconds and 4 git commands to generate.