• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #define LOG_TAG "AAudioThread"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include <pthread.h>
22 
23 #include <aaudio/AAudio.h>
24 #include <utility/AAudioUtilities.h>
25 
26 #include "AAudioThread.h"
27 
28 using namespace aaudio;
29 
30 std::atomic<uint32_t> AAudioThread::mNextThreadIndex{1};
31 
AAudioThread(const char * prefix)32 AAudioThread::AAudioThread(const char *prefix) {
33     setup(prefix);
34 }
35 
AAudioThread()36 AAudioThread::AAudioThread() {
37     setup("AAudio");
38 }
39 
~AAudioThread()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 }
45 
setup(const char * prefix)46 void AAudioThread::setup(const char *prefix) {
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);
53 }
54 
dispatch()55 void AAudioThread::dispatch() {
56     if (mRunnable != nullptr) {
57         mRunnable->run();
58     } else {
59         run();
60     }
61 }
62 
63 // This is the entry point for the new thread created by createThread_l().
64 // It converts the 'C' function call to a C++ method call.
AAudioThread_internalThreadProc(void * arg)65 static void * AAudioThread_internalThreadProc(void *arg) {
66     AAudioThread *aaudioThread = (AAudioThread *) arg;
67     aaudioThread->dispatch();
68     return nullptr;
69 }
70 
start(Runnable * runnable)71 aaudio_result_t AAudioThread::start(Runnable *runnable) {
72     if (mHasThread) {
73         ALOGE("start() - mHasThread already true");
74         return AAUDIO_ERROR_INVALID_STATE;
75     }
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;
79     int err = pthread_create(&mThread, nullptr, AAudioThread_internalThreadProc, this);
80     if (err != 0) {
81         ALOGE("start() - pthread_create() returned %d %s", err, strerror(err));
82         return AAudioConvert_androidToAAudioResult(-err);
83     } else {
84         int err = pthread_setname_np(mThread, mName);
85         ALOGW_IF((err != 0), "Could not set name of AAudioThread. err = %d", err);
86         mHasThread = true;
87         return AAUDIO_OK;
88     }
89 }
90 
stop()91 aaudio_result_t AAudioThread::stop() {
92     if (!mHasThread) {
93         ALOGE("stop() but no thread running");
94         return AAUDIO_ERROR_INVALID_STATE;
95     }
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 
102     int err = pthread_join(mThread, nullptr);
103     if (err != 0) {
104         ALOGE("stop() - pthread_join() returned %d %s", err, strerror(err));
105         return AAudioConvert_androidToAAudioResult(-err);
106     } else {
107         mHasThread = false;
108         return AAUDIO_OK;
109     }
110 }
111