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