[vm] Avoid mutex lock when clearing catch entry cache.

Fixes https://github.com/dart-lang/sdk/issues/60535
TEST=iso_stress_linux ci

Change-Id: I8f7c6faf23645f6e35b9abdf5b5e8a3684879624
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422346
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
diff --git a/runtime/vm/heap/heap.cc b/runtime/vm/heap/heap.cc
index abf50cf..16b0a72 100644
--- a/runtime/vm/heap/heap.cc
+++ b/runtime/vm/heap/heap.cc
@@ -546,7 +546,7 @@
 
     // Some Code objects may have been collected so invalidate handler cache.
     thread->isolate_group()->handler_info_cache()->Clear();
-    thread->isolate_group()->ClearCatchEntryMovesCache();
+    thread->isolate_group()->ClearCatchEntryMovesCacheLocked();
     assume_scavenge_will_fail_ = false;
   }
 }
diff --git a/runtime/vm/isolate.cc b/runtime/vm/isolate.cc
index 6e8a89f..69cde8d 100644
--- a/runtime/vm/isolate.cc
+++ b/runtime/vm/isolate.cc
@@ -947,8 +947,14 @@
   }
 }
 
-void IsolateGroup::ClearCatchEntryMovesCache() {
-  SafepointMutexLocker ml(&cache_mutex_);
+void IsolateGroup::ClearCatchEntryMovesCacheLocked() {
+  auto thread = Thread::Current();
+  ASSERT(thread->OwnsSafepoint() ||
+         (thread->task_kind() == Thread::kMutatorTask) ||
+         (thread->task_kind() == Thread::kMarkerTask) ||
+         (thread->task_kind() == Thread::kCompactorTask) ||
+         (thread->task_kind() == Thread::kScavengerTask) ||
+         (thread->task_kind() == Thread::kIncrementalCompactorTask));
   catch_entry_moves_cache_.Clear();
 }
 
diff --git a/runtime/vm/isolate.h b/runtime/vm/isolate.h
index 6ba5018..2eb4da9 100644
--- a/runtime/vm/isolate.h
+++ b/runtime/vm/isolate.h
@@ -780,7 +780,7 @@
       const Code& code,
       intptr_t pc,
       std::function<void(const CatchEntryMoves&)> action);
-  void ClearCatchEntryMovesCache();
+  void ClearCatchEntryMovesCacheLocked();
 
   void SetNativeAssetsCallbacks(NativeAssetsApi* native_assets_api) {
     native_assets_api_ = *native_assets_api;