Example #1
0
void Service::switchtonext()
{
    while (1)
    {
        // First switch if we have work to do.
        if (!m_resume.empty())
        {
            Fiber *old = m_running;

            m_running = m_resume.get();

            fb_switch(&old->m_cntxt, &m_running->m_cntxt);

            if (m_recycle)
            {
                m_recycle->m_joins.set();
                m_recycle->Unref();
                m_recycle = NULL;
            }
            break;
        }

        // Then weakup async event.
        while (1)
        {
            AsyncEvent *p = m_aEvents.get();
            if (p == NULL)
                break;

            p->callback();
        }

        if (!m_resume.empty())
            continue;

        // doing smoething when we have time.
        if (m_Idle)
            m_Idle();

        if (!m_resume.empty())
            continue;

        // if we still have time, weakup yield fiber.
        while (1)
        {
            AsyncEvent *p = m_yieldList.get();
            if (p == NULL)
                break;

            p->callback();
        }

        if (!m_resume.empty())
            continue;

        // still no work, we wait, and wait, and wait.....
        m_aEvents.wait()->callback();
    }
}