blob: ed7895b006e6b04f51071e78383988f1f22baed0 [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
40void AAudioThread::setup(const char *prefix) {
Phil Burkc0c70e32017-02-09 13:18:38 -080041 // mThread is a pthread_t of unknown size so we need memset().
Phil Burk5ed503c2017-02-01 09:38:15 -080042 memset(&mThread, 0, sizeof(mThread));
Phil Burk55978892018-01-11 14:56:09 -080043
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 Burk5ed503c2017-02-01 09:38:15 -080050}
51
52void 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.
62static void * AAudioThread_internalThreadProc(void *arg) {
63 AAudioThread *aaudioThread = (AAudioThread *) arg;
64 aaudioThread->dispatch();
65 return nullptr;
66}
67
68aaudio_result_t AAudioThread::start(Runnable *runnable) {
69 if (mHasThread) {
Phil Burkfbf031e2017-10-12 15:58:31 -070070 ALOGE("start() - mHasThread already true");
Phil Burk5ed503c2017-02-01 09:38:15 -080071 return AAUDIO_ERROR_INVALID_STATE;
72 }
Phil Burkc0c70e32017-02-09 13:18:38 -080073 // 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 Burk5ed503c2017-02-01 09:38:15 -080076 int err = pthread_create(&mThread, nullptr, AAudioThread_internalThreadProc, this);
77 if (err != 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070078 ALOGE("start() - pthread_create() returned %d %s", err, strerror(err));
Phil Burkc0c70e32017-02-09 13:18:38 -080079 return AAudioConvert_androidToAAudioResult(-err);
Phil Burk5ed503c2017-02-01 09:38:15 -080080 } else {
Phil Burk55978892018-01-11 14:56:09 -080081 int err = pthread_setname_np(mThread, mName);
82 ALOGW_IF((err != 0), "Could not set name of AAudioThread. err = %d", err);
Phil Burk5ed503c2017-02-01 09:38:15 -080083 mHasThread = true;
84 return AAUDIO_OK;
85 }
86}
87
88aaudio_result_t AAudioThread::stop() {
89 if (!mHasThread) {
Phil Burkfbf031e2017-10-12 15:58:31 -070090 ALOGE("stop() but no thread running");
Phil Burk5ed503c2017-02-01 09:38:15 -080091 return AAUDIO_ERROR_INVALID_STATE;
92 }
93 int err = pthread_join(mThread, nullptr);
94 mHasThread = false;
Phil Burkc0c70e32017-02-09 13:18:38 -080095 if (err != 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070096 ALOGE("stop() - pthread_join() returned %d %s", err, strerror(err));
Phil Burkc0c70e32017-02-09 13:18:38 -080097 return AAudioConvert_androidToAAudioResult(-err);
98 } else {
99 return AAUDIO_OK;
100 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800101}
102