| 1 | // SPDX-FileCopyrightText: 2023 Olivier Dion <odion@efficios.com> |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
| 5 | /* |
| 6 | * test_wfqueue.c |
| 7 | * |
| 8 | * Userspace RCU library - test wfqueue race conditions |
| 9 | */ |
| 10 | |
| 11 | #define _LGPL_SOURCE |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #include <pthread.h> |
| 16 | |
| 17 | #define CDS_WFQ_DEPRECATED |
| 18 | #include <urcu/wfqueue.h> |
| 19 | |
| 20 | #include "tap.h" |
| 21 | |
| 22 | #define NR_TESTS 1 |
| 23 | #define NR_PRODUCERS 4 |
| 24 | #define LOOP 100 |
| 25 | |
| 26 | static void async_run(struct cds_wfq_queue *queue) |
| 27 | { |
| 28 | struct cds_wfq_node *node = malloc(sizeof(*node)); |
| 29 | |
| 30 | cds_wfq_node_init(node); |
| 31 | |
| 32 | cds_wfq_enqueue(queue, node); |
| 33 | } |
| 34 | |
| 35 | static void *async_loop(void *queue) |
| 36 | { |
| 37 | size_t k = 0; |
| 38 | |
| 39 | while (k < LOOP * NR_PRODUCERS) { |
| 40 | free(cds_wfq_dequeue_blocking(queue)); |
| 41 | ++k; |
| 42 | } |
| 43 | |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | static void *spawn_jobs(void *queue) |
| 48 | { |
| 49 | for (size_t k = 0; k < LOOP; ++k) { |
| 50 | async_run(queue); |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | int main(void) |
| 57 | { |
| 58 | pthread_t consumer; |
| 59 | pthread_t producers[NR_PRODUCERS]; |
| 60 | struct cds_wfq_queue queue; |
| 61 | |
| 62 | plan_tests(NR_TESTS); |
| 63 | |
| 64 | cds_wfq_init(&queue); |
| 65 | pthread_create(&consumer, NULL, async_loop, &queue); |
| 66 | |
| 67 | for (size_t k = 0; k < NR_PRODUCERS; ++k) { |
| 68 | pthread_create(&producers[k], NULL, spawn_jobs, &queue); |
| 69 | } |
| 70 | |
| 71 | pthread_join(consumer, NULL); |
| 72 | for (size_t k = 0; k < NR_PRODUCERS; ++k) { |
| 73 | pthread_join(producers[k], NULL); |
| 74 | } |
| 75 | |
| 76 | ok1("No race conditions"); |
| 77 | |
| 78 | return exit_status(); |
| 79 | } |