Skip to content

Commit

Permalink
Fixed|Windows: Shutting down the Timer thread
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 556a340 commit 9b16eef
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions doomsday/libs/core/src/core/timer.cpp
Expand Up @@ -45,8 +45,8 @@ struct TimerScheduler : public Thread, public Lockable
bool operator>(const Pending &other) const { return nextAt > other.nextAt; }
};

volatile bool running = true;
Waitable waiter;
bool running = true;
Waitable waiter;

std::priority_queue<Pending, std::vector<Pending>, std::greater<Pending>> pending;

Expand Down Expand Up @@ -130,26 +130,18 @@ struct TimerScheduler : public Thread, public Lockable
void stop()
{
// Wake up.
running = false;
waiter.post();
{
DE_GUARD(this);
running = false;
waiter.post();
}
join();
}
};

static LockableT<TimerScheduler *> scheduler;
static std::atomic_int timerCount; // Number of timers in existence.

static void deleteTimerScheduler()
{
DE_GUARD(scheduler);
if (scheduler.value)
{
scheduler.value->stop();
delete scheduler.value;
scheduler.value = nullptr;
}
}

} // namespace internal

DE_PIMPL_NOREF(Timer)
Expand All @@ -159,19 +151,19 @@ DE_PIMPL_NOREF(Timer)
bool isActive = false;
std::atomic_bool isPending{false}; // posted but not executed yet

// ~Impl()
// {
// using namespace internal;
//
// // The timer scheduler thread is stopped after all timers have been deleted.
// DE_GUARD(scheduler);
// if (--timerCount == 0)
// {
// scheduler.value->stop();
// delete scheduler.value;
// scheduler.value = nullptr;
// }
// }
~Impl()
{
using namespace internal;

// The timer scheduler thread is stopped after all timers have been deleted.
DE_GUARD(scheduler);
if (--internal::timerCount == 0)
{
scheduler.value->stop();
delete scheduler.value;
scheduler.value = nullptr;
}
}

DE_PIMPL_AUDIENCE(Trigger)
};
Expand All @@ -188,7 +180,6 @@ Timer::Timer()
{
scheduler.value = new TimerScheduler;
scheduler.value->start();
atexit(deleteTimerScheduler);
}
++timerCount;
}
Expand Down

0 comments on commit 9b16eef

Please sign in to comment.