static void *worker_thread(void *opaque)

{

    ThreadPool *pool = opaque;



    qemu_mutex_lock(&pool->lock);

    pool->pending_threads--;

    do_spawn_thread(pool);



    while (!pool->stopping) {

        ThreadPoolElement *req;

        int ret;



        do {

            pool->idle_threads++;

            qemu_mutex_unlock(&pool->lock);

            ret = qemu_sem_timedwait(&pool->sem, 10000);

            qemu_mutex_lock(&pool->lock);

            pool->idle_threads--;

        } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));

        if (ret == -1 || pool->stopping) {

            break;

        }



        req = QTAILQ_FIRST(&pool->request_list);

        QTAILQ_REMOVE(&pool->request_list, req, reqs);

        req->state = THREAD_ACTIVE;

        qemu_mutex_unlock(&pool->lock);



        ret = req->func(req->arg);



        req->ret = ret;

        /* Write ret before state.  */

        smp_wmb();

        req->state = THREAD_DONE;



        qemu_mutex_lock(&pool->lock);



        qemu_bh_schedule(pool->completion_bh);

    }



    pool->cur_threads--;

    qemu_cond_signal(&pool->worker_stopped);

    qemu_mutex_unlock(&pool->lock);

    return NULL;

}

static void *worker_thread(void *opaque)

{

    ThreadPool *pool = opaque;



    qemu_mutex_lock(&pool->lock);

    pool->pending_threads--;

    do_spawn_thread(pool);



    while (!pool->stopping) {

        ThreadPoolElement *req;

        int ret;



        do {

            pool->idle_threads++;

            qemu_mutex_unlock(&pool->lock);

            ret = qemu_sem_timedwait(&pool->sem, 10000);

            qemu_mutex_lock(&pool->lock);

            pool->idle_threads--;

        } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));

        if (ret == -1 || pool->stopping) {

            break;

        }



        req = QTAILQ_FIRST(&pool->request_list);

        QTAILQ_REMOVE(&pool->request_list, req, reqs);

        req->state = THREAD_ACTIVE;

        qemu_mutex_unlock(&pool->lock);



        ret = req->func(req->arg);



        req->ret = ret;

        

        smp_wmb();

        req->state = THREAD_DONE;



        qemu_mutex_lock(&pool->lock);



        qemu_bh_schedule(pool->completion_bh);

    }



    pool->cur_threads--;

    qemu_cond_signal(&pool->worker_stopped);

    qemu_mutex_unlock(&pool->lock);

    return NULL;

}

static void *FUNC_0(void *VAR_0)

{

    ThreadPool *pool = VAR_0;



    qemu_mutex_lock(&pool->lock);

    pool->pending_threads--;

    do_spawn_thread(pool);



    while (!pool->stopping) {

        ThreadPoolElement *req;

        int VAR_1;



        do {

            pool->idle_threads++;

            qemu_mutex_unlock(&pool->lock);

            VAR_1 = qemu_sem_timedwait(&pool->sem, 10000);

            qemu_mutex_lock(&pool->lock);

            pool->idle_threads--;

        } while (VAR_1 == -1 && !QTAILQ_EMPTY(&pool->request_list));

        if (VAR_1 == -1 || pool->stopping) {

            break;

        }



        req = QTAILQ_FIRST(&pool->request_list);

        QTAILQ_REMOVE(&pool->request_list, req, reqs);

        req->state = THREAD_ACTIVE;

        qemu_mutex_unlock(&pool->lock);



        VAR_1 = req->func(req->arg);



        req->VAR_1 = VAR_1;

        

        smp_wmb();

        req->state = THREAD_DONE;



        qemu_mutex_lock(&pool->lock);



        qemu_bh_schedule(pool->completion_bh);

    }



    pool->cur_threads--;

    qemu_cond_signal(&pool->worker_stopped);

    qemu_mutex_unlock(&pool->lock);

    return NULL;

}

static void *FUNC_0(void *VAR_0)
{ ThreadPool *pool = VAR_0; qemu_mutex_lock(&pool->lock); pool->pending_threads--; do_spawn_thread(pool); while (!pool->stopping) { ThreadPoolElement *req; int VAR_1; do { pool->idle_threads++; qemu_mutex_unlock(&pool->lock); VAR_1 = qemu_sem_timedwait(&pool->sem, 10000); qemu_mutex_lock(&pool->lock); pool->idle_threads--; } while (VAR_1 == -1 && !QTAILQ_EMPTY(&pool->request_list)); if (VAR_1 == -1 || pool->stopping) { break; } req = QTAILQ_FIRST(&pool->request_list); QTAILQ_REMOVE(&pool->request_list, req, reqs); req->state = THREAD_ACTIVE; qemu_mutex_unlock(&pool->lock); VAR_1 = req->func(req->arg); req->VAR_1 = VAR_1; smp_wmb(); req->state = THREAD_DONE; qemu_mutex_lock(&pool->lock); qemu_bh_schedule(pool->completion_bh); } pool->cur_threads--; qemu_cond_signal(&pool->worker_stopped); qemu_mutex_unlock(&pool->lock); return NULL; }