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 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <pthread.h> |
| 22 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 23 | #include <aaudio/AAudio.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 24 | #include <utility/AAudioUtilities.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 25 | |
| 26 | #include "AAudioThread.h" |
| 27 | |
| 28 | using namespace aaudio; |
| 29 | |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 30 | std::atomic<uint32_t> AAudioThread::mNextThreadIndex{1}; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 32 | AAudioThread::AAudioThread(const char *prefix) { |
| 33 | setup(prefix); |
| 34 | } |
| 35 | |
| 36 | AAudioThread::AAudioThread() { |
| 37 | setup("AAudio"); |
| 38 | } |
| 39 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 40 | AAudioThread::~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 Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 45 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 46 | void AAudioThread::setup(const char *prefix) { |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 47 | // 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 Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void AAudioThread::dispatch() { |
| 56 | if (mRunnable != nullptr) { |
| 57 | mRunnable->run(); |
| 58 | } else { |
| 59 | run(); |
| 60 | } |
| 61 | } |
| 62 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 63 | // This is the entry point for the new thread created by createThread_l(). |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 64 | // It converts the 'C' function call to a C++ method call. |
| 65 | static void * AAudioThread_internalThreadProc(void *arg) { |
| 66 | AAudioThread *aaudioThread = (AAudioThread *) arg; |
| 67 | aaudioThread->dispatch(); |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| 71 | aaudio_result_t AAudioThread::start(Runnable *runnable) { |
| 72 | if (mHasThread) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 73 | ALOGE("start() - mHasThread already true"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 74 | return AAUDIO_ERROR_INVALID_STATE; |
| 75 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 76 | // 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 Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 79 | int err = pthread_create(&mThread, nullptr, AAudioThread_internalThreadProc, this); |
| 80 | if (err != 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 81 | ALOGE("start() - pthread_create() returned %d %s", err, strerror(err)); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 82 | return AAudioConvert_androidToAAudioResult(-err); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 83 | } else { |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 84 | int err = pthread_setname_np(mThread, mName); |
| 85 | ALOGW_IF((err != 0), "Could not set name of AAudioThread. err = %d", err); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 86 | mHasThread = true; |
| 87 | return AAUDIO_OK; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | aaudio_result_t AAudioThread::stop() { |
| 92 | if (!mHasThread) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 93 | ALOGE("stop() but no thread running"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 94 | return AAUDIO_ERROR_INVALID_STATE; |
| 95 | } |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 96 | // 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 Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 102 | int err = pthread_join(mThread, nullptr); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | if (err != 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 104 | ALOGE("stop() - pthread_join() returned %d %s", err, strerror(err)); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 105 | return AAudioConvert_androidToAAudioResult(-err); |
| 106 | } else { |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 107 | mHasThread = false; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 | return AAUDIO_OK; |
| 109 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 110 | } |