4 # Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
36 sed '1,/^EOF/d' < $odir/$0 > kevent.c
37 cc -o kevent -Wall kevent.c -pthread
43 /tmp/kevent > /dev/null 2>&1 &
52 #include <sys/types.h>
54 #include <sys/event.h>
61 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
62 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
77 if ((kq = kqueue()) < 0)
78 err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
81 EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
82 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
84 EV_SET(&ev[n], fd2[1], EVFILT_WRITE,
85 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
87 EV_SET(&ev[n], fd3[1], EVFILT_WRITE,
88 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
91 if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
92 err(1, "kevent(). %s:%d", __FILE__, __LINE__);
94 if (pthread_mutex_lock(&mutex) == -1)
95 err(1, "pthread_mutex_lock");
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");
103 EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
108 if (kevent(kq, ev, n, NULL, 0, &ts) < 0)
109 err(1, "kevent(). %s:%d", __FILE__, __LINE__);
112 // printf("%s:%d\n", __FILE__, __LINE__); fflush(stdout);
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");
128 if (pthread_mutex_unlock(&mutex) == -1)
129 err(1, "pthread_mutex_unlock");
130 // printf("%s:%d\n", __FILE__, __LINE__); fflush(stdout);
138 main(int argc, char **argv)
140 pthread_t threads[2];
144 for (i = 0; i < 1000; i++) {
146 // printf("%s:%d\n", __FILE__, __LINE__); fflush(stdout);
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");
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));
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");