Skip to content

Commit 1c432e3

Browse files
ALLReadyGoHengan-tao
authored
fix: Timer'id is not recycled in timerQueue (#130)
Co-authored-by: Heng <zh1477367741@163.com> Co-authored-by: an-tao <antao2002@gmail.com>
1 parent 7374d22 commit 1c432e3

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

trantor/net/inner/TimerQueue.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
using namespace trantor;
2929
#ifdef __linux__
30-
int createTimerfd()
30+
static int createTimerfd()
3131
{
3232
int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
3333
if (timerfd < 0)
@@ -37,7 +37,7 @@ int createTimerfd()
3737
return timerfd;
3838
}
3939

40-
struct timespec howMuchTimeFromNow(const TimePoint &when)
40+
static struct timespec howMuchTimeFromNow(const TimePoint &when)
4141
{
4242
auto microSeconds = std::chrono::duration_cast<std::chrono::microseconds>(
4343
when - std::chrono::steady_clock::now())
@@ -51,7 +51,7 @@ struct timespec howMuchTimeFromNow(const TimePoint &when)
5151
ts.tv_nsec = static_cast<long>((microSeconds % 1000000) * 1000);
5252
return ts;
5353
}
54-
void resetTimerfd(int timerfd, const TimePoint &expiration)
54+
static void resetTimerfd(int timerfd, const TimePoint &expiration)
5555
{
5656
// wake up loop by timerfd_settime()
5757
struct itimerspec newValue;
@@ -65,7 +65,7 @@ void resetTimerfd(int timerfd, const TimePoint &expiration)
6565
// LOG_SYSERR << "timerfd_settime()";
6666
}
6767
}
68-
void readTimerfd(int timerfd, const TimePoint &now)
68+
static void readTimerfd(int timerfd, const TimePoint &now)
6969
{
7070
uint64_t howmany;
7171
ssize_t n = ::read(timerfd, &howmany, sizeof howmany);
@@ -99,7 +99,7 @@ void TimerQueue::handleRead()
9999
reset(expired, now);
100100
}
101101
#else
102-
int64_t howMuchTimeFromNow(const TimePoint &when)
102+
static int64_t howMuchTimeFromNow(const TimePoint &when)
103103
{
104104
auto microSeconds = std::chrono::duration_cast<std::chrono::microseconds>(
105105
when - std::chrono::steady_clock::now())
@@ -270,11 +270,18 @@ void TimerQueue::reset(const std::vector<TimerPtr> &expired,
270270
loop_->assertInLoopThread();
271271
for (auto const &timerPtr : expired)
272272
{
273-
if (timerPtr->isRepeat() &&
274-
timerIdSet_.find(timerPtr->id()) != timerIdSet_.end())
273+
auto iter = timerIdSet_.find(timerPtr->id());
274+
if (iter != timerIdSet_.end())
275275
{
276-
timerPtr->restart(now);
277-
insert(timerPtr);
276+
if (timerPtr->isRepeat())
277+
{
278+
timerPtr->restart(now);
279+
insert(timerPtr);
280+
}
281+
else
282+
{
283+
timerIdSet_.erase(iter);
284+
}
278285
}
279286
}
280287
#ifdef __linux__

trantor/net/inner/TimerQueue.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace trantor
2727
class EventLoop;
2828
class Channel;
2929
using TimerPtr = std::shared_ptr<Timer>;
30-
struct comp
30+
struct TimerPtrComparer
3131
{
3232
bool operator()(const TimerPtr &x, const TimerPtr &y) const
3333
{
@@ -61,7 +61,8 @@ class TimerQueue : NonCopyable
6161
std::shared_ptr<Channel> timerfdChannelPtr_;
6262
void handleRead();
6363
#endif
64-
std::priority_queue<TimerPtr, std::vector<TimerPtr>, comp> timers_;
64+
std::priority_queue<TimerPtr, std::vector<TimerPtr>, TimerPtrComparer>
65+
timers_;
6566

6667
bool callingExpiredTimers_;
6768
bool insert(const TimerPtr &timePtr);

trantor/utils/Logger.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ static thread_local uint64_t threadId_{0};
7676

7777
void Logger::formatTime()
7878
{
79-
uint64_t now = date_.microSecondsSinceEpoch();
80-
uint64_t microSec = now % 1000000;
81-
now = now / 1000000;
79+
uint64_t now = static_cast<uint64_t>(date_.secondsSinceEpoch());
80+
uint64_t microSec =
81+
static_cast<uint64_t>(date_.microSecondsSinceEpoch() -
82+
date_.roundSecond().microSecondsSinceEpoch());
8283
if (now != lastSecond_)
8384
{
8485
lastSecond_ = now;

0 commit comments

Comments
 (0)