113d2f99bSMatthew Dillon /*
213d2f99bSMatthew Dillon * cc evfilt_user.c -pthread -I/usr/src/sys/ -o /tmp/evfilt_user
313d2f99bSMatthew Dillon */
413d2f99bSMatthew Dillon
513d2f99bSMatthew Dillon #include <sys/types.h>
613d2f99bSMatthew Dillon #include <sys/event.h>
713d2f99bSMatthew Dillon #include <stdio.h>
813d2f99bSMatthew Dillon #include <stdlib.h>
9*b7cb6098SMatthew Dillon #include <unistd.h>
1013d2f99bSMatthew Dillon #include <pthread.h>
1113d2f99bSMatthew Dillon
1213d2f99bSMatthew Dillon static void *domaster(void *arg);
1313d2f99bSMatthew Dillon static void *doslave(void *arg);
1413d2f99bSMatthew Dillon
1513d2f99bSMatthew Dillon int
main(int ac,char ** avj)1613d2f99bSMatthew Dillon main(int ac, char **avj)
1713d2f99bSMatthew Dillon {
1813d2f99bSMatthew Dillon int fd;
1913d2f99bSMatthew Dillon int r;
2013d2f99bSMatthew Dillon struct kevent kev;
2113d2f99bSMatthew Dillon pthread_t td1;
2213d2f99bSMatthew Dillon pthread_t td2;
2313d2f99bSMatthew Dillon
2413d2f99bSMatthew Dillon fd = kqueue();
2513d2f99bSMatthew Dillon EV_SET(&kev, 1, EVFILT_USER, EV_ADD|EV_CLEAR, 0, 0, 0);
2613d2f99bSMatthew Dillon r = kevent(fd, &kev, 1, NULL, 0, NULL);
2713d2f99bSMatthew Dillon printf("r = %d\n", r);
2813d2f99bSMatthew Dillon pthread_create(&td1, NULL, domaster, (void *)(intptr_t)fd);
2913d2f99bSMatthew Dillon sleep(1);
3013d2f99bSMatthew Dillon pthread_create(&td2, NULL, doslave, (void *)(intptr_t)fd);
3113d2f99bSMatthew Dillon pthread_join(td1, NULL);
3213d2f99bSMatthew Dillon pthread_join(td2, NULL);
3313d2f99bSMatthew Dillon }
3413d2f99bSMatthew Dillon
3513d2f99bSMatthew Dillon static void *
domaster(void * arg)3613d2f99bSMatthew Dillon domaster(void *arg)
3713d2f99bSMatthew Dillon {
3813d2f99bSMatthew Dillon struct kevent kev;
3913d2f99bSMatthew Dillon int fd = (int)(intptr_t)arg;
4013d2f99bSMatthew Dillon int r;
4113d2f99bSMatthew Dillon
4213d2f99bSMatthew Dillon printf("master running\n");
4313d2f99bSMatthew Dillon for (;;) {
4413d2f99bSMatthew Dillon EV_SET(&kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
4513d2f99bSMatthew Dillon r = kevent(fd, &kev, 1, NULL, 0, NULL);
4613d2f99bSMatthew Dillon printf("issued trigger %d\n", r);
4713d2f99bSMatthew Dillon sleep(1);
4813d2f99bSMatthew Dillon }
4913d2f99bSMatthew Dillon
5013d2f99bSMatthew Dillon return NULL;
5113d2f99bSMatthew Dillon }
5213d2f99bSMatthew Dillon
5313d2f99bSMatthew Dillon static void *
doslave(void * arg)5413d2f99bSMatthew Dillon doslave(void *arg)
5513d2f99bSMatthew Dillon {
5613d2f99bSMatthew Dillon struct kevent kev;
5713d2f99bSMatthew Dillon int fd = (int)(intptr_t)arg;
5813d2f99bSMatthew Dillon int r;
5913d2f99bSMatthew Dillon
6013d2f99bSMatthew Dillon printf("slave running\n");
6113d2f99bSMatthew Dillon for (;;) {
6213d2f99bSMatthew Dillon r = kevent(fd, NULL, 0, &kev, 1, NULL);
6313d2f99bSMatthew Dillon printf("received %d\n", r);
6413d2f99bSMatthew Dillon /*
6513d2f99bSMatthew Dillon EV_SET(&kev, 1, EVFILT_USER, EV_CLEAR, 0, 0, 0);
6613d2f99bSMatthew Dillon r = kevent(fd, &kev, 1, NULL, 0, NULL);
6713d2f99bSMatthew Dillon */
6813d2f99bSMatthew Dillon }
6913d2f99bSMatthew Dillon return NULL;
7013d2f99bSMatthew Dillon }
71