1 /*
2  * ivykis, an event handling library
3  * Copyright (C) 2012 Lennert Buytenhek
4  * Dedicated to Marija Kulikova.
5  *
6  * This library is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version
8  * 2.1 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License version 2.1 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License version 2.1 along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <iv.h>
24 #include <iv_event_raw.h>
25 #ifdef USE_SIGNAL
26 #include <signal.h>
27 #endif
28 
29 static int die;
30 static int ev_received;
31 static struct iv_event_raw ev;
32 static struct timespec tim_start;
33 static struct timespec tim_end;
34 
35 #ifdef USE_SIGNAL
got_signal_timeout(int sigh)36 static void got_signal_timeout(int sigh)
37 {
38 	die = 1;
39 }
40 #else
41 static struct iv_timer timeout;
42 
got_timer_timeout(void * _dummy)43 static void got_timer_timeout(void *_dummy)
44 {
45 	die = 1;
46 }
47 #endif
48 
got_ev(void * _dummy)49 static void got_ev(void *_dummy)
50 {
51 	ev_received++;
52 
53 	if (!die) {
54 		iv_event_raw_post(&ev);
55 	} else {
56 		iv_validate_now();
57 		tim_end = iv_now;
58 		iv_event_raw_unregister(&ev);
59 	}
60 }
61 
main()62 int main()
63 {
64 	long long nsec;
65 
66 	iv_init();
67 
68 #ifdef USE_SIGNAL
69 	signal(SIGALRM, got_signal_timeout);
70 	alarm(5);
71 #else
72 	IV_TIMER_INIT(&timeout);
73 	iv_validate_now();
74 	timeout.expires = iv_now;
75 	timeout.expires.tv_sec += 5;
76 	timeout.handler = got_timer_timeout;
77 	iv_timer_register(&timeout);
78 #endif
79 
80 	IV_EVENT_RAW_INIT(&ev);
81 	ev.handler = got_ev;
82 	iv_event_raw_register(&ev);
83 
84 	iv_validate_now();
85 	tim_start = iv_now;
86 
87 	iv_event_raw_post(&ev);
88 
89 	iv_main();
90 
91 	iv_deinit();
92 
93 	nsec = 1000000000ULL * (tim_end.tv_sec - tim_start.tv_sec) +
94 		(tim_end.tv_nsec - tim_start.tv_nsec);
95 
96 	printf("%s: %d in %ld nsec => %d/sec\n",
97 	       iv_poll_method_name(), ev_received, (long)nsec,
98 	       (int)(1000000000ULL * ev_received / nsec));
99 
100 	return 0;
101 }
102