Skip to content

Commit

Permalink
Bug 1336855 - Use shared_ptr for timers, r=franziskus
Browse files Browse the repository at this point in the history
--HG--
extra : rebase_source : 4cec5ea61f3777ad2ced42325ff27965940ee3d5
extra : histedit_source : c499f1fcda9c1c47ed0d3a483de456a3d7d86560
  • Loading branch information
martinthomson committed Feb 6, 2017
1 parent 602249e commit 874d863
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gtests/ssl_gtest/gtest_utils.h
Expand Up @@ -34,7 +34,7 @@ class Timeout : public PollTarget {
bool timed_out() const { return !handle_; }

private:
Poller::Timer* handle_;
std::shared_ptr<Poller::Timer> handle_;
};

} // namespace nss_test
Expand Down
2 changes: 1 addition & 1 deletion gtests/ssl_gtest/ssl_auth_unittest.cc
Expand Up @@ -517,7 +517,7 @@ static void TriggerAuthComplete(PollTarget* target, Event event) {
TEST_F(TlsConnectDatagram13, AuthCompleteAfterFinished) {
client_->SetAuthCertificateCallback(
[this](TlsAgent*, PRBool, PRBool) -> SECStatus {
Poller::Timer* timer_handle;
std::shared_ptr<Poller::Timer> timer_handle;
// This is really just to unroll the stack.
Poller::Instance()->SetTimer(1U, client_.get(), TriggerAuthComplete,
&timer_handle);
Expand Down
17 changes: 4 additions & 13 deletions gtests/ssl_gtest/test_io.cc
Expand Up @@ -402,14 +402,6 @@ void Poller::Shutdown() {
instance = nullptr;
}

Poller::~Poller() {
while (!timers_.empty()) {
Timer *timer = timers_.top();
timers_.pop();
delete timer;
}
}

void Poller::Wait(Event event, std::shared_ptr<DummyPrSocket> &adapter,
PollTarget *target, PollCallback cb) {
assert(event < TIMER_EVENT);
Expand Down Expand Up @@ -447,8 +439,8 @@ void Poller::Cancel(Event event, std::shared_ptr<DummyPrSocket> &adapter) {
}

void Poller::SetTimer(uint32_t timer_ms, PollTarget *target, PollCallback cb,
Timer **timer) {
Timer *t = new Timer(PR_Now() + timer_ms * 1000, target, cb);
std::shared_ptr<Timer> *timer) {
auto t = std::make_shared<Timer>(PR_Now() + timer_ms * 1000, target, cb);
timers_.push(t);
if (timer) *timer = t;
}
Expand All @@ -464,7 +456,7 @@ bool Poller::Poll() {

// Figure out the timer for the select.
if (!timers_.empty()) {
Timer *first_timer = timers_.top();
auto first_timer = timers_.top();
if (now >= first_timer->deadline_) {
// Timer expired.
timeout = PR_INTERVAL_NO_WAIT;
Expand Down Expand Up @@ -504,12 +496,11 @@ bool Poller::Poll() {
while (!timers_.empty()) {
if (now < timers_.top()->deadline_) break;

Timer *timer = timers_.top();
auto timer = timers_.top();
timers_.pop();
if (timer->callback_) {
timer->callback_(timer->target_, TIMER_EVENT);
}
delete timer;
}

return true;
Expand Down
11 changes: 7 additions & 4 deletions gtests/ssl_gtest/test_io.h
Expand Up @@ -115,12 +115,12 @@ class Poller {
PollTarget* target, PollCallback cb);
void Cancel(Event event, std::shared_ptr<DummyPrSocket>& adapter);
void SetTimer(uint32_t timer_ms, PollTarget* target, PollCallback cb,
Timer** handle);
std::shared_ptr<Timer>* handle);
bool Poll();

private:
Poller() : waiters_(), timers_() {}
~Poller();
~Poller() {}

class Waiter {
public:
Expand All @@ -137,14 +137,17 @@ class Poller {

class TimerComparator {
public:
bool operator()(const Timer* lhs, const Timer* rhs) {
bool operator()(const std::shared_ptr<Timer> lhs,
const std::shared_ptr<Timer> rhs) {
return lhs->deadline_ > rhs->deadline_;
}
};

static Poller* instance;
std::map<std::shared_ptr<DummyPrSocket>, std::unique_ptr<Waiter>> waiters_;
std::priority_queue<Timer*, std::vector<Timer*>, TimerComparator> timers_;
std::priority_queue<std::shared_ptr<Timer>,
std::vector<std::shared_ptr<Timer>>, TimerComparator>
timers_;
};

} // end of namespace
Expand Down
2 changes: 1 addition & 1 deletion gtests/ssl_gtest/tls_agent.h
Expand Up @@ -347,7 +347,7 @@ class TlsAgent : public PollTarget {
std::shared_ptr<DummyPrSocket> adapter_;
ScopedPRFileDesc ssl_fd_;
State state_;
Poller::Timer* timer_handle_;
std::shared_ptr<Poller::Timer> timer_handle_;
bool falsestart_enabled_;
uint16_t expected_version_;
uint16_t expected_cipher_suite_;
Expand Down

0 comments on commit 874d863

Please sign in to comment.