Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Add support to configure.ac for checking thread local support
Browse files Browse the repository at this point in the history
and also check IPADDR_LOOPBACK is defined. In test use
loopback if we can to prevent firewall issues and use correct
thread local qualifier in sever. Plus ensure server.h qualifies
calls to std::bind to prevent clash with C socket API.
  • Loading branch information
Martin2112 committed Aug 14, 2015
1 parent a4bd53a commit be0abe9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Expand Up @@ -135,6 +135,9 @@ AC_TYPE_UINT32_T
AC_TYPE_UINT64_T AC_TYPE_UINT64_T
AC_TYPE_UINT8_T AC_TYPE_UINT8_T


CT_CHECK_TLS
AC_CHECK_DECLS([INADDR_LOOPBACK], [], [], [#include <netinet/in.h>])

AC_MSG_CHECKING([whether pthread_t is a pointer]) AC_MSG_CHECKING([whether pthread_t is a pointer])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#include <pthread.h> #include <pthread.h>
Expand Down
18 changes: 17 additions & 1 deletion cpp/net/url_fetcher_test.cc
@@ -1,3 +1,5 @@
#include "config.h"

#include <csignal> #include <csignal>
#include <fcntl.h> #include <fcntl.h>
#include <gflags/gflags.h> #include <gflags/gflags.h>
Expand All @@ -7,6 +9,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_VFORK_H
#include <vfork.h>
#endif


#include "net/connection_pool.h" #include "net/connection_pool.h"
#include "net/url_fetcher.h" #include "net/url_fetcher.h"
Expand Down Expand Up @@ -57,7 +62,12 @@ class LocalhostResolver : public libevent::Base::Resolver {
pid_t RunOpenSSLServer(uint16_t port, const std::string& cert_file, pid_t RunOpenSSLServer(uint16_t port, const std::string& cert_file,
const std::string& key_file, const std::string& key_file,
const std::string& mode = "-www") { const std::string& mode = "-www") {
#ifdef HAVE_WORKING_VFORK
pid_t pid(vfork());
#else
pid_t pid(fork()); pid_t pid(fork());
#endif

if (pid == -1) { if (pid == -1) {
LOG(INFO) << "fork() failed: " << pid; LOG(INFO) << "fork() failed: " << pid;
} else if (pid == 0) { } else if (pid == 0) {
Expand Down Expand Up @@ -315,7 +325,13 @@ int main(int argc, char** argv) {
struct sockaddr_in hang_addr; struct sockaddr_in hang_addr;
bzero((char*)&hang_addr, sizeof(hang_addr)); bzero((char*)&hang_addr, sizeof(hang_addr));
hang_addr.sin_family = AF_INET; hang_addr.sin_family = AF_INET;
hang_addr.sin_addr.s_addr = INADDR_ANY; // Prefer to use INADDR_LOOPBACK if available as it avoids the firewall
// triggering on some platforms if we bind a non-local address.
#ifdef HAVE_INADDR_LOOPBACK
hang_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
#else
hang_addr.sin_addr.s_addr = htonl(INADDR_ANY);
#endif
hang_addr.sin_port = htons(cert_trans::kHangPort); hang_addr.sin_port = htons(cert_trans::kHangPort);
CHECK_EQ(0, bind(hang_fd, reinterpret_cast<struct sockaddr*>(&hang_addr), CHECK_EQ(0, bind(hang_fd, reinterpret_cast<struct sockaddr*>(&hang_addr),
sizeof(hang_addr))); sizeof(hang_addr)));
Expand Down
2 changes: 1 addition & 1 deletion cpp/server/event.cc
Expand Up @@ -251,7 +251,7 @@ bool Services::InitServer(int* sock, int port, const char* ip, int type) {
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons((unsigned short)port); server.sin_port = htons((unsigned short)port);
if (ip == NULL) if (ip == NULL)
server.sin_addr.s_addr = INADDR_ANY; server.sin_addr.s_addr = htonl(INADDR_ANY);
else else
memcpy(&server.sin_addr.s_addr, ip, 4); memcpy(&server.sin_addr.s_addr, ip, 4);


Expand Down
2 changes: 2 additions & 0 deletions cpp/server/server.h
Expand Up @@ -36,6 +36,8 @@
#include "util/thread_pool.h" #include "util/thread_pool.h"
#include "util/uuid.h" #include "util/uuid.h"


using std::bind;

DEFINE_int32(node_state_refresh_seconds, 10, DEFINE_int32(node_state_refresh_seconds, 10,
"How often to refresh the ClusterNodeState entry for this node."); "How often to refresh the ClusterNodeState entry for this node.");
DEFINE_int32(watchdog_seconds, 120, DEFINE_int32(watchdog_seconds, 120,
Expand Down
37 changes: 37 additions & 0 deletions m4/ct_check_tls.m4
@@ -0,0 +1,37 @@
dnl Checks for thread-local storage support.
dnl
dnl Taken from the openvswitch config code (Apache 2.0 License)
dnl with some local modifications. Does not include <threads.h>
dnl as this does not currently exist on GCC.
dnl Checks whether the compiler and linker support the C11
dnl thread_local macro from <threads.h>, and if so defines
dnl HAVE_THREAD_LOCAL. If not, checks whether the compiler and linker
dnl support the GCC __thread extension, and if so defines
dnl HAVE___THREAD.
AC_DEFUN([CT_CHECK_TLS],
[AC_CACHE_CHECK(
[whether $CC has <threads.h> that supports thread_local],
[ct_cv_thread_local],
[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([ static thread_local int var;], [return var;])],
[ct_cv_thread_local=yes],
[ct_cv_thread_local=no])])
if test $ct_cv_thread_local = yes; then
AC_DEFINE([HAVE_THREAD_LOCAL], [1],
[Define to 1 if the C compiler and linker supports the C11
thread_local macro defined in <threads.h>.])
else
AC_CACHE_CHECK(
[whether $CC supports __thread],
[ct_cv___thread],
[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([static __thread int var;], [return var;])],
[ct_cv___thread=yes],
[ct_cv___thread=no])])
if test $ct_cv___thread = yes; then
AC_DEFINE([HAVE___THREAD], [1],
[Define to 1 if the C compiler and linker supports the
GCC __thread extensions.])
fi
fi])

0 comments on commit be0abe9

Please sign in to comment.