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 gfxstream { 23 namespace guest { 24 25 // WorkPool provides a way to queue several different + arbitrary wait / signal 26 // operations. There is no specific imposed order to the operations; all 27 // ordering is derived from dependencies among the queued tasks. The number of 28 // threads used is the number of concurrent tasks in flight. Tasks are sent in 29 // groups, representing a collection that can be waited on (a wait group). 30 class WorkPool { 31 public: 32 using Task = std::function<void()>; 33 using WaitGroupHandle = uint64_t; 34 using TimeoutUs = uint64_t; 35 36 WorkPool(int numInitialThreads = 4); 37 ~WorkPool(); 38 39 WaitGroupHandle schedule(const std::vector<Task>& tasks); 40 41 bool waitAny(WaitGroupHandle waitGroup, TimeoutUs timeout = -1); 42 bool waitAll(WaitGroupHandle waitGroup, TimeoutUs timeout = -1); 43 private: 44 class Impl; 45 std::unique_ptr<Impl> mImpl; 46 }; 47 48 } // namespace guest 49 } // namespace gfxstream 50