18a272653SPeter Holm#!/bin/sh 28a272653SPeter Holm 38a272653SPeter Holm# 48a272653SPeter Holm# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org> 58a272653SPeter Holm# All rights reserved. 68a272653SPeter Holm# 78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without 88a272653SPeter Holm# modification, are permitted provided that the following conditions 98a272653SPeter Holm# are met: 108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright 118a272653SPeter Holm# notice, this list of conditions and the following disclaimer. 128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 138a272653SPeter Holm# notice, this list of conditions and the following disclaimer in the 148a272653SPeter Holm# documentation and/or other materials provided with the distribution. 158a272653SPeter Holm# 168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198a272653SPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268a272653SPeter Holm# SUCH DAMAGE. 278a272653SPeter Holm# 288a272653SPeter Holm 298a272653SPeter Holm. ../default.cfg 308a272653SPeter Holm 318a272653SPeter Holmodir=`pwd` 328a272653SPeter Holm 338a272653SPeter Holmcd /tmp 348a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > kevent2.c 358a272653SPeter Holmmycc -o kevent2 -Wall kevent2.c -pthread || exit 1 368a272653SPeter Holmrm -f kevent2.c 378a272653SPeter Holmcd $RUNDIR 388a272653SPeter Holm 398a272653SPeter Holmfor i in `jot 10`; do 408a272653SPeter Holm for j in `jot 12`; do 418a272653SPeter Holm /tmp/kevent2 > /dev/null 2>&1 & 428a272653SPeter Holm done 438a272653SPeter Holm wait 448a272653SPeter Holmdone 458a272653SPeter Holmrm -f /tmp/kevent2 468a272653SPeter Holmexit 478a272653SPeter HolmEOF 488a272653SPeter Holm#include <sys/types.h> 498a272653SPeter Holm#include <sys/event.h> 508a272653SPeter Holm#include <sys/time.h> 518a272653SPeter Holm 528a272653SPeter Holm#include <err.h> 538a272653SPeter Holm#include <pthread.h> 548a272653SPeter Holm#include <stdio.h> 558a272653SPeter Holm#include <stdlib.h> 568a272653SPeter Holm#include <string.h> 578a272653SPeter Holm#include <unistd.h> 588a272653SPeter Holm 598a272653SPeter Holmpthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 608a272653SPeter Holmpthread_cond_t cond = PTHREAD_COND_INITIALIZER; 618a272653SPeter Holmstatic int waiting; 628a272653SPeter Holm 638a272653SPeter Holmstatic int fd1[2]; 648a272653SPeter Holmstatic int fd2[2]; 658a272653SPeter Holmstatic int fd3[2]; 668a272653SPeter Holm 678a272653SPeter Holmstatic void * 688a272653SPeter Holmthr1(void *arg) 698a272653SPeter Holm{ 708a272653SPeter Holm struct kevent ev[3]; 718a272653SPeter Holm struct timespec ts; 728a272653SPeter Holm int kq, n, r; 738a272653SPeter Holm 748a272653SPeter Holm if ((kq = kqueue()) < 0) 758a272653SPeter Holm err(1, "kqueue(). %s:%d", __FILE__, __LINE__); 768a272653SPeter Holm 778a272653SPeter Holm n = 0; 788a272653SPeter Holm EV_SET(&ev[n], fd1[1], EVFILT_WRITE, 798a272653SPeter Holm EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); 808a272653SPeter Holm n++; 818a272653SPeter Holm EV_SET(&ev[n], fd2[1], EVFILT_WRITE, 828a272653SPeter Holm EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); 838a272653SPeter Holm n++; 848a272653SPeter Holm EV_SET(&ev[n], fd3[1], EVFILT_WRITE, 858a272653SPeter Holm EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); 868a272653SPeter Holm n++; 878a272653SPeter Holm 888a272653SPeter Holm if (kevent(kq, ev, n, NULL, 0, NULL) < 0) 898a272653SPeter Holm err(1, "kevent(). %s:%d", __FILE__, __LINE__); 908a272653SPeter Holm 918a272653SPeter Holm if ((r = pthread_mutex_lock(&mutex)) != 0) 928a272653SPeter Holm errc(1, r, "pthread_mutex_lock"); 938a272653SPeter Holm waiting = 0; 948a272653SPeter Holm if ((r = pthread_cond_signal(&cond)) != 0) 958a272653SPeter Holm errc(1, r, "pthread_cond_signal"); 968a272653SPeter Holm if ((r = pthread_mutex_unlock(&mutex)) != 0) 978a272653SPeter Holm errc(1, r, "pthread_mutex_unlock"); 988a272653SPeter Holm 998a272653SPeter Holm n = 0; 1008a272653SPeter Holm EV_SET(&ev[n], fd1[1], EVFILT_WRITE, 1018a272653SPeter Holm EV_DELETE, 0, 0, 0); 1028a272653SPeter Holm n++; 1038a272653SPeter Holm ts.tv_sec = 0; 1048a272653SPeter Holm ts.tv_nsec = 0; 1058a272653SPeter Holm if (kevent(kq, ev, n, NULL, 0, &ts) < 0) 1068a272653SPeter Holm err(1, "kevent(). %s:%d", __FILE__, __LINE__); 1078a272653SPeter Holm close(kq); 1088a272653SPeter Holm 1098a272653SPeter Holm close(fd1[1]); 1108a272653SPeter Holm close(fd2[1]); 1118a272653SPeter Holm close(fd3[1]); 1128a272653SPeter Holm return (0); 1138a272653SPeter Holm} 1148a272653SPeter Holm 1158a272653SPeter Holmstatic void * 1168a272653SPeter Holmthr2(void *arg) 1178a272653SPeter Holm{ 1188a272653SPeter Holm int r; 1198a272653SPeter Holm 1208a272653SPeter Holm if ((r = pthread_mutex_lock(&mutex)) != 0) 1218a272653SPeter Holm errc(1, r, "pthread_mutex_lock"); 1228a272653SPeter Holm while (waiting == 1) { 1238a272653SPeter Holm if ((r = pthread_cond_wait(&cond, &mutex)) != 0) 1248a272653SPeter Holm errc(1, r, "pthread_cond_wait"); 1258a272653SPeter Holm } 1268a272653SPeter Holm if ((r = pthread_mutex_unlock(&mutex)) != 0) 1278a272653SPeter Holm errc(1, r, "pthread_mutex_unlock"); 1288a272653SPeter Holm close(fd1[0]); 1298a272653SPeter Holm close(fd2[0]); 1308a272653SPeter Holm close(fd3[0]); 1318a272653SPeter Holm return (0); 1328a272653SPeter Holm} 1338a272653SPeter Holm 1348a272653SPeter Holmint 1358a272653SPeter Holmmain(int argc, char **argv) 1368a272653SPeter Holm{ 1378a272653SPeter Holm pthread_t threads[2]; 1388a272653SPeter Holm int i, r; 1398a272653SPeter Holm 1408a272653SPeter Holm for (i = 0; i < 1000; i++) { 1418a272653SPeter Holm waiting = 1; 1428a272653SPeter Holm if (pipe(fd1) == -1) 1438a272653SPeter Holm err(1, "pipe()"); 1448a272653SPeter Holm if (pipe(fd2) == -1) 1458a272653SPeter Holm err(1, "pipe()"); 1468a272653SPeter Holm if (pipe(fd3) == -1) 1478a272653SPeter Holm err(1, "pipe()"); 1488a272653SPeter Holm 1498a272653SPeter Holm if ((r = pthread_mutex_init(&mutex, 0)) != 0) 1508a272653SPeter Holm errc(1, r, "pthread_mutex_init"); 1518a272653SPeter Holm if ((r = pthread_cond_init(&cond, NULL)) != 0) 1528a272653SPeter Holm errc(1, r, "pthread_cond_init"); 1538a272653SPeter Holm 1548a272653SPeter Holm if ((r = pthread_create(&threads[0], NULL, thr1, 0)) != 0) 1558a272653SPeter Holm errc(1, r, "pthread_create()"); 1568a272653SPeter Holm if ((r = pthread_create(&threads[1], NULL, thr2, 0)) != 0) 1578a272653SPeter Holm errc(1, r, "pthread_create()"); 1588a272653SPeter Holm 1598a272653SPeter Holm if ((r = pthread_join(threads[0], NULL)) != 0) 1608a272653SPeter Holm errc(1, r, "pthread_join(%d)", 0); 1618a272653SPeter Holm if ((r = pthread_join(threads[1], NULL)) != 0) 1628a272653SPeter Holm errc(1, r, "pthread_join(%d)", 1); 1638a272653SPeter Holm if ((r = pthread_mutex_destroy(&mutex)) != 0) 1648a272653SPeter Holm errc(1, r, "pthread_mutex_destroy"); 1658a272653SPeter Holm if ((r = pthread_cond_destroy(&cond)) != 0) 1668a272653SPeter Holm errc(1, r, "pthread_condattr_destroy"); 1678a272653SPeter Holm } 1688a272653SPeter Holm 1698a272653SPeter Holm return (0); 1708a272653SPeter Holm} 171