Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioThread" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 19 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 20 | #include <system_error> |
| 21 | |
| 22 | #include <utils/Log.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 23 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 24 | #include <aaudio/AAudio.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <utility/AAudioUtilities.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | |
| 27 | #include "AAudioThread.h" |
| 28 | |
| 29 | using namespace aaudio; |
| 30 | |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 31 | std::atomic<uint32_t> AAudioThread::mNextThreadIndex{1}; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 33 | AAudioThread::AAudioThread(const char *prefix) { |
| 34 | setup(prefix); |
| 35 | } |
| 36 | |
| 37 | AAudioThread::AAudioThread() { |
| 38 | setup("AAudio"); |
| 39 | } |
| 40 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 41 | AAudioThread::~AAudioThread() { |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 42 | ALOGE_IF(mThread.get_id() == std::this_thread::get_id(), |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 43 | "%s() destructor running in thread", __func__); |
| 44 | ALOGE_IF(mHasThread, "%s() thread never joined", __func__); |
| 45 | } |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 46 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 47 | void AAudioThread::setup(const char *prefix) { |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 48 | // Name the thread with an increasing index, "prefix_#", for debugging. |
| 49 | uint32_t index = mNextThreadIndex++; |
| 50 | // Wrap the index so that we do not hit the 16 char limit |
| 51 | // and to avoid hard-to-read large numbers. |
| 52 | index = index % 100000; // arbitrary |
| 53 | snprintf(mName, sizeof(mName), "%s_%u", prefix, index); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void AAudioThread::dispatch() { |
| 57 | if (mRunnable != nullptr) { |
| 58 | mRunnable->run(); |
| 59 | } else { |
| 60 | run(); |
| 61 | } |
| 62 | } |
| 63 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 64 | aaudio_result_t AAudioThread::start(Runnable *runnable) { |
| 65 | if (mHasThread) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 66 | ALOGE("start() - mHasThread already true"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 67 | return AAUDIO_ERROR_INVALID_STATE; |
| 68 | } |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 69 | // mRunnable will be read by the new thread when it starts. A std::thread is created. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 70 | mRunnable = runnable; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 71 | mHasThread = true; |
| 72 | mThread = std::thread(&AAudioThread::dispatch, this); |
| 73 | return AAUDIO_OK; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | aaudio_result_t AAudioThread::stop() { |
| 77 | if (!mHasThread) { |
jiabin | 3b759c6 | 2023-05-30 19:18:15 +0000 | [diff] [blame] | 78 | // There can be cases that the thread is just created but not started. |
| 79 | // Logging as warning to attract attention but not too serious. |
| 80 | ALOGW("stop() but no thread running"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 81 | return AAUDIO_ERROR_INVALID_STATE; |
| 82 | } |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 83 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 84 | if (mThread.get_id() == std::this_thread::get_id()) { |
| 85 | // The thread must not be joined by itself. |
| 86 | ALOGE("%s() attempt to join() from launched thread!", __func__); |
| 87 | return AAUDIO_ERROR_INTERNAL; |
| 88 | } else if (mThread.joinable()) { |
| 89 | // Double check if the thread is joinable to avoid exception when calling join. |
| 90 | mThread.join(); |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 91 | mHasThread = false; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 92 | return AAUDIO_OK; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 93 | } else { |
| 94 | ALOGE("%s() the thread is not joinable", __func__); |
| 95 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 97 | } |