1 2 // Copyright (C) 2019 The Android Open Source Project 3 // Copyright (C) 2019 Google Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 #pragma once 17 18 #include <functional> 19 #include <memory> 20 #include <vector> 21 22 namespace android { 23 namespace base { 24 namespace guest { 25 26 // WorkPool provides a way to queue several different + arbitrary wait / signal 27 // operations. There is no specific imposed order to the operations; all 28 // ordering is derived from dependencies among the queued tasks. The number of 29 // threads used is the number of concurrent tasks in flight. Tasks are sent in 30 // groups, representing a collection that can be waited on (a wait group). 31 class WorkPool { 32 public: 33 using Task = std::function<void()>; 34 using WaitGroupHandle = uint64_t; 35 using TimeoutUs = uint64_t; 36 37 WorkPool(int numInitialThreads = 4); 38 ~WorkPool(); 39 40 WaitGroupHandle schedule(const std::vector<Task>& tasks); 41 42 bool waitAny(WaitGroupHandle waitGroup, TimeoutUs timeout = -1); 43 bool waitAll(WaitGroupHandle waitGroup, TimeoutUs timeout = -1); 44 private: 45 class Impl; 46 std::unique_ptr<Impl> mImpl; 47 }; 48 49 } // namespace android 50 } // namespace base 51 } // namespace guest 52