Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically changing EmptyQueueBehavior from Sleep to anything else can leave threads permanently sleeping #107

Open
RichieSams opened this issue May 3, 2020 · 0 comments

Comments

@RichieSams
Copy link
Owner

In multiple places in the code, we switch based on the empty queue behavior and do different logic for each.

Most prominantly, when FiberStart function fails to find a task or fiber to run, it will either loop, yield, or sleep. If behavior is set to Sleep, in AddTask(s) and when waiting tasks are ready, we then notify sleeping threads to wake.

const EmptyQueueBehavior behavior = m_emptyQueueBehavior.load(std::memory_order_relaxed);
if (behavior == EmptyQueueBehavior::Sleep) {
    // Find a thread that is sleeping and wake it
    for (size_t i = 0; i < m_numThreads; ++i) {
        std::unique_lock<std::mutex> lock(m_tls[i].FailedQueuePopLock);
        if (m_tls[i].FailedQueuePopAttempts >= kFailedPopAttemptsHeuristic) {
            m_tls[i].FailedQueuePopAttempts = 0;
            m_tls[i].FailedQueuePopCV.notify_one();

            break;
        }
    }
}

However, if the user changes the behavior mid run, we can end up with a thread permanently sleeping:

  1. Worker thread A goes to sleep since there are no tasks
  2. User changes behavior from Sleep to Yield
  3. User code calls AddTasks()
  4. AddTasks() doesn't wake thread A because behavior is set to Yield now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant