blob: 68496acc0fd745613b59a93a4e1b9ad06dc27375 [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
19#include <utils/Log.h>
20
21#include <pthread.h>
22
Phil Burka4eb0d82017-04-12 15:44:06 -070023#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080024#include <utility/AAudioUtilities.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080025
26#include "AAudioThread.h"
27
28using namespace aaudio;
29
Phil Burk55978892018-01-11 14:56:09 -080030std::atomic<uint32_t> AAudioThread::mNextThreadIndex{1};
Phil Burk5ed503c2017-02-01 09:38:15 -080031
Phil Burk55978892018-01-11 14:56:09 -080032AAudioThread::AAudioThread(const char *prefix) {
33 setup(prefix);
34}
35
36AAudioThread::AAudioThread() {
37 setup("AAudio");
38}
39
Phil Burkdd582922020-10-15 20:29:51 +000040AAudioThread::~AAudioThread() {
41 ALOGE_IF(pthread_equal(pthread_self(), mThread),
42 "%s() destructor running in thread", __func__);
43 ALOGE_IF(mHasThread, "%s() thread never joined", __func__);
44}
Phil Burk55978892018-01-11 14:56:09 -080045
Phil Burkdd582922020-10-15 20:29:51 +000046void AAudioThread::setup(const char *prefix) {
Phil Burk55978892018-01-11 14:56:09 -080047 // Name the thread with an increasing index, "prefix_#", for debugging.
48 uint32_t index = mNextThreadIndex++;
49 // Wrap the index so that we do not hit the 16 char limit
50 // and to avoid hard-to-read large numbers.
51 index = index % 100000; // arbitrary
52 snprintf(mName, sizeof(mName), "%s_%u", prefix, index);
Phil Burk5ed503c2017-02-01 09:38:15 -080053}
54
55void AAudioThread::dispatch() {
56 if (mRunnable != nullptr) {
57 mRunnable->run();
58 } else {
59 run();
60 }
61}
62
Phil Burkdd582922020-10-15 20:29:51 +000063// This is the entry point for the new thread created by createThread_l().
Phil Burk5ed503c2017-02-01 09:38:15 -080064// It converts the 'C' function call to a C++ method call.
65static void * AAudioThread_internalThreadProc(void *arg) {
66 AAudioThread *aaudioThread = (AAudioThread *) arg;
67 aaudioThread->dispatch();
68 return nullptr;
69}
70
71aaudio_result_t AAudioThread::start(Runnable *runnable) {
72 if (mHasThread) {
Phil Burkfbf031e2017-10-12 15:58:31 -070073 ALOGE("start() - mHasThread already true");
Phil Burk5ed503c2017-02-01 09:38:15 -080074 return AAUDIO_ERROR_INVALID_STATE;
75 }
Phil Burkc0c70e32017-02-09 13:18:38 -080076 // mRunnable will be read by the new thread when it starts.
77 // pthread_create() forces a memory synchronization so mRunnable does not need to be atomic.
78 mRunnable = runnable;
Phil Burk5ed503c2017-02-01 09:38:15 -080079 int err = pthread_create(&mThread, nullptr, AAudioThread_internalThreadProc, this);
80 if (err != 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070081 ALOGE("start() - pthread_create() returned %d %s", err, strerror(err));
Phil Burkc0c70e32017-02-09 13:18:38 -080082 return AAudioConvert_androidToAAudioResult(-err);
Phil Burk5ed503c2017-02-01 09:38:15 -080083 } else {
Phil Burk55978892018-01-11 14:56:09 -080084 int err = pthread_setname_np(mThread, mName);
85 ALOGW_IF((err != 0), "Could not set name of AAudioThread. err = %d", err);
Phil Burk5ed503c2017-02-01 09:38:15 -080086 mHasThread = true;
87 return AAUDIO_OK;
88 }
89}
90
91aaudio_result_t AAudioThread::stop() {
92 if (!mHasThread) {
Phil Burkfbf031e2017-10-12 15:58:31 -070093 ALOGE("stop() but no thread running");
Phil Burk5ed503c2017-02-01 09:38:15 -080094 return AAUDIO_ERROR_INVALID_STATE;
95 }
Phil Burkdd582922020-10-15 20:29:51 +000096 // Check to see if the thread is trying to stop itself.
97 if (pthread_equal(pthread_self(), mThread)) {
98 ALOGE("%s() attempt to pthread_join() from launched thread!", __func__);
99 return AAUDIO_ERROR_INTERNAL;
100 }
101
Phil Burk5ed503c2017-02-01 09:38:15 -0800102 int err = pthread_join(mThread, nullptr);
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 if (err != 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700104 ALOGE("stop() - pthread_join() returned %d %s", err, strerror(err));
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 return AAudioConvert_androidToAAudioResult(-err);
106 } else {
Phil Burkdd582922020-10-15 20:29:51 +0000107 mHasThread = false;
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 return AAUDIO_OK;
109 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800110}