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 #include "aemu/base/threads/internal/ParallelTaskBase.h"
16
17 namespace android {
18 namespace base {
19 namespace internal {
20
ParallelTaskBase(Looper * looper,Looper::Duration checkTimeoutMs,ThreadFlags flags)21 ParallelTaskBase::ParallelTaskBase(Looper* looper,
22 Looper::Duration checkTimeoutMs,
23 ThreadFlags flags)
24 : mLooper(looper),
25 mCheckTimeoutMs(checkTimeoutMs),
26 mManagedThread(this, flags) {}
27
start()28 bool ParallelTaskBase::start() {
29 if (!mManagedThread.start()) {
30 return false;
31 }
32 mTimer.reset(mLooper->createTimer(&tryWaitTillJoinedStatic, this));
33 mTimer->startRelative(0);
34 isRunning = true;
35 return true;
36 }
37
inFlight() const38 bool ParallelTaskBase::inFlight() const {
39 return isRunning;
40 }
41
42 // static
tryWaitTillJoinedStatic(void * opaqueThis,Looper::Timer * timer)43 void ParallelTaskBase::tryWaitTillJoinedStatic(void* opaqueThis,
44 Looper::Timer* timer) {
45 static_cast<ParallelTaskBase*>(opaqueThis)->tryWaitTillJoined(timer);
46 }
47
tryWaitTillJoined(Looper::Timer * timer)48 void ParallelTaskBase::tryWaitTillJoined(Looper::Timer* timer) {
49
50 if (!mManagedThread.tryWait(nullptr)) {
51 mTimer->startRelative(mCheckTimeoutMs);
52 return;
53 }
54
55 isRunning = false;
56 taskDoneImpl();
57 }
58
59 } // namespace internal
60 } // namespace base
61 } // namespace android
62