t_scan.c revision 1.1 1 1.1 thorpej /* $NetBSD: t_scan.c,v 1.1 2021/10/10 17:47:39 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1 thorpej */
28 1.1 thorpej
29 1.1 thorpej #include <sys/cdefs.h>
30 1.1 thorpej __RCSID("$NetBSD: t_scan.c,v 1.1 2021/10/10 17:47:39 thorpej Exp $");
31 1.1 thorpej
32 1.1 thorpej #include <sys/event.h>
33 1.1 thorpej #include <sys/time.h>
34 1.1 thorpej #include <sys/types.h>
35 1.1 thorpej
36 1.1 thorpej #include <err.h>
37 1.1 thorpej #include <pthread.h>
38 1.1 thorpej #include <stdio.h>
39 1.1 thorpej #include <stdlib.h>
40 1.1 thorpej #include <unistd.h>
41 1.1 thorpej
42 1.1 thorpej #include <atf-c.h>
43 1.1 thorpej
44 1.1 thorpej /*
45 1.1 thorpej * Each kevent thread will make this many kevent() calls, and if it
46 1.1 thorpej * achieves this mark, we assume the race condition has not occurred
47 1.1 thorpej * the delcare the test passes.
48 1.1 thorpej */
49 1.1 thorpej #define NKEVENT_CALLS 10000
50 1.1 thorpej
51 1.1 thorpej static int kq;
52 1.1 thorpej
53 1.1 thorpej static void *
54 1.1 thorpej kevent_thread(void *arg)
55 1.1 thorpej {
56 1.1 thorpej struct timespec ts = { 0, 0 };
57 1.1 thorpej struct kevent event;
58 1.1 thorpej int rv, i;
59 1.1 thorpej
60 1.1 thorpej for (i = 0; i < NKEVENT_CALLS; i++) {
61 1.1 thorpej rv = kevent(kq, NULL, 0, &event, 1, &ts);
62 1.1 thorpej if (rv < 0) {
63 1.1 thorpej i = rv;
64 1.1 thorpej break;
65 1.1 thorpej }
66 1.1 thorpej if (rv != 1) {
67 1.1 thorpej break;
68 1.1 thorpej }
69 1.1 thorpej }
70 1.1 thorpej
71 1.1 thorpej return (void *)(intptr_t)i;
72 1.1 thorpej }
73 1.1 thorpej
74 1.1 thorpej ATF_TC(scan1);
75 1.1 thorpej ATF_TC_HEAD(scan1, tc)
76 1.1 thorpej {
77 1.1 thorpej atf_tc_set_md_var(tc, "descr",
78 1.1 thorpej "Exercises multi-threaded kevent() calls (PR kern/50094)");
79 1.1 thorpej }
80 1.1 thorpej
81 1.1 thorpej ATF_TC_BODY(scan1, tc)
82 1.1 thorpej {
83 1.1 thorpej pthread_t t1, t2;
84 1.1 thorpej void *v1, *v2;
85 1.1 thorpej intptr_t r1, r2;
86 1.1 thorpej struct kevent event;
87 1.1 thorpej int p[2];
88 1.1 thorpej
89 1.1 thorpej ATF_REQUIRE(pipe(p) == 0);
90 1.1 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
91 1.1 thorpej
92 1.1 thorpej EV_SET(&event, p[0], EVFILT_READ, EV_ADD, 0, 0, 0);
93 1.1 thorpej ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0);
94 1.1 thorpej ATF_REQUIRE(write(p[1], p, 1) == 1);
95 1.1 thorpej
96 1.1 thorpej ATF_REQUIRE(pthread_create(&t1, NULL, kevent_thread, NULL) == 0);
97 1.1 thorpej ATF_REQUIRE(pthread_create(&t2, NULL, kevent_thread, NULL) == 0);
98 1.1 thorpej
99 1.1 thorpej ATF_REQUIRE(pthread_join(t1, &v1) == 0);
100 1.1 thorpej r1 = (intptr_t)v1;
101 1.1 thorpej
102 1.1 thorpej ATF_REQUIRE(pthread_join(t2, &v2) == 0);
103 1.1 thorpej r2 = (intptr_t)v2;
104 1.1 thorpej
105 1.1 thorpej ATF_REQUIRE(r1 >= 0);
106 1.1 thorpej ATF_REQUIRE(r2 >= 0);
107 1.1 thorpej
108 1.1 thorpej ATF_REQUIRE(r1 == NKEVENT_CALLS);
109 1.1 thorpej ATF_REQUIRE(r2 == NKEVENT_CALLS);
110 1.1 thorpej }
111 1.1 thorpej
112 1.1 thorpej ATF_TP_ADD_TCS(tp)
113 1.1 thorpej {
114 1.1 thorpej ATF_TP_ADD_TC(tp, scan1);
115 1.1 thorpej
116 1.1 thorpej return atf_no_error();
117 1.1 thorpej }
118