diff --git a/src/hotspot/share/gc/shared/workerThread.cpp b/src/hotspot/share/gc/shared/workerThread.cpp index ee8abb25488..2e6fd768593 100644 --- a/src/hotspot/share/gc/shared/workerThread.cpp +++ b/src/hotspot/share/gc/shared/workerThread.cpp @@ -93,7 +93,24 @@ void WorkerThreads::initialize_workers() { } } +bool WorkerThreads::allow_inject_creation_failure() const { + if (!is_init_completed()) { + // Never allow creation failures during VM init + return false; + } + + if (_created_workers == 0) { + // Never allow creation failures of the first worker, it will cause the VM to exit + return false; + } + + return true; +} + WorkerThread* WorkerThreads::create_worker(uint name_suffix) { + if (InjectGCWorkerCreationFailure && allow_inject_creation_failure()) { + return nullptr; + } WorkerThread* const worker = new WorkerThread(_name, name_suffix, &_dispatcher); @@ -114,13 +131,6 @@ uint WorkerThreads::set_active_workers(uint num_workers) { "Invalid number of active workers %u (should be 1-%u)", num_workers, _max_workers); - if (_created_workers > 0 && InjectGCWorkerCreationFailure) { - assert(is_init_completed(), "Initialization not completed"); - log_error(gc, task)("Failed to create worker thread (InjectGCWorkerCreationFailure)"); - _active_workers = MIN2(_created_workers, num_workers); - return _active_workers; - } - while (_created_workers < num_workers) { WorkerThread* const worker = create_worker(_created_workers); if (worker == nullptr) { diff --git a/src/hotspot/share/gc/shared/workerThread.hpp b/src/hotspot/share/gc/shared/workerThread.hpp index d4e92797039..abb977decc9 100644 --- a/src/hotspot/share/gc/shared/workerThread.hpp +++ b/src/hotspot/share/gc/shared/workerThread.hpp @@ -103,6 +103,7 @@ public: WorkerThreads(const char* name, uint max_workers); void initialize_workers(); + bool allow_inject_creation_failure() const; uint max_workers() const { return _max_workers; } uint created_workers() const { return _created_workers; }