1 /** 2 * Copyright (c) 2021-2022 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 panda { 26 27 /** Meeting point for all managed threads. 28 * High level plan: 29 * * Check if there's already a main thread (running_) 30 * * If there is, wait until we get woken up 31 * * Otherwise, acquire write global Mutator lock and set field running_ 32 */ 33 class Rendezvous { 34 public: 35 explicit Rendezvous() = default; 36 virtual ~Rendezvous() = default; 37 38 // Wait until all threads release Mutator lock and acquires it for write; 39 virtual void SafepointBegin() ACQUIRE(*Locks::mutator_lock); 40 // Ends safepoint (wakes up waiting threads, releases Mutator lock); 41 virtual void SafepointEnd() RELEASE(*Locks::mutator_lock); 42 43 private: 44 NO_MOVE_SEMANTIC(Rendezvous); 45 NO_COPY_SEMANTIC(Rendezvous); 46 }; 47 48 class EmptyRendezvous final : public Rendezvous { 49 public: 50 explicit EmptyRendezvous() = default; 51 ~EmptyRendezvous() override = default; 52 SafepointBegin()53 void SafepointBegin() override {} SafepointEnd()54 void SafepointEnd() override {} 55 56 private: 57 NO_MOVE_SEMANTIC(EmptyRendezvous); 58 NO_COPY_SEMANTIC(EmptyRendezvous); 59 }; 60 61 class ScopedSuspendAllThreads { 62 public: ScopedSuspendAllThreads(Rendezvous * rendezvous)63 explicit ScopedSuspendAllThreads(Rendezvous *rendezvous) ACQUIRE(*Locks::mutator_lock) : rendezvous_(rendezvous) 64 { 65 ASSERT(rendezvous_ != nullptr); 66 rendezvous_->SafepointBegin(); 67 } 68 ~ScopedSuspendAllThreads() RELEASE(*Locks::mutator_lock) 69 { 70 ASSERT(rendezvous_ != nullptr); 71 rendezvous_->SafepointEnd(); 72 } 73 74 NO_COPY_SEMANTIC(ScopedSuspendAllThreads); 75 NO_MOVE_SEMANTIC(ScopedSuspendAllThreads); 76 77 private: 78 Rendezvous *rendezvous_; 79 }; 80 81 class ScopedSuspendAllThreadsRunning { 82 public: 83 explicit ScopedSuspendAllThreadsRunning(Rendezvous *rendezvous) ACQUIRE(*Locks::mutator_lock); 84 ~ScopedSuspendAllThreadsRunning() RELEASE(*Locks::mutator_lock); 85 86 NO_COPY_SEMANTIC(ScopedSuspendAllThreadsRunning); 87 NO_MOVE_SEMANTIC(ScopedSuspendAllThreadsRunning); 88 89 private: 90 Rendezvous *rendezvous_; 91 }; 92 93 } // namespace panda 94 95 #endif // PANDA_RUNTIME_MEM_RENDEZVOUS_H 96