• 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 
setup(const char * prefix)40 void AAudioThread::setup(const char *prefix) {
41     // mThread is a pthread_t of unknown size so we need memset().
42     memset(&mThread, 0, sizeof(mThread));
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);
50 }
51 
dispatch()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.
AAudioThread_internalThreadProc(void * arg)62 static void * AAudioThread_internalThreadProc(void *arg) {
63     AAudioThread *aaudioThread = (AAudioThread *) arg;
64     aaudioThread->dispatch();
65     return nullptr;
66 }
67 
start(Runnable * runnable)68 aaudio_result_t AAudioThread::start(Runnable *runnable) {
69     if (mHasThread) {
70         ALOGE("start() - mHasThread already true");
71         return AAUDIO_ERROR_INVALID_STATE;
72     }
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;
76     int err = pthread_create(&mThread, nullptr, AAudioThread_internalThreadProc, this);
77     if (err != 0) {
78         ALOGE("start() - pthread_create() returned %d %s", err, strerror(err));
79         return AAudioConvert_androidToAAudioResult(-err);
80     } else {
81         int err = pthread_setname_np(mThread, mName);
82         ALOGW_IF((err != 0), "Could not set name of AAudioThread. err = %d", err);
83         mHasThread = true;
84         return AAUDIO_OK;
85     }
86 }
87 
stop()88 aaudio_result_t AAudioThread::stop() {
89     if (!mHasThread) {
90         ALOGE("stop() but no thread running");
91         return AAUDIO_ERROR_INVALID_STATE;
92     }
93     int err = pthread_join(mThread, nullptr);
94     mHasThread = false;
95     if (err != 0) {
96         ALOGE("stop() - pthread_join() returned %d %s", err, strerror(err));
97         return AAudioConvert_androidToAAudioResult(-err);
98     } else {
99         return AAUDIO_OK;
100     }
101 }
102 
103