arkcompiler_ets_runtime/ecmascript/mutator_lock.h
wuzhefeng a6c0d7989b Introducing barrier to improve suspend-all performance
To improve the performance of suspend-all framework, the logic of suspend-all should be refactored.
Introduce barrier mechanism to synchronize the mutator threads when suspending-gc instead of via read-write-lock.

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I9IRJZ

Signed-off-by: wuzhefeng <wuzhefeng1@huawei.com>
Change-Id: I09d374c1276a8d39888739179ebd908c984812d0
2024-04-27 18:50:53 +08:00

79 lines
1.8 KiB
C++

/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMASCRIPT_MUTATOR_LOCK_H
#define ECMASCRIPT_MUTATOR_LOCK_H
#include "ecmascript/platform/mutex.h"
namespace panda::ecmascript {
class MutatorLock : public RWLock {
#ifndef NDEBUG
public:
enum MutatorLockState { UNLOCKED, RDLOCK, WRLOCK };
void ReadLock();
void WriteLock();
bool TryReadLock();
bool TryWriteLock();
void Unlock();
bool HasLock() const;
private:
MutatorLockState GetState() const;
void SetState(MutatorLockState newState);
#endif
};
class SuspendBarrier {
public:
SuspendBarrier() : passBarrierCount_(0)
{
}
explicit SuspendBarrier(int32_t count) : passBarrierCount_(count)
{
}
void Wait();
void Pass()
{
PassCount(-1);
}
void PassStrongly()
{
passBarrierCount_.fetch_sub(1, std::memory_order_seq_cst);
}
void Initialize(int32_t count)
{
passBarrierCount_.store(count, std::memory_order_relaxed);
}
void Increment(int32_t delta)
{
PassCount(delta);
Wait();
}
private:
void PassCount(int32_t delta);
std::atomic<int32_t> passBarrierCount_;
};
} // namespace panda::ecmascript
#endif // ECMASCRIPT_MUTATOR_LOCK_H