diff --git a/ace_lite.gni b/ace_lite.gni index b793b928f0967cf5f0b28559bc8455882065bad2..fa86740ab9a1b1f65ae3f30707a56cbeaeccc423 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 0000000000000000000000000000000000000000..cd1927c4e1bf8c7f9c35bc646b372061bc1a98d1 --- /dev/null +++ b/frameworks/src/core/base/ace_lock.cpp @@ -0,0 +1,78 @@ +/* + * 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 { +#if (defined(__LINUX__) || defined(__LITEOS_A__)) +LockType::LockType() : mutex_(PTHREAD_MUTEX_INITIALIZER), mutexInited_(0) +{ + mutexInited_ = pthread_mutex_init(&mutex_, nullptr); +} +#else +LockType::LockType() : mutexInited_(0) +{ +} +#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) : lock_(lock) +{ +#if (defined(__LINUX__) || defined(__LITEOS_A__)) + lock_.Lock(); +#elif (defined(__LITEOS_M__) || defined(OHOS_ACELITE_PRODUCT_WATCH)) + 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 0000000000000000000000000000000000000000..a0f04a6df161aa7c312cc7810ba1dfdd04a58643 --- /dev/null +++ b/frameworks/src/core/base/ace_lock.h @@ -0,0 +1,60 @@ +/* + * 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_; +}; + +// 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: + ACE_DISALLOW_COPY_AND_MOVE(AutoLockGuard); + explicit AutoLockGuard(LockType &lock); + ~AutoLockGuard(); + +private: + LockType &lock_; +}; +} // namespace ACELite +} // namespace OHOS +#endif // OHOS_ACELITE_ACE_AUTO_LOCK_H \ No newline at end of file