Mikhail Naganov | 0b9c5fe | 2022-08-08 18:28:36 +0000 | [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 <pthread.h> |
| 18 | #include <sched.h> |
| 19 | #include <sys/resource.h> |
| 20 | |
| 21 | #include "include/StreamWorker.h" |
| 22 | |
| 23 | namespace android::hardware::audio::common::internal { |
| 24 | |
| 25 | bool ThreadController::start(const std::string& name, int priority) { |
| 26 | mThreadName = name; |
| 27 | mThreadPriority = priority; |
| 28 | mWorker = std::thread(&ThreadController::workerThread, this); |
| 29 | std::unique_lock<std::mutex> lock(mWorkerLock); |
| 30 | android::base::ScopedLockAssertion lock_assertion(mWorkerLock); |
| 31 | mWorkerCv.wait(lock, [&]() { |
| 32 | android::base::ScopedLockAssertion lock_assertion(mWorkerLock); |
| 33 | return mWorkerState == WorkerState::RUNNING || !mError.empty(); |
| 34 | }); |
| 35 | mWorkerStateChangeRequest = false; |
| 36 | return mWorkerState == WorkerState::RUNNING; |
| 37 | } |
| 38 | |
| 39 | void ThreadController::stop() { |
| 40 | { |
| 41 | std::lock_guard<std::mutex> lock(mWorkerLock); |
| 42 | if (mWorkerState != WorkerState::STOPPED) { |
| 43 | mWorkerState = WorkerState::STOPPED; |
| 44 | mWorkerStateChangeRequest = true; |
| 45 | } |
| 46 | } |
| 47 | if (mWorker.joinable()) { |
| 48 | mWorker.join(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | bool ThreadController::waitForAtLeastOneCycle() { |
| 53 | WorkerState newState; |
| 54 | switchWorkerStateSync(WorkerState::RUNNING, WorkerState::PAUSE_REQUESTED, &newState); |
| 55 | if (newState != WorkerState::PAUSED) return false; |
| 56 | switchWorkerStateSync(newState, WorkerState::RESUME_REQUESTED, &newState); |
| 57 | return newState == WorkerState::RUNNING; |
| 58 | } |
| 59 | |
| 60 | void ThreadController::switchWorkerStateSync(WorkerState oldState, WorkerState newState, |
| 61 | WorkerState* finalState) { |
| 62 | std::unique_lock<std::mutex> lock(mWorkerLock); |
| 63 | android::base::ScopedLockAssertion lock_assertion(mWorkerLock); |
| 64 | if (mWorkerState != oldState) { |
| 65 | if (finalState) *finalState = mWorkerState; |
| 66 | return; |
| 67 | } |
| 68 | mWorkerState = newState; |
| 69 | mWorkerStateChangeRequest = true; |
| 70 | mWorkerCv.wait(lock, [&]() { |
| 71 | android::base::ScopedLockAssertion lock_assertion(mWorkerLock); |
| 72 | return mWorkerState != newState; |
| 73 | }); |
| 74 | if (finalState) *finalState = mWorkerState; |
| 75 | } |
| 76 | |
| 77 | void ThreadController::workerThread() { |
| 78 | using Status = StreamLogic::Status; |
| 79 | |
| 80 | std::string error = mLogic->init(); |
| 81 | if (error.empty() && !mThreadName.empty()) { |
| 82 | std::string compliantName(mThreadName.substr(0, 15)); |
| 83 | if (int errCode = pthread_setname_np(pthread_self(), compliantName.c_str()); errCode != 0) { |
| 84 | error.append("Failed to set thread name: ").append(strerror(errCode)); |
| 85 | } |
| 86 | } |
| 87 | if (error.empty() && mThreadPriority != ANDROID_PRIORITY_DEFAULT) { |
| 88 | if (int result = setpriority(PRIO_PROCESS, 0, mThreadPriority); result != 0) { |
| 89 | int errCode = errno; |
| 90 | error.append("Failed to set thread priority: ").append(strerror(errCode)); |
| 91 | } |
| 92 | } |
| 93 | { |
| 94 | std::lock_guard<std::mutex> lock(mWorkerLock); |
| 95 | mWorkerState = error.empty() ? WorkerState::RUNNING : WorkerState::STOPPED; |
| 96 | mError = error; |
| 97 | } |
| 98 | mWorkerCv.notify_one(); |
| 99 | if (!error.empty()) return; |
| 100 | |
| 101 | for (WorkerState state = WorkerState::RUNNING; state != WorkerState::STOPPED;) { |
| 102 | bool needToNotify = false; |
| 103 | if (Status status = state != WorkerState::PAUSED ? mLogic->cycle() |
| 104 | : (sched_yield(), Status::CONTINUE); |
| 105 | status == Status::CONTINUE) { |
| 106 | { |
| 107 | // See https://developer.android.com/training/articles/smp#nonracing |
| 108 | android::base::ScopedLockAssertion lock_assertion(mWorkerLock); |
| 109 | if (!mWorkerStateChangeRequest.load(std::memory_order_relaxed)) continue; |
| 110 | } |
| 111 | // |
| 112 | // Pause and resume are synchronous. One worker cycle must complete |
| 113 | // before the worker indicates a state change. This is how 'mWorkerState' and |
| 114 | // 'state' interact: |
| 115 | // |
| 116 | // mWorkerState == RUNNING |
| 117 | // client sets mWorkerState := PAUSE_REQUESTED |
| 118 | // last workerCycle gets executed, state := mWorkerState := PAUSED by us |
| 119 | // (or the workers enters the 'error' state if workerCycle fails) |
| 120 | // client gets notified about state change in any case |
| 121 | // thread is doing a busy wait while 'state == PAUSED' |
| 122 | // client sets mWorkerState := RESUME_REQUESTED |
| 123 | // state := mWorkerState (RESUME_REQUESTED) |
| 124 | // mWorkerState := RUNNING, but we don't notify the client yet |
| 125 | // first workerCycle gets executed, the code below triggers a client notification |
| 126 | // (or if workerCycle fails, worker enters 'error' state and also notifies) |
| 127 | // state := mWorkerState (RUNNING) |
| 128 | std::lock_guard<std::mutex> lock(mWorkerLock); |
| 129 | if (state == WorkerState::RESUME_REQUESTED) { |
| 130 | needToNotify = true; |
| 131 | } |
| 132 | state = mWorkerState; |
| 133 | if (mWorkerState == WorkerState::PAUSE_REQUESTED) { |
| 134 | state = mWorkerState = WorkerState::PAUSED; |
| 135 | needToNotify = true; |
| 136 | } else if (mWorkerState == WorkerState::RESUME_REQUESTED) { |
| 137 | mWorkerState = WorkerState::RUNNING; |
| 138 | } |
| 139 | } else { |
| 140 | std::lock_guard<std::mutex> lock(mWorkerLock); |
| 141 | if (state == WorkerState::RESUME_REQUESTED || |
| 142 | mWorkerState == WorkerState::PAUSE_REQUESTED) { |
| 143 | needToNotify = true; |
| 144 | } |
| 145 | state = mWorkerState = WorkerState::STOPPED; |
| 146 | if (status == Status::ABORT) { |
| 147 | mError = "Received ABORT from the logic cycle"; |
| 148 | } |
| 149 | } |
| 150 | if (needToNotify) { |
| 151 | { |
| 152 | std::lock_guard<std::mutex> lock(mWorkerLock); |
| 153 | mWorkerStateChangeRequest = false; |
| 154 | } |
| 155 | mWorkerCv.notify_one(); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | } // namespace android::hardware::audio::common::internal |