1 /** 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef PANDA_RUNTIME_MEM_RENDEZVOUS_H 16 #define PANDA_RUNTIME_MEM_RENDEZVOUS_H 17 18 #include <atomic> 19 20 #include "libpandabase/os/mutex.h" 21 #include "macros.h" 22 #include "runtime/include/thread.h" 23 #include "include/locks.h" 24 25 namespace ark { 26 27 class PandaVM; 28 29 /** 30 * Meeting point for all managed threads. 31 * High level plan: 32 * * Check if there's already a main thread (running_) 33 * * If there is, wait until we get woken up 34 * * Otherwise, acquire write global Mutator lock and set field running_ 35 */ 36 class Rendezvous { 37 public: 38 explicit Rendezvous(PandaVM *pandaVm); 39 virtual ~Rendezvous() = default; 40 41 // Wait until all threads release Mutator lock and acquires it for write; 42 virtual void SafepointBegin() ACQUIRE(*mutatorLock_); 43 // Ends safepoint (wakes up waiting threads, releases Mutator lock); 44 virtual void SafepointEnd() RELEASE(*mutatorLock_); 45 GetMutatorLock()46 MutatorLock *GetMutatorLock() 47 { 48 return mutatorLock_; 49 } 50 51 private: 52 NO_MOVE_SEMANTIC(Rendezvous); 53 NO_COPY_SEMANTIC(Rendezvous); 54 55 MutatorLock *mutatorLock_; 56 }; 57 58 class ScopedSuspendAllThreads { 59 public: ScopedSuspendAllThreads(Rendezvous * rendezvous)60 explicit ScopedSuspendAllThreads(Rendezvous *rendezvous) ACQUIRE(*rendezvous_->GetMutatorLock()) 61 : rendezvous_(rendezvous) 62 { 63 ASSERT(rendezvous_ != nullptr); 64 rendezvous_->SafepointBegin(); 65 } 66 ~ScopedSuspendAllThreads() RELEASE(*rendezvous_->GetMutatorLock()) 67 { 68 ASSERT(rendezvous_ != nullptr); 69 rendezvous_->SafepointEnd(); 70 } 71 72 NO_COPY_SEMANTIC(ScopedSuspendAllThreads); 73 NO_MOVE_SEMANTIC(ScopedSuspendAllThreads); 74 75 private: 76 Rendezvous *rendezvous_; 77 }; 78 79 class ScopedSuspendAllThreadsRunning { 80 public: 81 explicit ScopedSuspendAllThreadsRunning(Rendezvous *rendezvous) ACQUIRE(*rendezvous_->GetMutatorLock()); 82 ~ScopedSuspendAllThreadsRunning() RELEASE(*rendezvous_->GetMutatorLock()); 83 84 NO_COPY_SEMANTIC(ScopedSuspendAllThreadsRunning); 85 NO_MOVE_SEMANTIC(ScopedSuspendAllThreadsRunning); 86 87 private: 88 Rendezvous *rendezvous_; 89 }; 90 91 } // namespace ark 92 93 #endif // PANDA_RUNTIME_MEM_RENDEZVOUS_H 94