WORKER_THREAD_SCHEDULE_PROCESS_RESULT worker_thread_schedule_process()

in src/worker_thread.c [236:281]


WORKER_THREAD_SCHEDULE_PROCESS_RESULT worker_thread_schedule_process(WORKER_THREAD_HANDLE worker_thread)
{
    WORKER_THREAD_SCHEDULE_PROCESS_RESULT result;

    if (worker_thread == NULL)
    {
        /* Codes_SRS_WORKER_THREAD_01_016: [ If worker_thread is NULL, worker_thread_schedule_process shall fail and return WORKER_THREAD_SCHEDULE_PROCESS_ERROR. ]*/
        LogError("NULL worker_thread");
        result = WORKER_THREAD_SCHEDULE_PROCESS_ERROR;
    }
    else
    {
        /* Codes_SRS_WORKER_THREAD_01_041: [ Otherwise worker_thread_schedule_process shall call sm_exec_begin. ]*/
        if (sm_exec_begin(worker_thread->sm) != SM_EXEC_GRANTED)
        {
            /* Codes_SRS_WORKER_THREAD_01_042: [ If sm_exec_begin does not grant the execution, worker_thread_schedule_process shall fail and return WORKER_THREAD_SCHEDULE_PROCESS_INVALID_STATE. ]*/
            LogError("Cannot start execution of worker_thread_schedule_process");
            result = WORKER_THREAD_SCHEDULE_PROCESS_INVALID_STATE;
        }
        else
        {
            /* Codes_SRS_WORKER_THREAD_01_017: [ worker_thread_schedule_process shall set the thread state to WORKER_THREAD_STATE_PROCESS_ITEM. ]*/
            WORKER_THREAD_STATE worker_thread_state = interlocked_compare_exchange(&worker_thread->state, WORKER_THREAD_STATE_PROCESS_ITEM, WORKER_THREAD_STATE_IDLE);
            if (
                (worker_thread_state != WORKER_THREAD_STATE_IDLE) &&
                (worker_thread_state != WORKER_THREAD_STATE_PROCESS_ITEM)
                )
            {
                // Codes_SRS_WORKER_THREAD_11_003: [ If the thread state is not WORKER_THREAD_STATE_IDLE or WORKER_THREAD_STATE_PROCESS_ITEM, worker_thread_schedule_process shall fail and return WORKER_THREAD_SCHEDULE_PROCESS_INVALID_STATE. ]
                LogError("Cannot start execution of worker_thread_schedule_process, worker_thread_state=%" PRI_MU_ENUM "", MU_ENUM_VALUE(WORKER_THREAD_STATE, worker_thread_state));
                result = WORKER_THREAD_SCHEDULE_PROCESS_INVALID_STATE;
            }
            else
            {
                wake_by_address_single(&worker_thread->state);

                /* Codes_SRS_WORKER_THREAD_01_015: [ On success worker_thread_schedule_process shall return WORKER_THREAD_SCHEDULE_PROCESS_OK. ]*/
                result = WORKER_THREAD_SCHEDULE_PROCESS_OK;
            }
            /* Codes_SRS_WORKER_THREAD_01_043: [ worker_thread_schedule_process shall call sm_exec_end. ]*/
            sm_exec_end(worker_thread->sm);
        }
    }

    return result;
}