xref: /freebsd/tools/test/stress2/misc/kevent2.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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. ../default.cfg
30
31odir=`pwd`
32
33cd /tmp
34sed '1,/^EOF/d' < $odir/$0 > kevent2.c
35mycc -o kevent2 -Wall kevent2.c -pthread || exit 1
36rm -f kevent2.c
37cd $RUNDIR
38
39for i in `jot 10`; do
40	for j in `jot 12`; do
41		/tmp/kevent2 > /dev/null 2>&1 &
42	done
43	wait
44done
45rm -f /tmp/kevent2
46exit
47EOF
48#include <sys/types.h>
49#include <sys/event.h>
50#include <sys/time.h>
51
52#include <err.h>
53#include <pthread.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58
59pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
60pthread_cond_t  cond  = PTHREAD_COND_INITIALIZER;
61static int waiting;
62
63static int fd1[2];
64static int fd2[2];
65static int fd3[2];
66
67static void *
68thr1(void *arg)
69{
70	struct kevent ev[3];
71	struct timespec ts;
72	int kq, n, r;
73
74	if ((kq = kqueue()) < 0)
75		err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
76
77	n = 0;
78	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
79		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
80	n++;
81	EV_SET(&ev[n], fd2[1], EVFILT_WRITE,
82		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
83	n++;
84	EV_SET(&ev[n], fd3[1], EVFILT_WRITE,
85		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
86	n++;
87
88	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
89		err(1, "kevent(). %s:%d", __FILE__, __LINE__);
90
91	if ((r = pthread_mutex_lock(&mutex)) != 0)
92		errc(1, r, "pthread_mutex_lock");
93	waiting = 0;
94	if ((r = pthread_cond_signal(&cond)) != 0)
95		errc(1, r, "pthread_cond_signal");
96	if ((r = pthread_mutex_unlock(&mutex)) != 0)
97		errc(1, r, "pthread_mutex_unlock");
98
99	n = 0;
100	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
101		    EV_DELETE, 0, 0, 0);
102	n++;
103	ts.tv_sec = 0;
104	ts.tv_nsec = 0;
105	if (kevent(kq, ev, n, NULL, 0, &ts) < 0)
106		err(1, "kevent(). %s:%d", __FILE__, __LINE__);
107	close(kq);
108
109	close(fd1[1]);
110	close(fd2[1]);
111	close(fd3[1]);
112	return (0);
113}
114
115static void *
116thr2(void *arg)
117{
118	int r;
119
120	if ((r = pthread_mutex_lock(&mutex)) != 0)
121		errc(1, r, "pthread_mutex_lock");
122	while (waiting == 1) {
123		if ((r = pthread_cond_wait(&cond, &mutex)) != 0)
124			errc(1, r, "pthread_cond_wait");
125	}
126	if ((r = pthread_mutex_unlock(&mutex)) != 0)
127		errc(1, r, "pthread_mutex_unlock");
128	close(fd1[0]);
129	close(fd2[0]);
130	close(fd3[0]);
131	return (0);
132}
133
134int
135main(int argc, char **argv)
136{
137	pthread_t threads[2];
138	int i, r;
139
140	for (i = 0; i < 1000; i++) {
141		waiting = 1;
142		if (pipe(fd1) == -1)
143			err(1, "pipe()");
144		if (pipe(fd2) == -1)
145			err(1, "pipe()");
146		if (pipe(fd3) == -1)
147			err(1, "pipe()");
148
149		if ((r = pthread_mutex_init(&mutex, 0)) != 0)
150			errc(1, r, "pthread_mutex_init");
151		if ((r = pthread_cond_init(&cond, NULL)) != 0)
152			errc(1, r, "pthread_cond_init");
153
154		if ((r = pthread_create(&threads[0], NULL, thr1, 0)) != 0)
155			errc(1, r, "pthread_create()");
156		if ((r = pthread_create(&threads[1], NULL, thr2, 0)) != 0)
157			errc(1, r, "pthread_create()");
158
159		if ((r = pthread_join(threads[0], NULL)) != 0)
160			errc(1, r, "pthread_join(%d)", 0);
161		if ((r = pthread_join(threads[1], NULL)) != 0)
162			errc(1, r, "pthread_join(%d)", 1);
163		if ((r = pthread_mutex_destroy(&mutex)) != 0)
164			errc(1, r, "pthread_mutex_destroy");
165		if ((r = pthread_cond_destroy(&cond)) != 0)
166			errc(1, r, "pthread_condattr_destroy");
167	}
168
169	return (0);
170}
171