1 /* $NetBSD: time-test.c,v 1.1.1.1 2013/04/11 16:43:31 christos Exp $ */
3 * XXX This sample code was once meant to show how to use the basic Libevent
4 * interfaces, but it never worked on non-Unix platforms, and some of the
5 * interfaces have changed since it was first written. It should probably
6 * be removed or replaced with something better.
9 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
12 #include <sys/types.h>
14 #include <event2/event-config.h>
18 #include <sys/queue.h>
22 #ifdef _EVENT_HAVE_SYS_TIME_H
31 #include <event2/event.h>
32 #include <event2/event_struct.h>
33 #include <event2/util.h>
39 struct timeval lasttime
;
41 int event_is_persistent
;
44 timeout_cb(evutil_socket_t fd
, short event
, void *arg
)
46 struct timeval newtime
, difference
;
47 struct event
*timeout
= arg
;
50 evutil_gettimeofday(&newtime
, NULL
);
51 evutil_timersub(&newtime
, &lasttime
, &difference
);
52 elapsed
= difference
.tv_sec
+
53 (difference
.tv_usec
/ 1.0e6
);
55 printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
56 (int)newtime
.tv_sec
, elapsed
);
59 if (! event_is_persistent
) {
61 evutil_timerclear(&tv
);
63 event_add(timeout
, &tv
);
68 main(int argc
, char **argv
)
72 struct event_base
*base
;
76 WORD wVersionRequested
;
79 wVersionRequested
= MAKEWORD(2, 2);
81 (void)WSAStartup(wVersionRequested
, &wsaData
);
84 if (argc
== 2 && !strcmp(argv
[1], "-p")) {
85 event_is_persistent
= 1;
88 event_is_persistent
= 0;
92 /* Initalize the event library */
93 base
= event_base_new();
95 /* Initalize one event */
96 event_assign(&timeout
, base
, -1, flags
, timeout_cb
, (void*) &timeout
);
98 evutil_timerclear(&tv
);
100 event_add(&timeout
, &tv
);
102 evutil_gettimeofday(&lasttime
, NULL
);
104 event_base_dispatch(base
);