function futures = initFullThreadWorkerState(pool)
% Asynchronously initialize internal state for pool thread-workers. This includes RNG initialisation,
% GPU default device index and poolStartup if required. This function returns a
% FevalOnAllFuture.
%
% Internal-only.

%   Copyright 2021 The MathWorks, Inc.

% Hostname required for GPU mediator.
hostname = parallel.internal.general.HostNameUtils.getLocalCanonicalHostName();

% Get the current streamIdxVector at this level of pool-heirarchy. This
% could be empty.
streamIdxVector = parallel.internal.pool.workerRandStreamIndex();

% Get UUIDs for workers in this pool. These are used to assign reproducible
% RandStreams.
uuids = fetchOutputs(parfevalOnAll(pool, @parallel.internal.pool.workerUuid, 1));

% Flag if this is a backgroundPool. RNG setting is modified in this case
% and we don't run poolStartup script on workers.
if isa(pool,'parallel.BackgroundPool')
    isBackgroundWorker = true;
else
    isBackgroundWorker = false;
end

% Return future.
futures = parfevalOnAll(pool, @parallel.internal.pool.initThreadWorker, 1, hostname, streamIdxVector, uuids, isBackgroundWorker);

end
