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