Author: roger
Date: Tue Jun 19 20:42:33 2012
New Revision: 1351845
URL: http://svn.apache.org/viewvc?rev=1351845&view=rev
Log:
THRIFT-1626 concurrency::Mutex timedlock fix and lesser improvements
Patch: Andrew Majorov
Modified:
thrift/trunk/lib/cpp/src/thrift/concurrency/Mutex.cpp
thrift/trunk/lib/cpp/src/thrift/protocol/TProtocol.h
thrift/trunk/lib/cpp/src/thrift/transport/TSocket.h
thrift/trunk/lib/cpp/src/thrift/transport/TTransportException.cpp
Modified: thrift/trunk/lib/cpp/src/thrift/concurrency/Mutex.cpp
URL:
http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/thrift/concurrency/Mutex.cpp?rev=1351845&r1=1351844&r2=1351845&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/thrift/concurrency/Mutex.cpp (original)
+++ thrift/trunk/lib/cpp/src/thrift/concurrency/Mutex.cpp Tue Jun 19 20:42:33
2012
@@ -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
}
Modified: thrift/trunk/lib/cpp/src/thrift/protocol/TProtocol.h
URL:
http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/thrift/protocol/TProtocol.h?rev=1351845&r1=1351844&r2=1351845&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/thrift/protocol/TProtocol.h (original)
+++ thrift/trunk/lib/cpp/src/thrift/protocol/TProtocol.h Tue Jun 19 20:42:33
2012
@@ -83,26 +83,26 @@ using apache::thrift::transport::TTransp
#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 @@ using apache::thrift::transport::TTransp
# 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::TTransp
# 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
Modified: thrift/trunk/lib/cpp/src/thrift/transport/TSocket.h
URL:
http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/thrift/transport/TSocket.h?rev=1351845&r1=1351844&r2=1351845&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/thrift/transport/TSocket.h (original)
+++ thrift/trunk/lib/cpp/src/thrift/transport/TSocket.h Tue Jun 19 20:42:33 2012
@@ -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 {
/**
Modified: thrift/trunk/lib/cpp/src/thrift/transport/TTransportException.cpp
URL:
http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/thrift/transport/TTransportException.cpp?rev=1351845&r1=1351844&r2=1351845&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/thrift/transport/TTransportException.cpp (original)
+++ thrift/trunk/lib/cpp/src/thrift/transport/TTransportException.cpp Tue Jun
19 20:42:33 2012
@@ -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;