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 TASK_POOL_H 17 #define TASK_POOL_H 18 19 #include <functional> 20 #include <string> 21 22 #include "macro_utils.h" 23 24 namespace DistributedDB { 25 using Task = std::function<void(void)>; 26 27 class TaskPool { 28 public: 29 // Start the task pool. 30 virtual int Start() = 0; 31 32 // Stop the task pool. 33 virtual void Stop() = 0; 34 35 // Schedule a task, the task can be ran in any thread. 36 virtual int Schedule(const Task &task) = 0; 37 38 // Schedule tasks using FIFO policy(tasks with the same 'tag'). 39 virtual int Schedule(const std::string &tag, const Task &task) = 0; 40 41 // Shrink memory associated with the given tag if possible. 42 virtual void ShrinkMemory(const std::string &tag) = 0; 43 44 // Create/Destroy a task pool. 45 static TaskPool *Create(int maxThreads, int minThreads, int &errCode); 46 static void Release(TaskPool *&taskPool); 47 48 protected: 49 TaskPool() = default; ~TaskPool()50 virtual ~TaskPool() {} 51 DISABLE_COPY_ASSIGN_MOVE(TaskPool); 52 }; 53 } // namespace DistributedDB 54 55 #endif // TASK_POOL_H