| 1 | /* |
| 2 | * test_wfstack.c |
| 3 | * |
| 4 | * Userspace RCU library - test wftack 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 | #include <urcu/wfstack.h> |
| 30 | |
| 31 | #include "tap.h" |
| 32 | |
| 33 | #define NR_TESTS 1 |
| 34 | #define NR_PRODUCERS 4 |
| 35 | #define LOOP 100 |
| 36 | |
| 37 | static void async_run(struct cds_wfs_stack *queue) |
| 38 | { |
| 39 | struct cds_wfs_node *node = malloc(sizeof(*node)); |
| 40 | |
| 41 | cds_wfs_node_init(node); |
| 42 | |
| 43 | cds_wfs_push(queue, node); |
| 44 | } |
| 45 | |
| 46 | static void *async_loop(void *queue) |
| 47 | { |
| 48 | size_t k = 0; |
| 49 | |
| 50 | while (k < LOOP * NR_PRODUCERS) { |
| 51 | free(cds_wfs_pop_blocking(queue)); |
| 52 | ++k; |
| 53 | } |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | static void *spawn_jobs(void *queue) |
| 59 | { |
| 60 | for (size_t k = 0; k < LOOP; ++k) { |
| 61 | async_run(queue); |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int main(void) |
| 68 | { |
| 69 | pthread_t consumer; |
| 70 | pthread_t producers[NR_PRODUCERS]; |
| 71 | struct cds_wfs_stack queue; |
| 72 | |
| 73 | plan_tests(NR_TESTS); |
| 74 | |
| 75 | cds_wfs_init(&queue); |
| 76 | pthread_create(&consumer, NULL, async_loop, &queue); |
| 77 | |
| 78 | for (size_t k = 0; k < NR_PRODUCERS; ++k) { |
| 79 | pthread_create(&producers[k], NULL, spawn_jobs, &queue); |
| 80 | } |
| 81 | |
| 82 | pthread_join(consumer, NULL); |
| 83 | for (size_t k = 0; k < NR_PRODUCERS; ++k) { |
| 84 | pthread_join(producers[k], NULL); |
| 85 | } |
| 86 | |
| 87 | ok1("No race conditions"); |
| 88 | |
| 89 | return exit_status(); |
| 90 | } |