Skip to content

Commit 9a0a3ba

Browse files
committed
WIP: Load DSO and Star catalogs in parallel
1 parent e3e4a5b commit 9a0a3ba

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

src/celestia/celestiacore.cpp

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cassert>
2525
#include <ctime>
2626
#include <fstream>
27+
#include <future>
2728
#include <iomanip>
2829
#include <iostream>
2930
#include <iterator>
@@ -2307,6 +2308,11 @@ void CelestiaCore::updateFOV(float newFOV, std::optional<Eigen::Vector2f> focus,
23072308
}
23082309
}
23092310

2311+
template<typename T>
2312+
bool ready(const std::future<T> &future)
2313+
{
2314+
return future.wait_for(std::chrono::seconds(1)) == std::future_status::ready;
2315+
}
23102316

23112317
bool CelestiaCore::initSimulation(const fs::path& configFileName,
23122318
const vector<fs::path>& extrasDirs,
@@ -2391,23 +2397,46 @@ bool CelestiaCore::initSimulation(const fs::path& configFileName,
23912397

23922398
StarDetails::SetStarTextures(config->starTextures);
23932399

2394-
std::unique_ptr<StarDatabase> starCatalog = loadStars(*config, progressNotifier);
2395-
if (starCatalog == nullptr)
2396-
{
2397-
fatalError(_("Cannot read star database."), false);
2398-
return false;
2399-
}
2400-
universe->setStarCatalog(std::move(starCatalog));
2400+
progressNotifier->update(_("Deep Space Objects and Star catalogs"));
24012401

2402-
/***** Load the deep sky catalogs *****/
2402+
auto *cfg = config.get();
24032403

2404-
std::unique_ptr<DSODatabase> dsoCatalog = loadDSO(*config, progressNotifier);
2405-
if (dsoCatalog == nullptr)
2404+
std::future<std::unique_ptr<StarDatabase>> starsLoader = std::async(std::launch::async, [cfg]() {
2405+
return loadStars(*cfg, nullptr);
2406+
});
2407+
2408+
std::future<std::unique_ptr<DSODatabase>> dsoLoader = std::async(std::launch::async, [cfg]() {
2409+
return loadDSO(*cfg, nullptr);
2410+
});
2411+
2412+
for (bool starsReady = false, dsoReady = false; !starsReady || !dsoReady;)
24062413
{
2407-
fatalError(_("Cannot read DSO database."), false);
2408-
return false;
2414+
if (!starsReady && ready(starsLoader))
2415+
{
2416+
starsReady = true;
2417+
2418+
std::unique_ptr<StarDatabase> starCatalog = starsLoader.get();
2419+
if (starCatalog == nullptr)
2420+
{
2421+
fatalError(_("Cannot read star database."), false);
2422+
return false;
2423+
}
2424+
universe->setStarCatalog(std::move(starCatalog));
2425+
}
2426+
2427+
if (!dsoReady && ready(dsoLoader))
2428+
{
2429+
dsoReady = true;
2430+
2431+
std::unique_ptr<DSODatabase> dsoCatalog = dsoLoader.get();
2432+
if (dsoCatalog == nullptr)
2433+
{
2434+
fatalError(_("Cannot read DSO database."), false);
2435+
return false;
2436+
}
2437+
universe->setDSOCatalog(std::move(dsoCatalog));
2438+
}
24092439
}
2410-
universe->setDSOCatalog(std::move(dsoCatalog));
24112440

24122441
/***** Load the solar system catalogs *****/
24132442

@@ -3270,14 +3299,14 @@ void CelestiaCore::setAudioNoPause(int channel, bool nopause)
32703299

32713300
void CelestiaCore::pauseAudioIfNeeded()
32723301
{
3273-
for (auto const &[_, value] : audioSessions)
3302+
for (const auto &[_, value] : audioSessions)
32743303
if (!value->nopause())
32753304
value->stop();
32763305
}
32773306

32783307
void CelestiaCore::resumeAudioIfNeeded()
32793308
{
3280-
for (auto const &[_, value] : audioSessions)
3309+
for (const auto &[_, value] : audioSessions)
32813310
if (!value->nopause())
32823311
value->play();
32833312
}

0 commit comments

Comments
 (0)