Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/TaskExecutor.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ Logger backends with `Config::use_dedicated_executor=true` own a
worker thread per configured logger, while single-threaded Emscripten builds use
a cooperative per-instance queue. `Logger::shutdown()` calls each backend's
`ILogger::shutdown()` hook before stopping the global executor so these
logger-owned workers drain and stop cleanly.
logger-owned workers drain and stop cleanly. For `SingleThreadExecutor`,
`wait()` waits for already accepted tasks to drain, while `shutdown()` rejects
new tasks and joins the worker on native builds.

## 6. Emscripten (no pthreads)

Expand Down
7 changes: 4 additions & 3 deletions include/logit_cpp/logit/detail/SingleThreadExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ class SingleThreadExecutor {
void wait() {
std::unique_lock<std::mutex> lock(m_mutex);
m_cv.wait(lock, [this]() {
return (m_queue.empty() &&
m_active_tasks.load(std::memory_order_relaxed) == 0) ||
m_stop.load(std::memory_order_acquire);
return m_queue.empty() &&
m_active_tasks.load(std::memory_order_relaxed) == 0;
});
}

Expand Down Expand Up @@ -127,6 +126,7 @@ class SingleThreadExecutor {
/// \brief Change the queue overflow policy.
void set_queue_policy(QueuePolicy policy) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_stop.load(std::memory_order_acquire)) return;
m_overflow_policy = policy;
}

Expand Down Expand Up @@ -274,6 +274,7 @@ class SingleThreadExecutor {

void set_queue_policy(QueuePolicy policy) {
std::lock_guard<std::mutex> lock(m_state->mutex);
if (m_state->shutdown_requested) return;
m_state->overflow_policy = policy;
}

Expand Down
60 changes: 60 additions & 0 deletions tests/single_thread_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,65 @@ static bool test_post_shutdown_rejection() {
return counter.load() == 1;
}

static bool test_wait_drains_after_shutdown_start() {
SingleThreadExecutor ex;
std::atomic<int> counter{0};
std::atomic<bool> wait_done{false};

std::mutex gate_mutex;
std::condition_variable gate_cv;
bool first_task_started = false;
bool gate_open = false;

ex.add_task([&]() {
std::unique_lock<std::mutex> lk(gate_mutex);
first_task_started = true;
gate_cv.notify_all();
gate_cv.wait(lk, [&]() { return gate_open; });
counter.fetch_add(1, std::memory_order_relaxed);
});
ex.add_task([&]() {
counter.fetch_add(1, std::memory_order_relaxed);
});

{
std::unique_lock<std::mutex> lk(gate_mutex);
if (!gate_cv.wait_for(lk, std::chrono::seconds(1), [&]() {
return first_task_started;
})) {
gate_open = true;
gate_cv.notify_all();
return false;
}
}

std::thread waiter([&]() {
ex.wait();
wait_done.store(true, std::memory_order_release);
});

std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::thread stopper([&]() {
ex.shutdown();
});

std::this_thread::sleep_for(std::chrono::milliseconds(50));
const bool returned_while_blocked = wait_done.load(std::memory_order_acquire);

{
std::lock_guard<std::mutex> lk(gate_mutex);
gate_open = true;
}
gate_cv.notify_all();

stopper.join();
waiter.join();

return !returned_while_blocked &&
wait_done.load(std::memory_order_acquire) &&
counter.load(std::memory_order_relaxed) == 2;
}

int main() {
int passed = 0;
int failed = 0;
Expand All @@ -328,6 +387,7 @@ int main() {
run("exception_in_task", test_exception_in_task());
run("concurrent_producers", test_concurrent_producers());
run("post_shutdown_rejection", test_post_shutdown_rejection());
run("wait_drains_after_shutdown_start", test_wait_drains_after_shutdown_start());

std::cout << "\n" << passed << " passed, " << failed << " failed" << std::endl;
return failed > 0 ? 1 : 0;
Expand Down
Loading