Rebase webrtc/base 6163:6216 (svn diff -r 6163:6216 http://webrtc.googlecode.com/svn/trunk/talk/base, apply diff manually)

BUG=3379
TBR=wu@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/17619004

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@6217 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/base/messagequeue.cc b/base/messagequeue.cc
index bf31a05..1b312ff 100644
--- a/base/messagequeue.cc
+++ b/base/messagequeue.cc
@@ -109,7 +109,7 @@
 // MessageQueue
 
 MessageQueue::MessageQueue(SocketServer* ss)
-    : ss_(ss), fStop_(false), fPeekKeep_(false), active_(false),
+    : ss_(ss), fStop_(false), fPeekKeep_(false),
       dmsgq_next_num_(0) {
   if (!ss_) {
     // Currently, MessageQueue holds a socket server, and is the base class for
@@ -121,6 +121,7 @@
     ss_ = default_ss_.get();
   }
   ss_->SetMessageQueue(this);
+  MessageQueueManager::Add(this);
 }
 
 MessageQueue::~MessageQueue() {
@@ -128,10 +129,8 @@
   // that it always gets called when the queue
   // is going away.
   SignalQueueDestroyed();
-  if (active_) {
-    MessageQueueManager::Remove(this);
-    Clear(NULL);
-  }
+  MessageQueueManager::Remove(this);
+  Clear(NULL);
   if (ss_) {
     ss_->SetMessageQueue(NULL);
   }
@@ -279,7 +278,6 @@
   // Signal for the multiplexer to return
 
   CritScope cs(&crit_);
-  EnsureActive();
   Message msg;
   msg.phandler = phandler;
   msg.message_id = id;
@@ -301,7 +299,6 @@
   // Signal for the multiplexer to return.
 
   CritScope cs(&crit_);
-  EnsureActive();
   Message msg;
   msg.phandler = phandler;
   msg.message_id = id;
@@ -384,12 +381,4 @@
   pmsg->phandler->OnMessage(pmsg);
 }
 
-void MessageQueue::EnsureActive() {
-  ASSERT(crit_.CurrentThreadIsOwner());
-  if (!active_) {
-    active_ = true;
-    MessageQueueManager::Add(this);
-  }
-}
-
 }  // namespace rtc
diff --git a/base/messagequeue.h b/base/messagequeue.h
index 958a39e..41c1e24 100644
--- a/base/messagequeue.h
+++ b/base/messagequeue.h
@@ -58,7 +58,7 @@
   void ClearInternal(MessageHandler *handler);
 
   static MessageQueueManager* instance_;
-  // This list contains 'active' MessageQueues.
+  // This list contains all live MessageQueues.
   std::vector<MessageQueue *> message_queues_;
   CriticalSection crit_;
 };
@@ -230,7 +230,6 @@
     void reheap() { make_heap(c.begin(), c.end(), comp); }
   };
 
-  void EnsureActive();
   void DoDelayPost(int cmsDelay, uint32 tstamp, MessageHandler *phandler,
                    uint32 id, MessageData* pdata);
 
@@ -241,9 +240,6 @@
   bool fStop_;
   bool fPeekKeep_;
   Message msgPeek_;
-  // A message queue is active if it has ever had a message posted to it.
-  // This also corresponds to being in MessageQueueManager's global list.
-  bool active_;
   MessageList msgq_;
   PriorityQueue dmsgq_;
   uint32 dmsgq_next_num_;
diff --git a/base/thread.cc b/base/thread.cc
index 0e4f0f3..3963b38 100644
--- a/base/thread.cc
+++ b/base/thread.cc
@@ -140,8 +140,7 @@
 
 Thread::~Thread() {
   Stop();
-  if (active_)
-    Clear(NULL);
+  Clear(NULL);
 }
 
 bool Thread::SleepMs(int milliseconds) {
@@ -386,7 +385,6 @@
   bool ready = false;
   {
     CritScope cs(&crit_);
-    EnsureActive();
     _SendMessage smsg;
     smsg.thread = current_thread;
     smsg.msg = msg;
diff --git a/base/timeutils.cc b/base/timeutils.cc
index aefa285..dcf83e3 100644
--- a/base/timeutils.cc
+++ b/base/timeutils.cc
@@ -186,4 +186,18 @@
 #endif
 }
 
+TimestampWrapAroundHandler::TimestampWrapAroundHandler()
+    : last_ts_(0), num_wrap_(0) {}
+
+int64 TimestampWrapAroundHandler::Unwrap(uint32 ts) {
+  if (ts < last_ts_) {
+    if (last_ts_ > 0xf0000000 && ts < 0x0fffffff) {
+      ++num_wrap_;
+    }
+  }
+  last_ts_ = ts;
+  int64_t unwrapped_ts = ts + (num_wrap_ << 32);
+  return unwrapped_ts;
+}
+
 } // namespace rtc
diff --git a/base/timeutils.h b/base/timeutils.h
index bdf73cc..ca041a7 100644
--- a/base/timeutils.h
+++ b/base/timeutils.h
@@ -80,6 +80,17 @@
   return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs;
 }
 
+class TimestampWrapAroundHandler {
+ public:
+  TimestampWrapAroundHandler();
+
+  int64 Unwrap(uint32 ts);
+
+ private:
+  uint32 last_ts_;
+  int64 num_wrap_;
+};
+
 }  // namespace rtc
 
 #endif  // WEBRTC_BASE_TIMEUTILS_H_
diff --git a/base/timeutils_unittest.cc b/base/timeutils_unittest.cc
index 86a1817..087fb0c 100644
--- a/base/timeutils_unittest.cc
+++ b/base/timeutils_unittest.cc
@@ -143,4 +143,27 @@
   EXPECT_TRUE(0 <= microseconds && microseconds < 1000000);
 }
 
+class TimestampWrapAroundHandlerTest : public testing::Test {
+ public:
+  TimestampWrapAroundHandlerTest() {}
+
+ protected:
+  TimestampWrapAroundHandler wraparound_handler_;
+};
+
+TEST_F(TimestampWrapAroundHandlerTest, Unwrap) {
+  uint32 ts = 0xfffffff2;
+  int64 unwrapped_ts = ts;
+  EXPECT_EQ(ts, wraparound_handler_.Unwrap(ts));
+  ts = 2;
+  unwrapped_ts += 0x10;
+  EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts));
+  ts = 0xfffffff2;
+  unwrapped_ts += 0xfffffff0;
+  EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts));
+  ts = 0;
+  unwrapped_ts += 0xe;
+  EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts));
+}
+
 }  // namespace rtc