Loading src/common/util.c +40 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,13 @@ #ifdef HAVE_FCNTL_H #include <fcntl.h> #endif #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> #endif #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #include <assert.h> #include "util.h" #include "log.h" Loading Loading @@ -107,3 +114,36 @@ void set_socket_nonblocking(int socket) fcntl(socket, F_SETFL, O_NONBLOCK); #endif } int spawn_func(int (*func)(void *), void *data) { #ifdef _MSC_VER int rv; rv = _beginthread(func, 0, data); if (rv == (unsigned long) -1) return -1; return 0; #else pid_t pid; pid = fork(); if (pid<0) return -1; if (pid==0) { /* Child */ func(data); assert(0); /* Should never reach here. */ } else { /* Parent */ return 0; } #endif } void spawn_exit() { #ifdef _MSC_VER _endthread(); #else exit(0); #endif } src/common/util.h +7 −0 Original line number Diff line number Diff line Loading @@ -45,4 +45,11 @@ int tv_cmp(struct timeval *a, struct timeval *b); void set_socket_nonblocking(int socket); /* Minimalist interface to run a void function in the background. On unix calls fork, on win32 calls beginthread. Returns -1 on failure. func should not return, but rather should call spawn_exit. */ int spawn_func(int (*func)(void *), void *data); void spawn_exit(); #endif Loading
src/common/util.c +40 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,13 @@ #ifdef HAVE_FCNTL_H #include <fcntl.h> #endif #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> #endif #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #include <assert.h> #include "util.h" #include "log.h" Loading Loading @@ -107,3 +114,36 @@ void set_socket_nonblocking(int socket) fcntl(socket, F_SETFL, O_NONBLOCK); #endif } int spawn_func(int (*func)(void *), void *data) { #ifdef _MSC_VER int rv; rv = _beginthread(func, 0, data); if (rv == (unsigned long) -1) return -1; return 0; #else pid_t pid; pid = fork(); if (pid<0) return -1; if (pid==0) { /* Child */ func(data); assert(0); /* Should never reach here. */ } else { /* Parent */ return 0; } #endif } void spawn_exit() { #ifdef _MSC_VER _endthread(); #else exit(0); #endif }
src/common/util.h +7 −0 Original line number Diff line number Diff line Loading @@ -45,4 +45,11 @@ int tv_cmp(struct timeval *a, struct timeval *b); void set_socket_nonblocking(int socket); /* Minimalist interface to run a void function in the background. On unix calls fork, on win32 calls beginthread. Returns -1 on failure. func should not return, but rather should call spawn_exit. */ int spawn_func(int (*func)(void *), void *data); void spawn_exit(); #endif