Skip to content

Commit

Permalink
fix hang-at-exit bug when use dll on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Nov 24, 2023
1 parent c9488af commit 61ddc7a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/co/sched.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Sched::Sched(uint32 id, uint32 sched_num, uint32 stack_num, uint32 stack_size)
}
Sched::~Sched() {
this->stop(128);
this->stop();
co::del(_x.epoll);
_x.ev.~sync_event();
for (size_t i = 0; i < _bufs.size(); ++i) {
Expand All @@ -52,10 +52,21 @@ Sched::~Sched() {
co::free(_stack, _stack_num * sizeof(Stack));
}
void Sched::stop(uint32 ms) {
static int g_cnt = 0;
void Sched::stop() {
const int n = atomic_inc(&g_cnt, mo_relaxed);
if (atomic_swap(&_x.stopped, true, mo_acq_rel) == false) {
_x.epoll->signal();
ms == (uint32)-1 ? _x.ev.wait() : (void)_x.ev.wait(ms);
#if defined(_WIN32) && defined(BUILDING_CO_SHARED)
if (n == 1) {
// the thread may not respond in dll, wait at most 64ms here
co::Timer t;
while (!_x.ev.wait(1) && t.ms() < 64);
}
#else
_x.ev.wait();
#endif
}
}
Expand Down Expand Up @@ -333,7 +344,7 @@ SchedManager::SchedManager() {
}
SchedManager::~SchedManager() {
this->stop(128);
this->stop();
co::cleanup_sock();
}
Expand All @@ -357,9 +368,9 @@ SchedInitializer::SchedInitializer() {
SchedInitializer::~SchedInitializer() {
}
void SchedManager::stop(uint32 ms) {
void SchedManager::stop() {
for (size_t i = 0; i < _scheds.size(); ++i) {
_scheds[i]->stop(ms);
_scheds[i]->stop();
}
atomic_swap(&is_active(), false, mo_acq_rel);
}
Expand Down
4 changes: 2 additions & 2 deletions src/co/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class Sched {
void start() { std::thread(&Sched::loop, this).detach(); }

// stop the scheduler thread
void stop(uint32 ms=-1);
void stop();

// the thread function
void loop();
Expand Down Expand Up @@ -476,7 +476,7 @@ class SchedManager {
return _scheds;
}

void stop(uint32 ms=(uint32)-1);
void stop();

private:
std::function<Sched*(const co::vector<Sched*>&)> _next;
Expand Down
6 changes: 6 additions & 0 deletions src/log/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,13 @@ void Logger::stop(bool signal_safe) {
if (s < 0) return; // thread not started
if (s == 0) {
if (!signal_safe) _log_event.signal();
#if defined(_WIN32) && defined(BUILDING_CO_SHARED)
// the thread may not respond in dll, wait at most 64ms here
co::Timer t;
while (_stop != 2 && t.ms() < 64) signal_safe_sleep(1);
#else
while (_stop != 2) signal_safe_sleep(1);
#endif

do {
// it may be not safe if logs are still being pushed to the buffer
Expand Down

0 comments on commit 61ddc7a

Please sign in to comment.