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
31 # panic: KN_INFLUX set when not suppose to be
38 sed '1,/^EOF/d' < $odir/$0 > kevent.c
39 cc
-o kevent
-Wall kevent.c
-pthread
45 /tmp
/kevent
> /dev
/null
2>&1 &
54 #include <sys/types.h>
55 #include <sys/event.h>
62 pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
63 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
,
106 if (kevent
(kq
, ev
, n
, NULL
, 0, NULL
) < 0)
107 err
(1, "kevent(). %s:%d", __FILE__
, __LINE__
);
110 // printf("%s:%d\n", __FILE__
, __LINE__
); fflush
(stdout
);
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");
123 if (pthread_mutex_unlock
(&mutex
) == -1)
124 err
(1, "pthread_mutex_unlock");
125 // printf("%s:%d\n", __FILE__
, __LINE__
); fflush
(stdout
);
136 main
(int argc
, char
**argv
)
138 pthread_t threads
[2];
142 for (i
= 0; i
< 1000; i
++) {
144 // printf("%s:%d\n", __FILE__
, __LINE__
); fflush
(stdout
);
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");
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
));
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");