Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SimpleThread.h" |
| 18 | |
| 19 | namespace android { |
| 20 | namespace hardware { |
| 21 | namespace camera { |
| 22 | namespace common { |
| 23 | namespace helper { |
| 24 | |
| 25 | SimpleThread::SimpleThread() : mDone(true), mThread() {} |
| 26 | SimpleThread::~SimpleThread() { |
Shuzhen Wang | 465f11d | 2025-03-10 21:47:33 -0700 | [diff] [blame] | 27 | // b/399939768: We need to be careful calling requestExitAndWait() from |
| 28 | // the destructor due to the possibility of |
| 29 | // OutputThread::threadLoop->~ExternalCameraDeviceSession->~OutputThread::requestExit() |
| 30 | // resulting in join() called on the calling thread. |
| 31 | // |
| 32 | // Rather than removing `requestExitAndExit`, we guard the `join` call |
| 33 | // in it by checking the thread id. |
Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 34 | requestExitAndWait(); |
| 35 | } |
| 36 | |
| 37 | void SimpleThread::run() { |
| 38 | requestExitAndWait(); // Exit current execution, if any. |
| 39 | |
| 40 | // start thread |
| 41 | mDone.store(false, std::memory_order_release); |
| 42 | mThread = std::thread(&SimpleThread::runLoop, this); |
| 43 | } |
| 44 | |
| 45 | void SimpleThread::requestExitAndWait() { |
| 46 | // Signal thread to stop |
| 47 | mDone.store(true, std::memory_order_release); |
| 48 | |
| 49 | // Wait for thread to exit if needed. This should happen in no more than one iteration of |
Shuzhen Wang | 465f11d | 2025-03-10 21:47:33 -0700 | [diff] [blame] | 50 | // threadLoop. Only call 'join' if this function is called from a thread |
| 51 | // different from the thread associated with this object. Otherwise call |
| 52 | // 'detach' so that the threadLoop can finish and clean up itself. |
Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 53 | if (mThread.joinable()) { |
Shuzhen Wang | 465f11d | 2025-03-10 21:47:33 -0700 | [diff] [blame] | 54 | if (mThread.get_id() != std::this_thread::get_id()) { |
| 55 | mThread.join(); |
| 56 | } else { |
| 57 | mThread.detach(); |
| 58 | } |
Avichal Rakesh | e1857f8 | 2022-06-08 17:47:23 -0700 | [diff] [blame] | 59 | } |
| 60 | mThread = std::thread(); |
| 61 | } |
| 62 | |
| 63 | void SimpleThread::runLoop() { |
| 64 | while (!exitPending()) { |
| 65 | if (!threadLoop()) { |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } // namespace helper |
| 72 | } // namespace common |
| 73 | } // namespace camera |
| 74 | } // namespace hardware |
| 75 | } // namespace android |