From f9f841d76953b13153ca32b4c7e34c00776d16a8 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Tue, 19 Jun 2012 20:42:33 +0000 Subject: [PATCH] THRIFT-1626 concurrency::Mutex timedlock fix and lesser improvements Patch: Andrew Majorov git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1351845 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/thrift/concurrency/Mutex.cpp | 24 +++++++++++++++---- lib/cpp/src/thrift/protocol/TProtocol.h | 24 +++++++++---------- lib/cpp/src/thrift/transport/TSocket.h | 8 +++---- .../thrift/transport/TTransportException.cpp | 3 +++ 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/cpp/src/thrift/concurrency/Mutex.cpp b/lib/cpp/src/thrift/concurrency/Mutex.cpp index 4f3f3232..601afa80 100644 --- a/lib/cpp/src/thrift/concurrency/Mutex.cpp +++ b/lib/cpp/src/thrift/concurrency/Mutex.cpp @@ -144,7 +144,7 @@ class Mutex::impl { PROFILE_MUTEX_START_LOCK(); struct timespec ts; - Util::toTimespec(ts, milliseconds); + Util::toTimespec(ts, milliseconds + Util::currentTime()); int ret = pthread_mutex_timedlock(&pthread_mutex_, &ts); if (ret == 0) { PROFILE_MUTEX_LOCKED(); @@ -154,10 +154,24 @@ class Mutex::impl { PROFILE_MUTEX_NOT_LOCKED(); return false; #else - (void)milliseconds; - // If pthread_mutex_timedlock isn't supported, the safest thing to do - // is just do a nonblocking trylock. - return trylock(); + /* Otherwise follow solution used by Mono for Android */ + struct timespec sleepytime, now, to; + + /* This is just to avoid a completely busy wait */ + sleepytime.tv_sec = 0; + sleepytime.tv_nsec = 10000000L; /* 10ms */ + + Util::toTimespec(to, milliseconds + Util::currentTime()); + + while ((trylock()) == false) { + Util::toTimespec(now, Util::currentTime()); + if (now.tv_sec >= to.tv_sec && now.tv_nsec >= to.tv_nsec) { + return false; + } + nanosleep(&sleepytime, NULL); + } + + return true; #endif } diff --git a/lib/cpp/src/thrift/protocol/TProtocol.h b/lib/cpp/src/thrift/protocol/TProtocol.h index f6802e6c..77b018e3 100644 --- a/lib/cpp/src/thrift/protocol/TProtocol.h +++ b/lib/cpp/src/thrift/protocol/TProtocol.h @@ -83,26 +83,26 @@ using apache::thrift::transport::TTransport; #include #endif -#ifndef __BYTE_ORDER +#ifndef __THRIFT_BYTE_ORDER # if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# define __BYTE_ORDER BYTE_ORDER -# define __LITTLE_ENDIAN LITTLE_ENDIAN -# define __BIG_ENDIAN BIG_ENDIAN +# define __THRIFT_BYTE_ORDER BYTE_ORDER +# define __THRIFT_LITTLE_ENDIAN LITTLE_ENDIAN +# define __THRIFT_BIG_ENDIAN BIG_ENDIAN # else # include # include -# define __BYTE_ORDER BOOST_BYTE_ORDER +# define __THRIFT_BYTE_ORDER BOOST_BYTE_ORDER # ifdef BOOST_LITTLE_ENDIAN -# define __LITTLE_ENDIAN __BYTE_ORDER -# define __BIG_ENDIAN 0 +# define __THRIFT_LITTLE_ENDIAN __THRIFT_BYTE_ORDER +# define __THRIFT_BIG_ENDIAN 0 # else -# define __LITTLE_ENDIAN 0 -# define __BIG_ENDIAN __BYTE_ORDER +# define __THRIFT_LITTLE_ENDIAN 0 +# define __THRIFT_BIG_ENDIAN __THRIFT_BYTE_ORDER # endif # endif #endif -#if __BYTE_ORDER == __BIG_ENDIAN +#if __THRIFT_BYTE_ORDER == __THRIFT_BIG_ENDIAN # define ntohll(n) (n) # define htonll(n) (n) # if defined(__GNUC__) && defined(__GLIBC__) @@ -122,7 +122,7 @@ using apache::thrift::transport::TTransport; # define htolell(n) bswap_64(n) # define letohll(n) bswap_64(n) # endif /* GNUC & GLIBC */ -#elif __BYTE_ORDER == __LITTLE_ENDIAN +#elif __THRIFT_BYTE_ORDER == __THRIFT_LITTLE_ENDIAN # define htolell(n) (n) # define letohll(n) (n) # if defined(__GNUC__) && defined(__GLIBC__) @@ -133,7 +133,7 @@ using apache::thrift::transport::TTransport; # define ntohll(n) ( (((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32) ) # define htonll(n) ( (((uint64_t)htonl(n)) << 32) + htonl(n >> 32) ) # endif /* GNUC & GLIBC */ -#else /* __BYTE_ORDER */ +#else /* __THRIFT_BYTE_ORDER */ # error "Can't define htonll or ntohll!" #endif diff --git a/lib/cpp/src/thrift/transport/TSocket.h b/lib/cpp/src/thrift/transport/TSocket.h index 9d075225..2357430c 100644 --- a/lib/cpp/src/thrift/transport/TSocket.h +++ b/lib/cpp/src/thrift/transport/TSocket.h @@ -22,6 +22,10 @@ #include +#include "TTransport.h" +#include "TVirtualTransport.h" +#include "TServerSocket.h" + #ifdef HAVE_SYS_TIME_H #include #endif @@ -29,10 +33,6 @@ #include #endif -#include "TTransport.h" -#include "TVirtualTransport.h" -#include "TServerSocket.h" - namespace apache { namespace thrift { namespace transport { /** diff --git a/lib/cpp/src/thrift/transport/TTransportException.cpp b/lib/cpp/src/thrift/transport/TTransportException.cpp index e24198ae..bd4f44b3 100644 --- a/lib/cpp/src/thrift/transport/TTransportException.cpp +++ b/lib/cpp/src/thrift/transport/TTransportException.cpp @@ -20,7 +20,10 @@ #include #include #include + +#ifdef HAVE_CONFIG_H #include +#endif using std::string; using boost::lexical_cast; -- 2.17.1