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 #ifndef FOUNDATION_APPEXECFWK_OHOS_PARALLEL_TASK_DISPATCHER_H 16 #define FOUNDATION_APPEXECFWK_OHOS_PARALLEL_TASK_DISPATCHER_H 17 18 #include <string> 19 #include <vector> 20 #include <list> 21 #include <iostream> 22 #include <assert.h> 23 #include "parallel_task_dispatcher_base.h" 24 #include "runnable.h" 25 #include "sync_task.h" 26 #include "task_priority.h" 27 #include "task_execute_interceptor.h" 28 #include "task_executor.h" 29 #include "barrier_handler.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 /** 34 * Customed parallel TaskDispatcher which means it can be created multi times. 35 */ 36 class ParallelTaskDispatcher : public ParallelTaskDispatcherBase { 37 public: 38 ParallelTaskDispatcher(const std::string &name, TaskPriority priority, std::shared_ptr<TaskExecutor> &executor); ~ParallelTaskDispatcher()39 ~ParallelTaskDispatcher(){}; 40 41 /** 42 * Set a barrier and meanwhile a sync task that execute after all tasks finished. 43 * 44 * @param runnable is the job to execute after barrier. 45 * @throws NullPointerException if |task| is null. 46 * 47 */ 48 ErrCode SyncDispatchBarrier(const std::shared_ptr<Runnable> &runnable); 49 50 /** 51 * Set a barrier and meanwhile an async task that execute after all tasks finished. 52 * 53 * @param runnable is the job to execute after jobs in group. 54 * @throws NullPointerException if |task| is null. 55 * 56 */ 57 ErrCode AsyncDispatchBarrier(const std::shared_ptr<Runnable> &runnable); 58 59 protected: 60 std::shared_ptr<TaskExecuteInterceptor> GetInterceptor(); 61 62 private: 63 static const std::string DISPATCHER_TAG; 64 static const std::string ASYNC_DISPATCHER_BARRIER_TAG; 65 static const std::string SYNC_DISPATCHER_BARRIER_TAG; 66 std::shared_ptr<BarrierHandler> barrierHandler_ = nullptr; 67 }; 68 69 } // namespace AppExecFwk 70 } // namespace OHOS 71 #endif