xref: /freebsd/tools/test/stress2/misc/kevent.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2
3#
4# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# panic: KN_INFLUX set when not suppose to be
30
31. ../default.cfg
32
33odir=`pwd`
34
35cd /tmp
36sed '1,/^EOF/d' < $odir/$0 > kevent.c
37mycc -o kevent -Wall kevent.c -pthread || exit 1
38rm -f kevent.c
39[ -d "$RUNDIR" ] || mkdir -p $RUNDIR
40cd $RUNDIR
41
42for i in `jot 10`; do
43	for j in `jot 12`; do
44		/tmp/kevent > /dev/null 2>&1 &
45	done
46	wait
47done
48rm -f /tmp/kevent
49exit
50EOF
51#include <sys/types.h>
52#include <sys/event.h>
53
54#include <err.h>
55#include <pthread.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <time.h>
60#include <unistd.h>
61
62pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
63pthread_cond_t  cond  = PTHREAD_COND_INITIALIZER;
64static int waiting;
65
66static int fd1[2];
67static int fd2[2];
68static int fd3[2];
69
70#define RUNTIME 12
71
72static void *
73thr1(void *arg)
74{
75	struct kevent ev[3];
76	int kq, n, r;
77
78	if ((kq = kqueue()) < 0)
79		err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
80
81	n = 0;
82	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
83		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
84	n++;
85	EV_SET(&ev[n], fd2[1], EVFILT_WRITE,
86		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
87	n++;
88	EV_SET(&ev[n], fd3[1], EVFILT_WRITE,
89		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
90	n++;
91
92	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
93		err(1, "kevent(). %s:%d", __FILE__, __LINE__);
94
95	if ((r = pthread_mutex_lock(&mutex)) != 0)
96		errc(1, r, "pthread_mutex_lock");
97	waiting = 0;
98	if ((r = pthread_cond_signal(&cond)) != 0)
99		errc(1, r, "pthread_cond_signal");
100	if ((r = pthread_mutex_unlock(&mutex)) != 0)
101		errc(1, r, "pthread_mutex_unlock");
102
103	n = 0;
104	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
105		    EV_DELETE, 0, 0, 0);
106	n++;
107	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
108		warn("kevent(). %s:%d", __FILE__, __LINE__);
109	close(kq);
110
111	return (0);
112}
113
114static void *
115thr2(void *arg)
116{
117	int r;
118
119	if ((r = pthread_mutex_lock(&mutex)) != 0)
120		errc(1, r, "pthread_mutex_lock");
121	while (waiting == 1) {
122		if ((r = pthread_cond_wait(&cond, &mutex)) != 0)
123			errc(1, r, "pthread_cond_wait");
124	}
125	if ((r = pthread_mutex_unlock(&mutex)) != 0)
126		errc(1, r, "pthread_mutex_unlock");
127	close(fd1[0]);
128	close(fd1[1]);
129	close(fd2[0]);
130	close(fd2[1]);
131	close(fd3[0]);
132	close(fd3[1]);
133	return (0);
134}
135
136int
137main(int argc, char **argv)
138{
139	pthread_t threads[2];
140	time_t start;
141	int r;
142
143	start = time(NULL);
144	while (time(NULL) - start < RUNTIME) {
145		waiting = 1;
146		if (pipe(fd1) == -1)
147			err(1, "pipe()");
148		if (pipe(fd2) == -1)
149			err(1, "pipe()");
150		if (pipe(fd3) == -1)
151			err(1, "pipe()");
152
153		if ((r = pthread_mutex_init(&mutex, 0)) != 0)
154			errc(1, r, "pthread_mutex_init");
155		if ((r = pthread_cond_init(&cond, NULL)) != 0)
156			errc(1, r, "pthread_cond_init");
157
158		if ((r = pthread_create(&threads[0], NULL, thr1, 0)) != 0)
159			errc(1, r, "pthread_create()");
160		if ((r = pthread_create(&threads[1], NULL, thr2, 0)) != 0)
161			errc(1, r, "pthread_create()");
162
163		if ((r = pthread_join(threads[0], NULL)) != 0)
164			errc(1, r, "pthread_join(%d)", 0);
165		if ((r = pthread_join(threads[1], NULL)) != 0)
166			errc(1, r, "pthread_join(%d)", 1);
167		if ((r = pthread_mutex_destroy(&mutex)) != 0)
168			errc(1, r, "pthread_mutex_destroy");
169		if ((r = pthread_cond_destroy(&cond)) != 0)
170			errc(1, r, "pthread_cond_destroy)");
171	}
172
173	return (0);
174}
175