EXECUTION_ENGINE_HANDLE execution_engine_create()

in win32/src/execution_engine_win32.c [19:98]


EXECUTION_ENGINE_HANDLE execution_engine_create(const EXECUTION_ENGINE_PARAMETERS* execution_engine_parameters)
{
    EXECUTION_ENGINE_HANDLE result;
    EXECUTION_ENGINE_PARAMETERS parameters_to_use;

    if (execution_engine_parameters == NULL)
    {
        /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_011: [ If execution_engine_parameters is NULL, execution_engine_create shall use the defaults DEFAULT_MIN_THREAD_COUNT and DEFAULT_MAX_THREAD_COUNT as parameters. ]*/
        parameters_to_use.min_thread_count = DEFAULT_MIN_THREAD_COUNT;
        parameters_to_use.max_thread_count = DEFAULT_MAX_THREAD_COUNT;
    }
    else
    {
        /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_002: [ execution_engine_parameters shall be interpreted as EXECUTION_ENGINE_PARAMETERS_WIN32. ]*/
        parameters_to_use.min_thread_count = execution_engine_parameters->min_thread_count;
        parameters_to_use.max_thread_count = execution_engine_parameters->max_thread_count;
    }

    PTP_POOL ptp_pool;

    LogInfo("Creating execution engine with min thread count=%" PRIu32 ", max thread count=%" PRIu32 "", parameters_to_use.min_thread_count, parameters_to_use.max_thread_count);

    /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_013: [ If max_thread_count is non-zero, but less than min_thread_count, execution_engine_create shall fail and return NULL. ]*/
    if ((parameters_to_use.max_thread_count != 0) &&
        (parameters_to_use.max_thread_count < parameters_to_use.min_thread_count))
    {
        LogError("Invalid arguments: execution_engine_parameters_win32->min_thread_count=%" PRIu32 ", execution_engine_parameters_win32->max_thread_count=%" PRIu32,
            parameters_to_use.min_thread_count, parameters_to_use.max_thread_count);
    }
    else
    {
        /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_003: [ execution_engine_create shall call CreateThreadpool to create the Win32 threadpool. ]*/
        ptp_pool = CreateThreadpool(NULL);
        if (ptp_pool == NULL)
        {
            /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_006: [ If any error occurs, execution_engine_create shall fail and return NULL. ]*/
            LogLastError("CreateThreadpool failed");
        }
        else
        {
            /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_004: [ execution_engine_create shall set the minimum number of threads to the min_thread_count field of execution_engine_parameters. ]*/
            if (!SetThreadpoolThreadMinimum(ptp_pool, parameters_to_use.min_thread_count))
            {
                /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_006: [ If any error occurs, execution_engine_create shall fail and return NULL. ]*/
                LogLastError("SetThreadpoolThreadMinimum failed");
            }
            else
            {
                /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_012: [ If max_thread_count is 0, execution_engine_create shall not set the maximum thread count. ]*/
                if (parameters_to_use.max_thread_count > 0)
                {
                    /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_005: [ execution_engine_create shall set the maximum number of threads to the max_thread_count field of execution_engine_parameters. ]*/
                    SetThreadpoolThreadMaximum(ptp_pool, parameters_to_use.max_thread_count);
                }

                /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_001: [ execution_engine_create shall allocate a new execution engine and on success shall return a non-NULL handle. ]*/
                result = REFCOUNT_TYPE_CREATE(EXECUTION_ENGINE);
                if (result == NULL)
                {
                    /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_006: [ If any error occurs, execution_engine_create shall fail and return NULL. ]*/
                    LogError("REFCOUNT_TYPE_CREATE failed.");
                }
                else
                {
                    /* Codes_SRS_EXECUTION_ENGINE_WIN32_01_001: [ execution_engine_create shall allocate a new execution engine and on success shall return a non-NULL handle. ]*/
                    result->ptp_pool = ptp_pool;
                    goto all_ok;
                }
                
            }

            CloseThreadpool(ptp_pool);
        }
    }

    result = NULL;

all_ok:
    return result;
}