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 | |
| 40 | void AAudioThread::setup(const char *prefix) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 41 | // mThread is a pthread_t of unknown size so we need memset(). |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 42 | memset(&mThread, 0, sizeof(mThread)); |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 43 | |
| 44 | // Name the thread with an increasing index, "prefix_#", for debugging. |
| 45 | uint32_t index = mNextThreadIndex++; |
| 46 | // Wrap the index so that we do not hit the 16 char limit |
| 47 | // and to avoid hard-to-read large numbers. |
| 48 | index = index % 100000; // arbitrary |
| 49 | snprintf(mName, sizeof(mName), "%s_%u", prefix, index); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void AAudioThread::dispatch() { |
| 53 | if (mRunnable != nullptr) { |
| 54 | mRunnable->run(); |
| 55 | } else { |
| 56 | run(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // This is the entry point for the new thread created by createThread(). |
| 61 | // It converts the 'C' function call to a C++ method call. |
| 62 | static void * AAudioThread_internalThreadProc(void *arg) { |
| 63 | AAudioThread *aaudioThread = (AAudioThread *) arg; |
| 64 | aaudioThread->dispatch(); |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | aaudio_result_t AAudioThread::start(Runnable *runnable) { |
| 69 | if (mHasThread) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 70 | ALOGE("start() - mHasThread already true"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 71 | return AAUDIO_ERROR_INVALID_STATE; |
| 72 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 73 | // mRunnable will be read by the new thread when it starts. |
| 74 | // pthread_create() forces a memory synchronization so mRunnable does not need to be atomic. |
| 75 | mRunnable = runnable; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 76 | int err = pthread_create(&mThread, nullptr, AAudioThread_internalThreadProc, this); |
| 77 | if (err != 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 78 | ALOGE("start() - pthread_create() returned %d %s", err, strerror(err)); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | return AAudioConvert_androidToAAudioResult(-err); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 80 | } else { |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 81 | int err = pthread_setname_np(mThread, mName); |
| 82 | 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] | 83 | mHasThread = true; |
| 84 | return AAUDIO_OK; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | aaudio_result_t AAudioThread::stop() { |
| 89 | if (!mHasThread) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 90 | ALOGE("stop() but no thread running"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 91 | return AAUDIO_ERROR_INVALID_STATE; |
| 92 | } |
| 93 | int err = pthread_join(mThread, nullptr); |
| 94 | mHasThread = false; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | if (err != 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 96 | ALOGE("stop() - pthread_join() returned %d %s", err, strerror(err)); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 97 | return AAudioConvert_androidToAAudioResult(-err); |
| 98 | } else { |
| 99 | return AAUDIO_OK; |
| 100 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 101 | } |
| 102 | |