You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2010/10/06 19:10:29 UTC

svn commit: r1005153 - /incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp

Author: dreiss
Date: Wed Oct  6 17:10:29 2010
New Revision: 1005153

URL: http://svn.apache.org/viewvc?rev=1005153&view=rev
Log:
THRIFT-926. cpp: Don't sleep in TFileTransport if we have data to write

Previously, the TFileTransport writer thread behaved as follows:

  while true:
    wait for main thread to notify new data in enqueueBuffer_
    swap(enqueueBuffer_, dequeueBuffer_)
    write out everything in dequeueBuffer_

Now the behavior is:

  while true:
    if enqueueBuffer_ is empty
      wait for main thread to notify new data in enqueueBuffer_
    swap(enqueueBuffer_, dequeueBuffer_)
    write out everything in dequeueBuffer_

The old behavior had a couple problems:
- Writes that arrived while the writer thread was writing
  dequeueBuffer_ wouldn't get processed immediately.  The writer thread
  would always wait until another write occurred after it started its
  condition variable wait, or until it timed out (3 seconds by default).

- If the main thread was writing fast enough to fill up enqueueBuffer_
  while the writer thread was still writing out dequeueBuffer_, it would
  block the next write call until the writer thread swapped the buffers.
  Unfortunately, the writer thread waits to do this until it the main
  thread notifies it of another write.  This deadlock is only broken by
  the 3 second timeout.  Performance then tanks, since the writer thread
  now always sleeps 3 seconds each time around the loop.

Modified:
    incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp?rev=1005153&r1=1005152&r2=1005153&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp Wed Oct  6 17:10:29 2010
@@ -272,33 +272,37 @@ void TFileTransport::enqueueEvent(const 
 
 bool TFileTransport::swapEventBuffers(struct timespec* deadline) {
   pthread_mutex_lock(&mutex_);
-  if (deadline != NULL) {
-    // if we were handed a deadline time struct, do a timed wait
-    pthread_cond_timedwait(&notEmpty_, &mutex_, deadline);
+
+  bool swap;
+  if (!enqueueBuffer_->isEmpty()) {
+    swap = true;
   } else {
-    // just wait until the buffer gets an item
-    pthread_cond_wait(&notEmpty_, &mutex_);
-  }
+    if (deadline != NULL) {
+      // if we were handed a deadline time struct, do a timed wait
+      pthread_cond_timedwait(&notEmpty_, &mutex_, deadline);
+    } else {
+      // just wait until the buffer gets an item
+      pthread_cond_wait(&notEmpty_, &mutex_);
+    }
 
-  bool swapped = false;
+    // could be empty if we timed out
+    swap = enqueueBuffer_->isEmpty();
+  }
 
-  // could be empty if we timed out
-  if (!enqueueBuffer_->isEmpty()) {
+  if (swap) {
     TFileTransportBuffer *temp = enqueueBuffer_;
     enqueueBuffer_ = dequeueBuffer_;
     dequeueBuffer_ = temp;
-
-    swapped = true;
   }
 
   // unlock the mutex and signal if required
   pthread_mutex_unlock(&mutex_);
 
-  if (swapped) {
+  if (swap) {
     pthread_cond_signal(&notFull_);
   }
 
-  return swapped;
+  return swap;
 }