function [isPoolRunning, pool] = pctTryCreatePoolIfNecessary()
% Try to create a pool if the Settings deem that this is
% appropriate and there isn't already a pool running.

%   Copyright 2013 The MathWorks, Inc.

isPoolRunning = false;
% We don't want to use the empty method from parallel.Pool as we don't know
% at this point if there is a PCT license available.
pool = [];

% Bail if we cannot checkout a license. Capture and ignore any license
% checkout failure message here.
[result, licenseMessage] = license('checkout', 'Distrib_Computing_Toolbox');
if ~result
    dctSchedulerMessage(6, 'Failed to checkout license: %s\n', licenseMessage);
    return;
end

try
    % Using an internal API in order to avoid extra over head of going
    % through GCP.
    pool = parallel.internal.pool.PoolArrayManager.getOrAutoCreateWithCleanup();
    isPoolRunning = ~isempty(pool);
catch err
    dctSchedulerMessage(6, 'Failed to open pool: %s\n', err.getReport());
end
end
