Skip to content

Commit 33b05c8

Browse files
committed
Avoid locking if we won't run a time event
1 parent ed369d7 commit 33b05c8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/ae.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop)
589589

590590
/* Process time events */
591591
static int processTimeEvents(aeEventLoop *eventLoop) {
592-
std::unique_lock<decltype(g_lock)> ulock(g_lock);
592+
std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock);
593593
int processed = 0;
594594
aeTimeEvent *te;
595595
long long maxId;
@@ -634,8 +634,10 @@ static int processTimeEvents(aeEventLoop *eventLoop) {
634634
eventLoop->timeEventHead = te->next;
635635
if (te->next)
636636
te->next->prev = te->prev;
637-
if (te->finalizerProc)
637+
if (te->finalizerProc) {
638+
if (!ulock.owns_lock()) ulock.lock();
638639
te->finalizerProc(eventLoop, te->clientData);
640+
}
639641
zfree(te);
640642
te = next;
641643
continue;
@@ -654,6 +656,7 @@ static int processTimeEvents(aeEventLoop *eventLoop) {
654656
if (now_sec > te->when_sec ||
655657
(now_sec == te->when_sec && now_ms >= te->when_ms))
656658
{
659+
if (!ulock.owns_lock()) ulock.lock();
657660
int retval;
658661

659662
id = te->id;

0 commit comments

Comments
 (0)