ipc/chromium/src/third_party/libevent/sample/time-test.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/sample/time-test.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + * XXX This sample code was once meant to show how to use the basic Libevent
     1.6 + * interfaces, but it never worked on non-Unix platforms, and some of the
     1.7 + * interfaces have changed since it was first written.  It should probably
     1.8 + * be removed or replaced with something better.
     1.9 + *
    1.10 + * Compile with:
    1.11 + * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
    1.12 + */
    1.13 +
    1.14 +#include <sys/types.h>
    1.15 +
    1.16 +#include <event2/event-config.h>
    1.17 +
    1.18 +#include <sys/stat.h>
    1.19 +#ifndef WIN32
    1.20 +#include <sys/queue.h>
    1.21 +#include <unistd.h>
    1.22 +#endif
    1.23 +#include <time.h>
    1.24 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.25 +#include <sys/time.h>
    1.26 +#endif
    1.27 +#include <fcntl.h>
    1.28 +#include <stdlib.h>
    1.29 +#include <stdio.h>
    1.30 +#include <string.h>
    1.31 +#include <errno.h>
    1.32 +
    1.33 +#include <event2/event.h>
    1.34 +#include <event2/event_struct.h>
    1.35 +#include <event2/util.h>
    1.36 +
    1.37 +#ifdef WIN32
    1.38 +#include <winsock2.h>
    1.39 +#endif
    1.40 +
    1.41 +struct timeval lasttime;
    1.42 +
    1.43 +int event_is_persistent;
    1.44 +
    1.45 +static void
    1.46 +timeout_cb(evutil_socket_t fd, short event, void *arg)
    1.47 +{
    1.48 +	struct timeval newtime, difference;
    1.49 +	struct event *timeout = arg;
    1.50 +	double elapsed;
    1.51 +
    1.52 +	evutil_gettimeofday(&newtime, NULL);
    1.53 +	evutil_timersub(&newtime, &lasttime, &difference);
    1.54 +	elapsed = difference.tv_sec +
    1.55 +	    (difference.tv_usec / 1.0e6);
    1.56 +
    1.57 +	printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
    1.58 +	    (int)newtime.tv_sec, elapsed);
    1.59 +	lasttime = newtime;
    1.60 +
    1.61 +	if (! event_is_persistent) {
    1.62 +		struct timeval tv;
    1.63 +		evutil_timerclear(&tv);
    1.64 +		tv.tv_sec = 2;
    1.65 +		event_add(timeout, &tv);
    1.66 +	}
    1.67 +}
    1.68 +
    1.69 +int
    1.70 +main(int argc, char **argv)
    1.71 +{
    1.72 +	struct event timeout;
    1.73 +	struct timeval tv;
    1.74 +	struct event_base *base;
    1.75 +	int flags;
    1.76 +
    1.77 +#ifdef WIN32
    1.78 +	WORD wVersionRequested;
    1.79 +	WSADATA wsaData;
    1.80 +
    1.81 +	wVersionRequested = MAKEWORD(2, 2);
    1.82 +
    1.83 +	(void)WSAStartup(wVersionRequested, &wsaData);
    1.84 +#endif
    1.85 +
    1.86 +	if (argc == 2 && !strcmp(argv[1], "-p")) {
    1.87 +		event_is_persistent = 1;
    1.88 +		flags = EV_PERSIST;
    1.89 +	} else {
    1.90 +		event_is_persistent = 0;
    1.91 +		flags = 0;
    1.92 +	}
    1.93 +
    1.94 +	/* Initalize the event library */
    1.95 +	base = event_base_new();
    1.96 +
    1.97 +	/* Initalize one event */
    1.98 +	event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
    1.99 +
   1.100 +	evutil_timerclear(&tv);
   1.101 +	tv.tv_sec = 2;
   1.102 +	event_add(&timeout, &tv);
   1.103 +
   1.104 +	evutil_gettimeofday(&lasttime, NULL);
   1.105 +
   1.106 +	event_base_dispatch(base);
   1.107 +
   1.108 +	return (0);
   1.109 +}
   1.110 +

mercurial