From 25518da36f69421a9ade9dfff2c6f7af9a50b44a Mon Sep 17 00:00:00 2001 From: jiadexiang Date: Wed, 31 Mar 2021 19:05:24 +0800 Subject: [PATCH 1/4] IssueNo:I3E91Z Description: implement auto lock wrapper Sig:ace_engine_lite Feature or Bugfix:Feature Binary Source:No --- ace_lite.gni | 3 +- frameworks/src/core/base/ace_lock.cpp | 76 +++++++++++++++++++++++++++ frameworks/src/core/base/ace_lock.h | 59 +++++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 frameworks/src/core/base/ace_lock.cpp create mode 100644 frameworks/src/core/base/ace_lock.h diff --git a/ace_lite.gni b/ace_lite.gni index b793b928..fa86740a 100755 --- a/ace_lite.gni +++ b/ace_lite.gni @@ -92,11 +92,12 @@ ace_lite_sources = [ "$ACE_LITE_PATH/src/core/animation/transition_impl.cpp", "$ACE_LITE_PATH/src/core/async/js_async_work.cpp", "$ACE_LITE_PATH/src/core/async/message_queue_utils.cpp", + "$ACE_LITE_PATH/src/core/base/ace_lock.cpp", "$ACE_LITE_PATH/src/core/base/ace_log.cpp", "$ACE_LITE_PATH/src/core/base/ace_mem_base.cpp", - "$ACE_LITE_PATH/src/core/base/dft_impl.cpp", "$ACE_LITE_PATH/src/core/base/async_task_manager.cpp", "$ACE_LITE_PATH/src/core/base/cache/cache_manager.cpp", + "$ACE_LITE_PATH/src/core/base/dft_impl.cpp", "$ACE_LITE_PATH/src/core/base/dfx_assist.cpp", "$ACE_LITE_PATH/src/core/base/event_util.cpp", "$ACE_LITE_PATH/src/core/base/js_debugger_config.cpp", diff --git a/frameworks/src/core/base/ace_lock.cpp b/frameworks/src/core/base/ace_lock.cpp new file mode 100644 index 00000000..948e8bbe --- /dev/null +++ b/frameworks/src/core/base/ace_lock.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021 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. + */ +#include "ace_lock.h" + +#if (defined(__LITEOS_M__) || defined(OHOS_ACELITE_PRODUCT_WATCH)) +#include "los_task.h" +#endif + +namespace OHOS { +namespace ACELite { +LockType::LockType() : mutexInited_(0) +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + mutexInited_ = pthread_mutex_init(&mutex_, nullptr); +#endif +} + +LockType::~LockType() +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + if (mutexInited_ == 0) { + pthread_mutex_destroy(&mutex_); + } +#endif +} + +void LockType::Lock() +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + if (mutexInited_ == 0) { + pthread_mutex_lock(&mutex_); + } +#endif +} +void LockType::Unlock() +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + if (mutexInited_ == 0) { + pthread_mutex_unlock(&mutex_); + } +#endif +} + +AutoLockGuard::AutoLockGuard(LockType &lock) +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + lock_ = &lock; + lock_->Lock(); +#elif (defined(__LITEOS_M__) || defined(OHOS_ACELITE_PRODUCT_WATCH)) + (void)(lock); + LOS_TaskLock(); +#endif +} + +AutoLockGuard::~AutoLockGuard() +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + lock_->Unlock(); +#elif (defined(__LITEOS_M__) || defined(OHOS_ACELITE_PRODUCT_WATCH)) + LOS_TaskUnlock(); +#endif +} +} // namespace ACELite +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/src/core/base/ace_lock.h b/frameworks/src/core/base/ace_lock.h new file mode 100644 index 00000000..8d4233f8 --- /dev/null +++ b/frameworks/src/core/base/ace_lock.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 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 OHOS_ACELITE_ACE_AUTO_LOCK_H +#define OHOS_ACELITE_ACE_AUTO_LOCK_H + +#include "acelite_config.h" + +#include "memory_heap.h" +#include "non_copyable.h" +#if (defined(__LINUX__) || defined(__LITEOS_A__)) +#include +#endif + +namespace OHOS { +namespace ACELite { +// The base lock type +class LockType final : public MemoryHeap { +public: + ACE_DISALLOW_COPY_AND_MOVE(LockType); + LockType(); + ~LockType(); + // Lock mutex, waiting for it if necessary. + void Lock(); + void Unlock(); + +private: +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + pthread_mutex_t mutex_; +#endif + // the mutex initialization result, 0 means success + int mutexInited_ = 0; +}; + +// The automatic lock and unlock wrapper binding with the scope. +// Usage: declare LockType lock inadvance, and declare AutoLockGuard(lock) in the target locking scope. +class AutoLockGuard final : public MemoryHeap { +public: + AutoLockGuard(LockType &lock); + ~AutoLockGuard(); + +private: + LockType *lock_ = nullptr; +}; +} // namespace ACELite +} // namespace OHOS +#endif // OHOS_ACELITE_ACE_AUTO_LOCK_H \ No newline at end of file -- Gitee From 01e363486540d8440555eea18f4e4b6f3ceab400 Mon Sep 17 00:00:00 2001 From: jiadexiang Date: Thu, 1 Apr 2021 14:39:23 +0800 Subject: [PATCH 2/4] IssueNo:I3E91Z Description: improve lock guard implementation Sig:ace_engine_lite Feature or Bugfix:Feature Binary Source:No --- frameworks/src/core/base/ace_lock.cpp | 8 +++----- frameworks/src/core/base/ace_lock.h | 5 +++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/frameworks/src/core/base/ace_lock.cpp b/frameworks/src/core/base/ace_lock.cpp index 948e8bbe..ebf8a2e3 100644 --- a/frameworks/src/core/base/ace_lock.cpp +++ b/frameworks/src/core/base/ace_lock.cpp @@ -53,13 +53,11 @@ void LockType::Unlock() #endif } -AutoLockGuard::AutoLockGuard(LockType &lock) +AutoLockGuard::AutoLockGuard(LockType &lock) : lock_(lock) { #if (defined(__LINUX__) || defined(__LITEOS_A__)) - lock_ = &lock; - lock_->Lock(); + lock_.Lock(); #elif (defined(__LITEOS_M__) || defined(OHOS_ACELITE_PRODUCT_WATCH)) - (void)(lock); LOS_TaskLock(); #endif } @@ -67,7 +65,7 @@ AutoLockGuard::AutoLockGuard(LockType &lock) AutoLockGuard::~AutoLockGuard() { #if (defined(__LINUX__) || defined(__LITEOS_A__)) - lock_->Unlock(); + lock_.Unlock(); #elif (defined(__LITEOS_M__) || defined(OHOS_ACELITE_PRODUCT_WATCH)) LOS_TaskUnlock(); #endif diff --git a/frameworks/src/core/base/ace_lock.h b/frameworks/src/core/base/ace_lock.h index 8d4233f8..886911e5 100644 --- a/frameworks/src/core/base/ace_lock.h +++ b/frameworks/src/core/base/ace_lock.h @@ -48,11 +48,12 @@ private: // Usage: declare LockType lock inadvance, and declare AutoLockGuard(lock) in the target locking scope. class AutoLockGuard final : public MemoryHeap { public: - AutoLockGuard(LockType &lock); + ACE_DISALLOW_COPY_AND_MOVE(AutoLockGuard); + explicit AutoLockGuard(LockType &lock); ~AutoLockGuard(); private: - LockType *lock_ = nullptr; + LockType &lock_; }; } // namespace ACELite } // namespace OHOS -- Gitee From f6c0ca02ccbb07d31b4cbf873d20c7700c66ccf0 Mon Sep 17 00:00:00 2001 From: jiadexiang Date: Thu, 1 Apr 2021 15:32:50 +0800 Subject: [PATCH 3/4] IssueNo:I3E91Z Description: fix review comments Sig:ace_engine_lite Feature or Bugfix:Feature Binary Source:No --- frameworks/src/core/base/ace_lock.cpp | 10 +++++++--- frameworks/src/core/base/ace_lock.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/frameworks/src/core/base/ace_lock.cpp b/frameworks/src/core/base/ace_lock.cpp index ebf8a2e3..f6d7319e 100644 --- a/frameworks/src/core/base/ace_lock.cpp +++ b/frameworks/src/core/base/ace_lock.cpp @@ -20,12 +20,16 @@ namespace OHOS { namespace ACELite { -LockType::LockType() : mutexInited_(0) -{ #if (defined(__LINUX__) || defined(__LITEOS_A__)) +LockType::LockType() : mutext_(PTHREAD_MUTEX_INITIALIZER), mutexInited_(0) +{ mutexInited_ = pthread_mutex_init(&mutex_, nullptr); -#endif } +#else +LockType::LockType() : mutexInited_(0) +{ +} +#endif LockType::~LockType() { diff --git a/frameworks/src/core/base/ace_lock.h b/frameworks/src/core/base/ace_lock.h index 886911e5..a0f04a6d 100644 --- a/frameworks/src/core/base/ace_lock.h +++ b/frameworks/src/core/base/ace_lock.h @@ -41,7 +41,7 @@ private: pthread_mutex_t mutex_; #endif // the mutex initialization result, 0 means success - int mutexInited_ = 0; + int mutexInited_; }; // The automatic lock and unlock wrapper binding with the scope. -- Gitee From 8dad13e888b800c1a9e69fae99a98455b0fe247b Mon Sep 17 00:00:00 2001 From: jiadexiang Date: Thu, 1 Apr 2021 15:41:22 +0800 Subject: [PATCH 4/4] IssueNo:I3E91Z Description: fix typo Sig:ace_engine_lite Feature or Bugfix:Feature Binary Source:No --- frameworks/src/core/base/ace_lock.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/src/core/base/ace_lock.cpp b/frameworks/src/core/base/ace_lock.cpp index f6d7319e..cd1927c4 100644 --- a/frameworks/src/core/base/ace_lock.cpp +++ b/frameworks/src/core/base/ace_lock.cpp @@ -21,7 +21,7 @@ namespace OHOS { namespace ACELite { #if (defined(__LINUX__) || defined(__LITEOS_A__)) -LockType::LockType() : mutext_(PTHREAD_MUTEX_INITIALIZER), mutexInited_(0) +LockType::LockType() : mutex_(PTHREAD_MUTEX_INITIALIZER), mutexInited_(0) { mutexInited_ = pthread_mutex_init(&mutex_, nullptr); } -- Gitee