22
33#include " ballistica/shared/ballistica.h"
44
5+ #include < chrono>
56#include < cstdio>
7+ #include < memory>
68#include < string>
9+ #include < thread>
710
811#include " ballistica/core/logging/logging.h"
912#include " ballistica/core/platform/core_platform.h"
@@ -41,7 +44,7 @@ auto main(int argc, char** argv) -> int {
4144namespace ballistica {
4245
4346// These are set automatically via script; don't modify them here.
44- const int kEngineBuildNumber = 22525 ;
47+ const int kEngineBuildNumber = 22528 ;
4548const char * kEngineVersion = " 1.7.50" ;
4649const int kEngineApiVersion = 9 ;
4750
@@ -228,11 +231,30 @@ class IncrementalInitRunner_ {
228231 last_step_time_ = core::CorePlatform::TimeMonotonicSeconds ();
229232 return false ;
230233
231- case 1 :
234+ case 1 : {
232235 core_->python ->WarmStart1 ();
236+
237+ // Launch a thread which spins and waits for the Python bg stuff
238+ // we just launched to complete. We do this in a separate thread
239+ // because even acquiring the GIL to make the check in the main
240+ // thread can be enough to trigger ANRs on slower hardware.
241+ thread_ = std::make_unique<std::thread>([this ] {
242+ while (explicit_bool (true )) {
243+ {
244+ Python::ScopedInterpreterLock gil_acquire;
245+ if (core_->python ->WarmStart1Completed ()) {
246+ warm_start_completed_ = true ;
247+ break ;
248+ }
249+ }
250+ std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
251+ }
252+ });
253+
233254 LogStepTime_ (step_);
234255 step_++;
235256 return false ;
257+ }
236258
237259 case 2 : {
238260 // This step is a special case; the previous step kicked off a
@@ -246,7 +268,11 @@ class IncrementalInitRunner_ {
246268 LogStepTime_ (step_);
247269 return false ;
248270 }
249- if (core_->python ->WarmStart1Completed ()) {
271+ // if (core_->python->WarmStart1Completed()) {
272+ if (warm_start_completed_) {
273+ // Our thread that set this should be done.
274+ thread_->join ();
275+
250276 // We finished this step.
251277 LogStepTime_ (step_);
252278 step_++;
@@ -349,6 +375,8 @@ class IncrementalInitRunner_ {
349375 int step_{};
350376 seconds_t last_step_time_{};
351377 bool zombie_{};
378+ bool warm_start_completed_{};
379+ std::unique_ptr<std::thread> thread_{};
352380 core::CoreConfig config_;
353381 core::CoreFeatureSet* core_{};
354382 core::BaseSoftInterface* base_{};
0 commit comments