Test a little more of compat_threads.c

This commit is contained in:
Nick Mathewson 2013-09-25 14:31:59 -04:00
parent 3868b5d210
commit c51f7c23e3
5 changed files with 38 additions and 16 deletions

View File

@ -155,19 +155,18 @@ sock_drain(tor_socket_t fd)
/** Allocate a new set of alert sockets, and set the appropriate function /** Allocate a new set of alert sockets, and set the appropriate function
* pointers, in <b>socks_out</b>. */ * pointers, in <b>socks_out</b>. */
int int
alert_sockets_create(alert_sockets_t *socks_out) alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
{ {
tor_socket_t socks[2]; tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
#ifdef HAVE_EVENTFD #ifdef HAVE_EVENTFD
/* First, we try the Linux eventfd() syscall. This gives a 64-bit counter /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
* associated with a single file descriptor. */ * associated with a single file descriptor. */
#if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK) #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK); if (!(flags & ASOCKS_NOEVENTFD2))
#else socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
socks[0] = -1;
#endif #endif
if (socks[0] < 0) { if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
socks[0] = eventfd(0,0); socks[0] = eventfd(0,0);
if (socks[0] >= 0) { if (socks[0] >= 0) {
if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 || if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
@ -188,7 +187,8 @@ alert_sockets_create(alert_sockets_t *socks_out)
#ifdef HAVE_PIPE2 #ifdef HAVE_PIPE2
/* Now we're going to try pipes. First type the pipe2() syscall, if we /* Now we're going to try pipes. First type the pipe2() syscall, if we
* have it, so we can save some calls... */ * have it, so we can save some calls... */
if (pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) { if (!(flags & ASOCKS_NOPIPE2) &&
pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
socks_out->read_fd = socks[0]; socks_out->read_fd = socks[0];
socks_out->write_fd = socks[1]; socks_out->write_fd = socks[1];
socks_out->alert_fn = pipe_alert; socks_out->alert_fn = pipe_alert;
@ -200,7 +200,8 @@ alert_sockets_create(alert_sockets_t *socks_out)
#ifdef HAVE_PIPE #ifdef HAVE_PIPE
/* Now try the regular pipe() syscall. Pipes have a bit lower overhead than /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
* socketpairs, fwict. */ * socketpairs, fwict. */
if (pipe(socks) == 0) { if (!(flags & ASOCKS_NOPIPE) &&
pipe(socks) == 0) {
if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 || if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 || fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
set_socket_nonblocking(socks[0]) < 0 || set_socket_nonblocking(socks[0]) < 0 ||
@ -218,7 +219,8 @@ alert_sockets_create(alert_sockets_t *socks_out)
#endif #endif
/* If nothing else worked, fall back on socketpair(). */ /* If nothing else worked, fall back on socketpair(). */
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) { if (!(flags & ASOCKS_NOSOCKETPAIR) &&
tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
if (set_socket_nonblocking(socks[0]) < 0 || if (set_socket_nonblocking(socks[0]) < 0 ||
set_socket_nonblocking(socks[1])) { set_socket_nonblocking(socks[1])) {
tor_close_socket(socks[0]); tor_close_socket(socks[0]);

View File

@ -102,7 +102,14 @@ typedef struct alert_sockets_s {
int (*drain_fn)(tor_socket_t read_fd); int (*drain_fn)(tor_socket_t read_fd);
} alert_sockets_t; } alert_sockets_t;
int alert_sockets_create(alert_sockets_t *socks_out); /* Flags to disable one or more alert_sockets backends. */
#define ASOCKS_NOEVENTFD2 (1u<<0)
#define ASOCKS_NOEVENTFD (1u<<1)
#define ASOCKS_NOPIPE2 (1u<<2)
#define ASOCKS_NOPIPE (1u<<3)
#define ASOCKS_NOSOCKETPAIR (1u<<4)
int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags);
void alert_sockets_close(alert_sockets_t *socks); void alert_sockets_close(alert_sockets_t *socks);
#endif #endif

View File

@ -397,12 +397,12 @@ threadpool_get_replyqueue(threadpool_t *tp)
* IO-centric event loop, it needs to get woken up with means other than a * IO-centric event loop, it needs to get woken up with means other than a
* condition variable. */ * condition variable. */
replyqueue_t * replyqueue_t *
replyqueue_new(void) replyqueue_new(uint32_t alertsocks_flags)
{ {
replyqueue_t *rq; replyqueue_t *rq;
rq = tor_malloc_zero(sizeof(replyqueue_t)); rq = tor_malloc_zero(sizeof(replyqueue_t));
if (alert_sockets_create(&rq->alert) < 0) { if (alert_sockets_create(&rq->alert, alertsocks_flags) < 0) {
tor_free(rq); tor_free(rq);
return NULL; return NULL;
} }

View File

@ -40,7 +40,7 @@ threadpool_t *threadpool_new(int n_threads,
void *arg); void *arg);
replyqueue_t *threadpool_get_replyqueue(threadpool_t *tp); replyqueue_t *threadpool_get_replyqueue(threadpool_t *tp);
replyqueue_t *replyqueue_new(void); replyqueue_t *replyqueue_new(uint32_t alertsocks_flags);
tor_socket_t replyqueue_get_socket(replyqueue_t *rq); tor_socket_t replyqueue_get_socket(replyqueue_t *rq);
void replyqueue_process(replyqueue_t *queue); void replyqueue_process(replyqueue_t *queue);

View File

@ -249,8 +249,10 @@ help(void)
" -N <items> Run this many items of work\n" " -N <items> Run this many items of work\n"
" -T <threads> Use this many threads\n" " -T <threads> Use this many threads\n"
" -I <inflight> Have no more than this many requests queued at once\n" " -I <inflight> Have no more than this many requests queued at once\n"
" -L <lowwater> Add items whenever fewer than this many are pending.\n" " -L <lowwater> Add items whenever fewer than this many are pending\n"
" -R <ratio> Make one out of this many items be a slow (RSA) one"); " -R <ratio> Make one out of this many items be a slow (RSA) one\n"
" --no-{eventfd2,eventfd,pipe2,pipe,socketpair}\n"
" Disable one of the alert_socket backends.");
} }
int int
@ -261,6 +263,7 @@ main(int argc, char **argv)
int i; int i;
tor_libevent_cfg evcfg; tor_libevent_cfg evcfg;
struct event *ev; struct event *ev;
uint32_t as_flags = 0;
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-v")) { if (!strcmp(argv[i], "-v")) {
@ -275,6 +278,16 @@ main(int argc, char **argv)
opt_n_lowwater = atoi(argv[++i]); opt_n_lowwater = atoi(argv[++i]);
} else if (!strcmp(argv[i], "-R") && i+1<argc) { } else if (!strcmp(argv[i], "-R") && i+1<argc) {
opt_ratio_rsa = atoi(argv[++i]); opt_ratio_rsa = atoi(argv[++i]);
} else if (!strcmp(argv[i], "--no-eventfd2")) {
as_flags |= ASOCKS_NOEVENTFD2;
} else if (!strcmp(argv[i], "--no-eventfd")) {
as_flags |= ASOCKS_NOEVENTFD;
} else if (!strcmp(argv[i], "--no-pipe2")) {
as_flags |= ASOCKS_NOPIPE2;
} else if (!strcmp(argv[i], "--no-pipe")) {
as_flags |= ASOCKS_NOPIPE;
} else if (!strcmp(argv[i], "--no-socketpair")) {
as_flags |= ASOCKS_NOSOCKETPAIR;
} else if (!strcmp(argv[i], "-h")) { } else if (!strcmp(argv[i], "-h")) {
help(); help();
return 0; return 0;
@ -294,7 +307,7 @@ main(int argc, char **argv)
crypto_global_init(1, NULL, NULL); crypto_global_init(1, NULL, NULL);
crypto_seed_rng(1); crypto_seed_rng(1);
rq = replyqueue_new(); rq = replyqueue_new(as_flags);
tor_assert(rq); tor_assert(rq);
tp = threadpool_new(opt_n_threads, tp = threadpool_new(opt_n_threads,
rq, new_state, free_state, NULL); rq, new_state, free_state, NULL);