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
diff --git a/lib/cpp/src/thrift/concurrency/Mutex.cpp b/lib/cpp/src/thrift/concurrency/Mutex.cpp
index 4f3f323..601afa8 100644
--- a/lib/cpp/src/thrift/concurrency/Mutex.cpp
+++ b/lib/cpp/src/thrift/concurrency/Mutex.cpp
@@ -144,7 +144,7 @@
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 @@
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 f6802e6..77b018e 100644
--- a/lib/cpp/src/thrift/protocol/TProtocol.h
+++ b/lib/cpp/src/thrift/protocol/TProtocol.h
@@ -83,26 +83,26 @@
#include <sys/param.h>
#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 <boost/config.hpp>
# include <boost/detail/endian.hpp>
-# 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 @@
# 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 @@
# 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 9d07522..2357430 100644
--- a/lib/cpp/src/thrift/transport/TSocket.h
+++ b/lib/cpp/src/thrift/transport/TSocket.h
@@ -22,6 +22,10 @@
#include <string>
+#include "TTransport.h"
+#include "TVirtualTransport.h"
+#include "TServerSocket.h"
+
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -29,10 +33,6 @@
#include <netdb.h>
#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 e24198a..bd4f44b 100644
--- a/lib/cpp/src/thrift/transport/TTransportException.cpp
+++ b/lib/cpp/src/thrift/transport/TTransportException.cpp
@@ -20,7 +20,10 @@
#include <thrift/transport/TTransportException.h>
#include <boost/lexical_cast.hpp>
#include <cstring>
+
+#ifdef HAVE_CONFIG_H
#include <config.h>
+#endif
using std::string;
using boost::lexical_cast;