1 /* 2 * Copyright (c) 2021 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 OHOS_APP_DISPATCHER_TASK_SYNC_TASK_H 17 #define OHOS_APP_DISPATCHER_TASK_SYNC_TASK_H 18 19 #include <condition_variable> 20 #include <mutex> 21 22 #include "runnable.h" 23 #include "task_priority.h" 24 #include "task.h" 25 #include "app_log_wrapper.h" 26 #include "base_task_dispatcher.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 31 /** 32 * SyncTask is the synchronized task which provide synchronized method. 33 */ 34 class SyncTask final : public Task { 35 36 public: 37 SyncTask(const std::shared_ptr<Runnable> &runnable, TaskPriority priority, 38 const std::shared_ptr<BaseTaskDispatcher> &baseTaskDispatcher); 39 ~SyncTask() = default; 40 void Run() override; 41 42 void WaitTask(); 43 44 private: 45 std::atomic<bool> executed_; 46 std::mutex mutex_; 47 std::condition_variable condition_variable_; 48 }; 49 50 } // namespace AppExecFwk 51 } // namespace OHOS 52 #endif 53