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@6217 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrike@webrtc.org
2014-05-21 20:42:17 +00:00
parent f9f1bfbdae
commit 99b4162ccf
6 changed files with 54 additions and 23 deletions

View File

@ -109,7 +109,7 @@ void MessageQueueManager::ClearInternal(MessageHandler *handler) {
// MessageQueue // MessageQueue
MessageQueue::MessageQueue(SocketServer* ss) MessageQueue::MessageQueue(SocketServer* ss)
: ss_(ss), fStop_(false), fPeekKeep_(false), active_(false), : ss_(ss), fStop_(false), fPeekKeep_(false),
dmsgq_next_num_(0) { dmsgq_next_num_(0) {
if (!ss_) { if (!ss_) {
// Currently, MessageQueue holds a socket server, and is the base class for // Currently, MessageQueue holds a socket server, and is the base class for
@ -121,6 +121,7 @@ MessageQueue::MessageQueue(SocketServer* ss)
ss_ = default_ss_.get(); ss_ = default_ss_.get();
} }
ss_->SetMessageQueue(this); ss_->SetMessageQueue(this);
MessageQueueManager::Add(this);
} }
MessageQueue::~MessageQueue() { MessageQueue::~MessageQueue() {
@ -128,10 +129,8 @@ MessageQueue::~MessageQueue() {
// that it always gets called when the queue // that it always gets called when the queue
// is going away. // is going away.
SignalQueueDestroyed(); SignalQueueDestroyed();
if (active_) {
MessageQueueManager::Remove(this); MessageQueueManager::Remove(this);
Clear(NULL); Clear(NULL);
}
if (ss_) { if (ss_) {
ss_->SetMessageQueue(NULL); ss_->SetMessageQueue(NULL);
} }
@ -279,7 +278,6 @@ void MessageQueue::Post(MessageHandler *phandler, uint32 id,
// Signal for the multiplexer to return // Signal for the multiplexer to return
CritScope cs(&crit_); CritScope cs(&crit_);
EnsureActive();
Message msg; Message msg;
msg.phandler = phandler; msg.phandler = phandler;
msg.message_id = id; msg.message_id = id;
@ -301,7 +299,6 @@ void MessageQueue::DoDelayPost(int cmsDelay, uint32 tstamp,
// Signal for the multiplexer to return. // Signal for the multiplexer to return.
CritScope cs(&crit_); CritScope cs(&crit_);
EnsureActive();
Message msg; Message msg;
msg.phandler = phandler; msg.phandler = phandler;
msg.message_id = id; msg.message_id = id;
@ -384,12 +381,4 @@ void MessageQueue::Dispatch(Message *pmsg) {
pmsg->phandler->OnMessage(pmsg); pmsg->phandler->OnMessage(pmsg);
} }
void MessageQueue::EnsureActive() {
ASSERT(crit_.CurrentThreadIsOwner());
if (!active_) {
active_ = true;
MessageQueueManager::Add(this);
}
}
} // namespace rtc } // namespace rtc

View File

@ -58,7 +58,7 @@ class MessageQueueManager {
void ClearInternal(MessageHandler *handler); void ClearInternal(MessageHandler *handler);
static MessageQueueManager* instance_; static MessageQueueManager* instance_;
// This list contains 'active' MessageQueues. // This list contains all live MessageQueues.
std::vector<MessageQueue *> message_queues_; std::vector<MessageQueue *> message_queues_;
CriticalSection crit_; CriticalSection crit_;
}; };
@ -230,7 +230,6 @@ class MessageQueue {
void reheap() { make_heap(c.begin(), c.end(), comp); } void reheap() { make_heap(c.begin(), c.end(), comp); }
}; };
void EnsureActive();
void DoDelayPost(int cmsDelay, uint32 tstamp, MessageHandler *phandler, void DoDelayPost(int cmsDelay, uint32 tstamp, MessageHandler *phandler,
uint32 id, MessageData* pdata); uint32 id, MessageData* pdata);
@ -241,9 +240,6 @@ class MessageQueue {
bool fStop_; bool fStop_;
bool fPeekKeep_; bool fPeekKeep_;
Message msgPeek_; 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_; MessageList msgq_;
PriorityQueue dmsgq_; PriorityQueue dmsgq_;
uint32 dmsgq_next_num_; uint32 dmsgq_next_num_;

View File

@ -140,7 +140,6 @@ Thread::Thread(SocketServer* ss)
Thread::~Thread() { Thread::~Thread() {
Stop(); Stop();
if (active_)
Clear(NULL); Clear(NULL);
} }
@ -386,7 +385,6 @@ void Thread::Send(MessageHandler *phandler, uint32 id, MessageData *pdata) {
bool ready = false; bool ready = false;
{ {
CritScope cs(&crit_); CritScope cs(&crit_);
EnsureActive();
_SendMessage smsg; _SendMessage smsg;
smsg.thread = current_thread; smsg.thread = current_thread;
smsg.msg = msg; smsg.msg = msg;

View File

@ -186,4 +186,18 @@ int32 TimeDiff(uint32 later, uint32 earlier) {
#endif #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 } // namespace rtc

View File

@ -80,6 +80,17 @@ inline int64 UnixTimestampNanosecsToNtpMillisecs(int64 unix_ts_ns) {
return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs;
} }
class TimestampWrapAroundHandler {
public:
TimestampWrapAroundHandler();
int64 Unwrap(uint32 ts);
private:
uint32 last_ts_;
int64 num_wrap_;
};
} // namespace rtc } // namespace rtc
#endif // WEBRTC_BASE_TIMEUTILS_H_ #endif // WEBRTC_BASE_TIMEUTILS_H_

View File

@ -143,4 +143,27 @@ TEST(TimeTest, DISABLED_CurrentTmTime) {
EXPECT_TRUE(0 <= microseconds && microseconds < 1000000); 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 } // namespace rtc