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/threads/Types.h"
18 #include "aemu/base/TypeTraits.h"
19 
20 #include <memory>
21 
22 namespace android {
23 namespace base {
24 
25 // Run a specified functor in a separate thread,
26 // not waiting for any kind of result
27 // returns true if thread starts successfully
28 bool async(const ThreadFunctor& func,
29            ThreadFlags flags = ThreadFlags::MaskSignals);
30 
31 template <class Callable, class = enable_if<is_callable_as<Callable, void()>>>
32 bool async(Callable&& func, ThreadFlags flags = ThreadFlags::MaskSignals) {
33     return async(ThreadFunctor([func]() { func(); return intptr_t(); }), flags);
34 }
35 
36 class Looper;
37 
38 // Creates a new thread with its own looper.
39 class AsyncThreadWithLooper {
40 public:
41     AsyncThreadWithLooper();
42     ~AsyncThreadWithLooper();
43 
44     Looper* getLooper();
45 
46 private:
47     class Impl;
48     std::unique_ptr<Impl> mImpl;
49 };
50 
51 }  // namespace base
52 }  // namespace android
53