• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #ifndef ECMASCRIPT_MUTATOR_LOCK_H
17 #define ECMASCRIPT_MUTATOR_LOCK_H
18 
19 #include <limits.h>
20 
21 #include "ecmascript/platform/mutex.h"
22 
23 namespace panda::ecmascript {
24 
25 class MutatorLock : public RWLock {
26 #ifndef NDEBUG
27 public:
28     enum MutatorLockState { UNLOCKED, RDLOCK, WRLOCK };
29     void ReadLock();
30     void WriteLock();
31     bool TryReadLock();
32     bool TryWriteLock();
33     void Unlock();
34     bool HasLock() const;
35 
36 private:
37     MutatorLockState GetState() const;
38     void SetState(MutatorLockState newState);
39 #endif
40 };
41 
42 class SuspendBarrier {
43 public:
SuspendBarrier()44     SuspendBarrier() : passBarrierCount_(0)
45     {
46     }
47 
SuspendBarrier(int32_t count)48     explicit SuspendBarrier(int32_t count) : passBarrierCount_(count)
49     {
50     }
51 
52     void Wait();
53 
PassStrongly()54     void PassStrongly()
55     {
56         [[maybe_unused]] int32_t oldCount = passBarrierCount_.fetch_sub(1, std::memory_order_seq_cst);
57 #if defined(PANDA_USE_FUTEX)
58         if (oldCount == 1) {
59             int32_t *addr = reinterpret_cast<int32_t*>(&passBarrierCount_);
60             futex(addr, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0);
61         }
62 #endif
63     }
64 
Initialize(int32_t count)65     void Initialize(int32_t count)
66     {
67         passBarrierCount_.store(count, std::memory_order_relaxed);
68     }
69 
70 private:
71     std::atomic<int32_t> passBarrierCount_;
72 };
73 
74 }  // namespace panda::ecmascript
75 #endif // ECMASCRIPT_MUTATOR_LOCK_H