blob: 502d773eb4d3fe16a62da361beefb8e017dbf2f2 [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
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 Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "AAudioThread"
Phil Burk5ed503c2017-02-01 09:38:15 -080018//#define LOG_NDEBUG 0
Phil Burk5ed503c2017-02-01 09:38:15 -080019
jiabin2a594622021-10-14 00:32:25 +000020#include <system_error>
21
22#include <utils/Log.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080023
Phil Burka4eb0d82017-04-12 15:44:06 -070024#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include <utility/AAudioUtilities.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080026
27#include "AAudioThread.h"
28
29using namespace aaudio;
30
Phil Burk55978892018-01-11 14:56:09 -080031std::atomic<uint32_t> AAudioThread::mNextThreadIndex{1};
Phil Burk5ed503c2017-02-01 09:38:15 -080032
Phil Burk55978892018-01-11 14:56:09 -080033AAudioThread::AAudioThread(const char *prefix) {
34 setup(prefix);
35}
36
37AAudioThread::AAudioThread() {
38 setup("AAudio");
39}
40
Phil Burkdd582922020-10-15 20:29:51 +000041AAudioThread::~AAudioThread() {
jiabin2a594622021-10-14 00:32:25 +000042 ALOGE_IF(mThread.get_id() == std::this_thread::get_id(),
Phil Burkdd582922020-10-15 20:29:51 +000043 "%s() destructor running in thread", __func__);
44 ALOGE_IF(mHasThread, "%s() thread never joined", __func__);
45}
Phil Burk55978892018-01-11 14:56:09 -080046
Phil Burkdd582922020-10-15 20:29:51 +000047void AAudioThread::setup(const char *prefix) {
Phil Burk55978892018-01-11 14:56:09 -080048 // 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 Burk5ed503c2017-02-01 09:38:15 -080054}
55
56void AAudioThread::dispatch() {
57 if (mRunnable != nullptr) {
58 mRunnable->run();
59 } else {
60 run();
61 }
62}
63
Phil Burk5ed503c2017-02-01 09:38:15 -080064aaudio_result_t AAudioThread::start(Runnable *runnable) {
65 if (mHasThread) {
Phil Burkfbf031e2017-10-12 15:58:31 -070066 ALOGE("start() - mHasThread already true");
Phil Burk5ed503c2017-02-01 09:38:15 -080067 return AAUDIO_ERROR_INVALID_STATE;
68 }
jiabin2a594622021-10-14 00:32:25 +000069 // mRunnable will be read by the new thread when it starts. A std::thread is created.
Phil Burkc0c70e32017-02-09 13:18:38 -080070 mRunnable = runnable;
jiabin2a594622021-10-14 00:32:25 +000071 mHasThread = true;
72 mThread = std::thread(&AAudioThread::dispatch, this);
73 return AAUDIO_OK;
Phil Burk5ed503c2017-02-01 09:38:15 -080074}
75
76aaudio_result_t AAudioThread::stop() {
77 if (!mHasThread) {
jiabin3b759c62023-05-30 19:18:15 +000078 // 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 Burk5ed503c2017-02-01 09:38:15 -080081 return AAUDIO_ERROR_INVALID_STATE;
82 }
Phil Burkdd582922020-10-15 20:29:51 +000083
jiabin2a594622021-10-14 00:32:25 +000084 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 Burkdd582922020-10-15 20:29:51 +000091 mHasThread = false;
Phil Burkc0c70e32017-02-09 13:18:38 -080092 return AAUDIO_OK;
jiabin2a594622021-10-14 00:32:25 +000093 } else {
94 ALOGE("%s() the thread is not joinable", __func__);
95 return AAUDIO_ERROR_INTERNAL;
Phil Burkc0c70e32017-02-09 13:18:38 -080096 }
Phil Burk5ed503c2017-02-01 09:38:15 -080097}