X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=helper_thread.c;h=7874985504be2aa6c4c82c7683320c39409d927d;hb=df8781b62a9d0dffae40d86916feff3659a5fb7e;hp=49cd4c75e68151363732a92de91b53f7790c4828;hpb=5d3a1a0dc53b73aeaddd3f6c9aab3336ca3ff0b9;p=fio.git diff --git a/helper_thread.c b/helper_thread.c index 49cd4c75..78749855 100644 --- a/helper_thread.c +++ b/helper_thread.c @@ -1,3 +1,4 @@ +#include #ifdef CONFIG_VALGRIND_DEV #include #else @@ -26,20 +27,71 @@ static struct helper_data { void helper_thread_destroy(void) { + if (!helper_data) + return; + close(helper_data->pipe[0]); close(helper_data->pipe[1]); sfree(helper_data); } +#ifdef _WIN32 +static void sock_init(void) +{ + WSADATA wsaData; + int res; + + /* It is allowed to call WSAStartup() more than once. */ + res = WSAStartup(MAKEWORD(2, 2), &wsaData); + assert(res == 0); +} + +static int make_nonblocking(int fd) +{ + unsigned long arg = 1; + + return ioctlsocket(fd, FIONBIO, &arg); +} + +static int write_to_pipe(int fd, const void *buf, size_t len) +{ + return send(fd, buf, len, 0); +} + +static int read_from_pipe(int fd, void *buf, size_t len) +{ + return recv(fd, buf, len, 0); +} +#else +static void sock_init(void) +{ +} + +static int make_nonblocking(int fd) +{ + return fcntl(fd, F_SETFL, O_NONBLOCK); +} + +static int write_to_pipe(int fd, const void *buf, size_t len) +{ + return write(fd, buf, len); +} + +static int read_from_pipe(int fd, void *buf, size_t len) +{ + return read(fd, buf, len); +} +#endif + static void submit_action(enum action a) { - const uint8_t data = a; + const char data = a; int ret; if (!helper_data) return; - ret = write(helper_data->pipe[1], &data, sizeof(data)); + ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data)); assert(ret == 1); } @@ -68,6 +120,9 @@ bool helper_should_exit(void) void helper_thread_exit(void) { + if (!helper_data) + return; + helper_data->exit = 1; submit_action(A_EXIT); pthread_join(helper_data->thread, NULL); @@ -78,11 +133,23 @@ static void *helper_thread_main(void *data) struct helper_data *hd = data; unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC; struct timespec ts, last_du, last_ss; - uint8_t action; + char action; int ret = 0; sk_out_assign(hd->sk_out); +#ifdef HAVE_PTHREAD_SIGMASK + { + sigset_t sigmask; + + /* Let another thread handle signals. */ + ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask); + assert(ret == 0); + ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL); + assert(ret == 0); + } +#endif + #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK clock_gettime(CLOCK_MONOTONIC, &ts); #else @@ -104,13 +171,17 @@ static void *helper_thread_main(void *data) timespec_add_msec(&ts, msec_to_next_event); - if (read(hd->pipe[0], &action, sizeof(action)) < 0) { + if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) { FD_ZERO(&rfds); FD_SET(hd->pipe[0], &rfds); FD_ZERO(&efds); FD_SET(hd->pipe[0], &efds); - select(1, &rfds, NULL, &efds, &timeout); - if (read(hd->pipe[0], &action, sizeof(action)) < 0) + ret = select(1, &rfds, NULL, &efds, &timeout); + if (ret < 0) + log_err("fio: select() call in helper thread failed: %s", + strerror(errno)); + if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < + 0) action = 0; } @@ -168,6 +239,54 @@ static void *helper_thread_main(void *data) return NULL; } +/* + * Connect two sockets to each other to emulate the pipe() system call on Windows. + */ +int pipe_over_loopback(int fd[2]) +{ + struct sockaddr_in addr = { .sin_family = AF_INET }; + socklen_t len = sizeof(addr); + int res; + + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + sock_init(); + + fd[0] = socket(AF_INET, SOCK_STREAM, 0); + if (fd[0] < 0) + goto err; + fd[1] = socket(AF_INET, SOCK_STREAM, 0); + if (fd[1] < 0) + goto close_fd_0; + res = bind(fd[0], (struct sockaddr *)&addr, len); + if (res < 0) + goto close_fd_1; + res = getsockname(fd[0], (struct sockaddr *)&addr, &len); + if (res < 0) + goto close_fd_1; + res = listen(fd[0], 1); + if (res < 0) + goto close_fd_1; + res = connect(fd[1], (struct sockaddr *)&addr, len); + if (res < 0) + goto close_fd_1; + res = accept(fd[0], NULL, NULL); + if (res < 0) + goto close_fd_1; + close(fd[0]); + fd[0] = res; + return 0; + +close_fd_1: + close(fd[1]); + +close_fd_0: + close(fd[0]); + +err: + return -1; +} + int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out) { struct helper_data *hd; @@ -180,15 +299,17 @@ int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out) hd->sk_out = sk_out; -#ifdef __linux__ +#if defined(CONFIG_PIPE2) ret = pipe2(hd->pipe, O_CLOEXEC); -#else +#elif defined(CONFIG_PIPE) ret = pipe(hd->pipe); +#else + ret = pipe_over_loopback(hd->pipe); #endif if (ret) return 1; - ret = fcntl(hd->pipe[0], F_SETFL, O_NONBLOCK); + ret = make_nonblocking(hd->pipe[0]); assert(ret >= 0); hd->startup_sem = startup_sem;