xref: /dragonfly/test/debug/evfilt_user.c (revision 9348a738)
1 /*
2  * cc evfilt_user.c -pthread -I/usr/src/sys/ -o /tmp/evfilt_user
3  */
4 
5 #include <sys/types.h>
6 #include <sys/event.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 
12 static void *domaster(void *arg);
13 static void *doslave(void *arg);
14 
15 int
16 main(int ac, char **avj)
17 {
18     int fd;
19     int r;
20     struct kevent kev;
21     pthread_t td1;
22     pthread_t td2;
23 
24     fd = kqueue();
25     EV_SET(&kev, 1, EVFILT_USER, EV_ADD|EV_CLEAR, 0, 0, 0);
26     r = kevent(fd, &kev, 1, NULL, 0, NULL);
27     printf("r = %d\n", r);
28     pthread_create(&td1, NULL, domaster, (void *)(intptr_t)fd);
29     sleep(1);
30     pthread_create(&td2, NULL, doslave, (void *)(intptr_t)fd);
31     pthread_join(td1, NULL);
32     pthread_join(td2, NULL);
33 }
34 
35 static void *
36 domaster(void *arg)
37 {
38 	struct kevent kev;
39 	int fd = (int)(intptr_t)arg;
40 	int r;
41 
42 	printf("master running\n");
43 	for (;;) {
44 		EV_SET(&kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
45 		r = kevent(fd, &kev, 1, NULL, 0, NULL);
46 		printf("issued trigger %d\n", r);
47 		sleep(1);
48 	}
49 
50 	return NULL;
51 }
52 
53 static void *
54 doslave(void *arg)
55 {
56 	struct kevent kev;
57 	int fd = (int)(intptr_t)arg;
58 	int r;
59 
60 	printf("slave running\n");
61 	for (;;) {
62 		r = kevent(fd, NULL, 0, &kev, 1, NULL);
63 		printf("received %d\n", r);
64 		/*
65 		EV_SET(&kev, 1, EVFILT_USER, EV_CLEAR, 0, 0, 0);
66 		r = kevent(fd, &kev, 1, NULL, 0, NULL);
67 		*/
68 	}
69 	return NULL;
70 }
71