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/Compiler.h" 18 #include "aemu/base/async/Looper.h" 19 #include "aemu/base/threads/Thread.h" 20 #include "aemu/base/threads/Types.h" 21 22 #include <memory> 23 24 namespace android { 25 namespace base { 26 namespace internal { 27 28 // Base class containing template free implemenatation details of ParallelTask 29 // to minimize template specialization blowup. 30 // 31 // This is an implementation detail. DO NOT use this class directly. 32 class ParallelTaskBase { 33 public: 34 virtual ~ParallelTaskBase() = default; 35 36 protected: 37 ParallelTaskBase(android::base::Looper* looper, 38 android::base::Looper::Duration checkTimeoutMs, 39 ThreadFlags flags); 40 41 // API functions. 42 bool start(); 43 bool inFlight() const; 44 45 // |ParallelTask<T>| implements these hooks. 46 virtual void taskImpl() = 0; 47 virtual void taskDoneImpl() = 0; 48 49 private: 50 class ManagedThread : public ::android::base::Thread { 51 public: ManagedThread(ParallelTaskBase * manager,ThreadFlags flags)52 ManagedThread(ParallelTaskBase* manager, ThreadFlags flags) 53 : Thread(flags), mManager(manager) {} 54 main()55 intptr_t main() { 56 mManager->taskImpl(); 57 // This return value is ignored. 58 return 0; 59 } 60 61 private: 62 ParallelTaskBase* mManager; 63 }; 64 65 // Called prediodically to |tryJoin| the launched thread. 66 static void tryWaitTillJoinedStatic(void* opaqueThis, 67 android::base::Looper::Timer* timer); 68 69 void tryWaitTillJoined(android::base::Looper::Timer* timer); 70 71 android::base::Looper* mLooper; 72 android::base::Looper::Duration mCheckTimeoutMs; 73 ManagedThread mManagedThread; 74 75 bool isRunning = false; 76 std::unique_ptr<android::base::Looper::Timer> mTimer; 77 78 DISALLOW_COPY_AND_ASSIGN(ParallelTaskBase); 79 }; 80 81 } // namespace internal 82 } // namespace base 83 } // namespace android 84