1600cc4afSopenharmony_ci/* 2600cc4afSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3600cc4afSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4600cc4afSopenharmony_ci * you may not use this file except in compliance with the License. 5600cc4afSopenharmony_ci * You may obtain a copy of the License at 6600cc4afSopenharmony_ci * 7600cc4afSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8600cc4afSopenharmony_ci * 9600cc4afSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10600cc4afSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11600cc4afSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12600cc4afSopenharmony_ci * See the License for the specific language governing permissions and 13600cc4afSopenharmony_ci * limitations under the License. 14600cc4afSopenharmony_ci */ 15600cc4afSopenharmony_ci 16600cc4afSopenharmony_ci#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_PROMISE_H 17600cc4afSopenharmony_ci#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_PROMISE_H 18600cc4afSopenharmony_ci 19600cc4afSopenharmony_ci#include <atomic> 20600cc4afSopenharmony_ci#include <future> 21600cc4afSopenharmony_ci 22600cc4afSopenharmony_ci#include "app_log_wrapper.h" 23600cc4afSopenharmony_ci 24600cc4afSopenharmony_cinamespace OHOS { 25600cc4afSopenharmony_cinamespace AppExecFwk { 26600cc4afSopenharmony_ciclass BundlePromise { 27600cc4afSopenharmony_cipublic: 28600cc4afSopenharmony_ci BundlePromise() = default; 29600cc4afSopenharmony_ci ~BundlePromise() = default; 30600cc4afSopenharmony_ci /** 31600cc4afSopenharmony_ci * @brief Notify all tasks has executed finished. 32600cc4afSopenharmony_ci * @return 33600cc4afSopenharmony_ci */ 34600cc4afSopenharmony_ci void NotifyAllTasksExecuteFinished() 35600cc4afSopenharmony_ci { 36600cc4afSopenharmony_ci std::lock_guard<std::mutex> lock(notifyTaskMutex_); 37600cc4afSopenharmony_ci if (hasNotified_) { 38600cc4afSopenharmony_ci APP_LOGE("promise has executed and abort when NotifyAllTasksExecuteFinished"); 39600cc4afSopenharmony_ci return; 40600cc4afSopenharmony_ci } 41600cc4afSopenharmony_ci 42600cc4afSopenharmony_ci APP_LOGD("Notify all tasks has executed finished"); 43600cc4afSopenharmony_ci hasNotified_ = true; 44600cc4afSopenharmony_ci promise_.set_value(); 45600cc4afSopenharmony_ci } 46600cc4afSopenharmony_ci /** 47600cc4afSopenharmony_ci * @brief Wait for all tasks execute. 48600cc4afSopenharmony_ci * @return 49600cc4afSopenharmony_ci */ 50600cc4afSopenharmony_ci void WaitForAllTasksExecute() 51600cc4afSopenharmony_ci { 52600cc4afSopenharmony_ci std::lock_guard<std::mutex> lock(waitTaskMutex_); 53600cc4afSopenharmony_ci if (hasWaited_) { 54600cc4afSopenharmony_ci APP_LOGE("promise has executed and abort when WaitForAllTasksExecute"); 55600cc4afSopenharmony_ci return; 56600cc4afSopenharmony_ci } 57600cc4afSopenharmony_ci 58600cc4afSopenharmony_ci APP_LOGD("Wait for all tasks execute"); 59600cc4afSopenharmony_ci hasWaited_ = true; 60600cc4afSopenharmony_ci future_.get(); 61600cc4afSopenharmony_ci } 62600cc4afSopenharmony_ciprivate: 63600cc4afSopenharmony_ci std::atomic_bool hasNotified_ = false; 64600cc4afSopenharmony_ci std::atomic_bool hasWaited_ = false; 65600cc4afSopenharmony_ci std::promise<void> promise_; 66600cc4afSopenharmony_ci std::future<void> future_ = promise_.get_future(); 67600cc4afSopenharmony_ci std::mutex waitTaskMutex_; 68600cc4afSopenharmony_ci std::mutex notifyTaskMutex_; 69600cc4afSopenharmony_ci}; 70600cc4afSopenharmony_ci} // namespace AppExecFwk 71600cc4afSopenharmony_ci} // namespace OHOS 72600cc4afSopenharmony_ci#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_PROMISE_H 73