diff options
author | Nick Mathewson <nickm@torproject.org> | 2005-01-12 06:42:32 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2005-01-12 06:42:32 +0000 |
commit | 324b192f68bfe697009831a5bad3acdd7bd2cec5 (patch) | |
tree | 13f8d3bd453244fcad1f519667bb4483156113bf /src/common | |
parent | 9b578f2fe2665a1b1a75f6fb5a85f8a77ee31545 (diff) | |
download | tor-324b192f68bfe697009831a5bad3acdd7bd2cec5.tar.gz tor-324b192f68bfe697009831a5bad3acdd7bd2cec5.zip |
Make Tor use Niels Provos's libevent instead of it's current
poll-but-sometimes-select mess. This will let us use faster async cores
(like epoll, kpoll, and /dev/poll), and hopefully work better on Windows
too.
There are some fairly nasty changes to main.c here; this will almost
certainly break something. But hey, that's what alphas are for.
svn:r3341
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Makefile.am | 4 | ||||
-rw-r--r-- | src/common/compat.c | 1 | ||||
-rw-r--r-- | src/common/compat.h | 4 | ||||
-rw-r--r-- | src/common/fakepoll.c | 108 | ||||
-rw-r--r-- | src/common/fakepoll.h | 73 |
5 files changed, 6 insertions, 184 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 65f722bfe8..ae3f8a8f7f 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -3,7 +3,7 @@ noinst_LIBRARIES = libor.a libor-crypto.a #CFLAGS = -Wall -Wpointer-arith -O2 -libor_a_SOURCES = log.c fakepoll.c util.c compat.c container.c +libor_a_SOURCES = log.c util.c compat.c container.c libor_crypto_a_SOURCES = crypto.c aes.c tortls.c torgzip.c -noinst_HEADERS = log.h crypto.h fakepoll.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h +noinst_HEADERS = log.h crypto.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h diff --git a/src/common/compat.c b/src/common/compat.c index e9f0b23371..69b82f8a41 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -8,7 +8,6 @@ const char compat_c_id[] = "$Id$"; #define _GNU_SOURCE #include "orconfig.h" -#include "fakepoll.h" #include "compat.h" #ifdef MS_WINDOWS diff --git a/src/common/compat.h b/src/common/compat.h index d9cb5669d6..186416d882 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -115,6 +115,10 @@ int replace_file(const char *from, const char *to); #define tor_close_socket(s) close(s) #endif +/* Now that we use libevent, all real sockets are safe for polling ... or + * if they aren't, libevent will help us. */ +#define SOCKET_IS_POLLABLE(fd) ((fd)>=0) + struct in_addr; int tor_inet_aton(const char *cp, struct in_addr *addr); int tor_lookup_hostname(const char *name, uint32_t *addr); diff --git a/src/common/fakepoll.c b/src/common/fakepoll.c deleted file mode 100644 index 19f5c1de31..0000000000 --- a/src/common/fakepoll.c +++ /dev/null @@ -1,108 +0,0 @@ -/* Copyright 2002,2003 Nick Mathewson, Roger Dingledine */ -/* See LICENSE for licensing information */ -/* $Id$ */ -const char fakepoll_c_id[] = "$Id$"; - -/** - * \file fakepoll.c - * - * \brief On systems where poll() doesn't exist, fake it with select(). - **/ - -#include "orconfig.h" -#include "fakepoll.h" - -#ifdef HAVE_SYS_TYPES_H -#include <sys/types.h> -#endif -#ifdef HAVE_UNISTD_H -#include <unistd.h> -#endif -#ifdef HAVE_STRING_H -#include <string.h> -#endif -#ifdef HAVE_SYS_TIME_H -#include <sys/time.h> -#endif - -#include <assert.h> -#include <stdlib.h> -#include "util.h" -#include "log.h" - -#ifndef USE_FAKE_POLL -int -tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout) -{ - unsigned int i; - for (i=0;i<nfds;++i) { - tor_assert(ufds[i].fd >= 0); - } - return poll(ufds,nfds,timeout); -} -#else -int -tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout) -{ - unsigned int idx; - int maxfd, fd; - int r; -#ifdef MS_WINDOWS - int any_fds_set = 0; -#endif - fd_set readfds, writefds, exceptfds; -#ifdef USING_FAKE_TIMEVAL -#undef timeval -#undef tv_sec -#undef tv_usec -#endif - struct timeval _timeout; - _timeout.tv_sec = timeout/1000; - _timeout.tv_usec = (timeout%1000)*1000; - FD_ZERO(&readfds); - FD_ZERO(&writefds); - FD_ZERO(&exceptfds); - - maxfd = -1; - for (idx = 0; idx < nfds; ++idx) { - ufds[idx].revents = 0; - fd = ufds[idx].fd; - tor_assert(SOCKET_IS_POLLABLE(fd)); - if (fd > maxfd) { - maxfd = fd; -#ifdef MS_WINDOWS - any_fds_set = 1; -#endif - } - if (ufds[idx].events & POLLIN) - FD_SET(fd, &readfds); - if (ufds[idx].events & POLLOUT) - FD_SET(fd, &writefds); - FD_SET(fd, &exceptfds); - } -#ifdef MS_WINDOWS - if (!any_fds_set) { - Sleep(timeout); - return 0; - } -#endif - r = select(maxfd+1, &readfds, &writefds, &exceptfds, - timeout == -1 ? NULL : &_timeout); - if (r <= 0) - return r; - r = 0; - for (idx = 0; idx < nfds; ++idx) { - fd = ufds[idx].fd; - if (FD_ISSET(fd, &readfds)) - ufds[idx].revents |= POLLIN; - if (FD_ISSET(fd, &writefds)) - ufds[idx].revents |= POLLOUT; - if (FD_ISSET(fd, &exceptfds)) - ufds[idx].revents |= POLLERR; - if (ufds[idx].revents) - ++r; - } - return r; -} -#endif - diff --git a/src/common/fakepoll.h b/src/common/fakepoll.h deleted file mode 100644 index 3f3d1610bb..0000000000 --- a/src/common/fakepoll.h +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright 2002,2003 Nick Mathewson, Roger Dingledine. */ -/* See LICENSE for licensing information */ -/* $Id$ */ - -#ifndef __FAKEPOLL_H -#define __FAKEPOLL_H -#define FAKEPOLL_H_ID "$Id$" - -/** - * \file fakepoll.h - * \brief Headers for fakepoll.c - */ - -#include "orconfig.h" - -#define POLL_NO_WARN - -#if defined(HAVE_POLL_H) -#include <poll.h> -#elif defined(HAVE_SYS_POLL_H) -#include <sys/poll.h> -#endif - -/* If _POLL_EMUL_H_ is defined, then poll is just a just a thin wrapper around - * select. On Mac OS 10.3, this wrapper is kinda flaky, and we should - * use our own. - */ -#if !(defined(HAVE_POLL_H)||defined(HAVE_SYS_POLL_H))&&!defined(_POLL_EMUL_H_) -#define USE_FAKE_POLL -#endif - -#if defined USE_FAKE_POLL && !defined(_POLL_EMUL_H_) -struct pollfd { - int fd; - short events; - short revents; -}; - -#define POLLIN 0x0001 -#define POLLPRI 0x0002 -#define POLLOUT 0x0004 -#define POLLERR 0x0008 -#define POLLHUP 0x0010 -#define POLLNVAL 0x0020 -#endif - -#ifdef MS_WINDOWS -#define MAXCONNECTIONS 10000 /* XXXX copied from or.h */ -/* This trick makes winsock resize fd_set, which defaults to the insanely low - * 64. */ -#define FD_SETSIZE MAXCONNECTIONS -/* XXXX But Windows FD_SET and FD_CLR are tremendously ugly, and linear in - * the total number of sockets set! Perhaps we should eventually use - * WSAEventSelect and WSAWaitForMultipleEvents instead of select? */ -#endif - -#if defined(MS_WINDOWS) || ! defined(USE_FAKE_POLL) -/* If we're using poll, we can poll as many sockets as we want. - * If we're on Windows, having too many sockets is harmless, since - * select stupidly uses an array of sockets rather than a bitfield. */ -#define SOCKET_IS_POLLABLE(fd) ((fd) >= 0) -#else -/* If we're using a real Posix select, then in order to be pollable, a socket - * must - * a) be valid (>= 0) - * b) be < FD_SETSIZE. - */ -#define SOCKET_IS_POLLABLE(fd) ((fd) >= 0 && (fd) < FD_SETSIZE) -#endif - -int tor_poll(struct pollfd *ufds, unsigned int nfds, int timeout); - -#endif |