1 // Copyright (C) 2015 The Android Open Source Project 2 // 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 #pragma once 16 17 #include "aemu/base/TypeTraits.h" 18 #include "aemu/base/threads/AndroidThread.h" 19 #include "aemu/base/threads/AndroidThreadTypes.h" 20 21 #include <utility> 22 23 // FunctorThread class is an implementation of base Thread interface that 24 // allows one to run a function object in separate thread. It's mostly a 25 // convenience class so one doesn't need to create a separate class if the only 26 // needed thing is to run a specific existing function in a thread. 27 28 namespace gfxstream { 29 namespace guest { 30 31 class FunctorThread : public gfxstream::guest::Thread { 32 public: 33 using Functor = gfxstream::guest::ThreadFunctor; 34 35 explicit FunctorThread(const Functor& func, 36 ThreadFlags flags = ThreadFlags::MaskSignals) FunctorThread(Functor (func),flags)37 : FunctorThread(Functor(func), flags) {} 38 39 explicit FunctorThread(Functor&& func, 40 ThreadFlags flags = ThreadFlags::MaskSignals); 41 42 // A constructor from a void function in case when result isn't important. 43 template <class Func, class = enable_if<is_callable_as<Func, void()>>> 44 explicit FunctorThread(Func&& func, 45 ThreadFlags flags = ThreadFlags::MaskSignals) Thread(flags)46 : Thread(flags), mThreadFunc([func = std::move(func)]() { 47 func(); 48 return intptr_t(); 49 }) {} 50 51 private: 52 intptr_t main() override; 53 54 Functor mThreadFunc; 55 }; 56 57 } // namespace guest 58 } // namespace gfxstream 59