1 /* 2 * Copyright (c) 2021 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 16 #ifndef PANDA_RUNTIME_INCLUDE_LOCKS_H_ 17 #define PANDA_RUNTIME_INCLUDE_LOCKS_H_ 18 19 #include "libpandabase/os/mutex.h" 20 21 #include <memory> 22 23 namespace panda { 24 25 class MutatorLock : public os::memory::RWLock { 26 #ifndef NDEBUG 27 public: 28 enum MutatorLockState { UNLOCKED, RDLOCK, WRLOCK }; 29 30 void ReadLock() ACQUIRE_SHARED(); 31 32 void WriteLock() ACQUIRE(); 33 34 bool TryReadLock() TRY_ACQUIRE_SHARED(true); 35 36 bool TryWriteLock() TRY_ACQUIRE(true); 37 38 void Unlock() RELEASE_GENERIC(); 39 40 MutatorLockState GetState(); 41 42 bool HasLock(); 43 #endif // !NDEBUG 44 }; 45 46 class Locks { 47 public: 48 static void Initialize(); 49 50 /** 51 * Lock used for preventing object heap modifications (for example at GC<->JIT,ManagedCode interaction during STW) 52 */ 53 static MutatorLock *mutator_lock; // NOLINT(misc-non-private-member-variables-in-classes) 54 /** 55 * Lock used for preventing custom_tls_cache_ modifications 56 */ 57 static os::memory::Mutex *custom_tls_lock; // NOLINT(misc-non-private-member-variables-in-classes) 58 59 /** 60 * The lock is a specific lock for exclusive suspension process, 61 * It is static for access from JVMTI interface 62 */ 63 static os::memory::Mutex *user_suspension_lock; 64 }; 65 66 } // namespace panda 67 68 #endif // PANDA_RUNTIME_INCLUDE_LOCKS_H_ 69