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