1 /*
2  * Copyright (c) 2023-2023 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_DEFERRED_PROCESSING_SERVICE_BASE_TASK_GROUP_H
17 #define OHOS_DEFERRED_PROCESSING_SERVICE_BASE_TASK_GROUP_H
18 
19 #include <any>
20 #include <atomic>
21 #include <cstdint>
22 #include <functional>
23 #include <memory>
24 #include <mutex>
25 #include "itask_group.h"
26 #include "blocking_queue.h"
27 #include "thread_pool.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 namespace DeferredProcessing {
32 class BaseTaskGroup : public ITaskGroup, public std::enable_shared_from_this<BaseTaskGroup> {
33 public:
34     BaseTaskGroup(const std::string& name, TaskFunc func, bool serial, const ThreadPool* threadPool);
35     ~BaseTaskGroup() override;
36     const std::string& GetName() final;
37     TaskGroupHandle GetHandle() final;
38     bool SubmitTask(std::any param) override;
39     void CancelAllTasks() override;
40     size_t GetTaskCount() override;
41 protected:
42     virtual void Initialize();
43     std::function<void()> GetTaskUnlocked();
44     TaskGroupHandle GenerateHandle();
45     void DispatchTaskUnlocked();
46     virtual void OnTaskComplete();
47 
48 private:
49     std::mutex mutex_;
50     const std::string name_;
51     const TaskFunc func_;
52     const bool serial_;
53     const ThreadPool* threadPool_;
54     TaskGroupHandle handle_;
55     std::atomic<bool> inflight_;
56     BlockingQueue<std::any> que_;
57 };
58 } //namespace DeferredProcessing
59 } // namespace CameraStandard
60 } // namespace OHOS
61 #endif // OHOS_DEFERRED_PROCESSING_SERVICE_BASE_TASK_GROUP_H
62