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