blob: b2774e079c4ea45d840b6544855d5aa62e1e1a95 [file] [log] [blame]
Phil Burkdec33ab2017-01-17 14:48:16 -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 Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_THREAD_H
18#define AAUDIO_THREAD_H
Phil Burkdec33ab2017-01-17 14:48:16 -080019
20#include <atomic>
jiabin2a594622021-10-14 00:32:25 +000021#include <thread>
Phil Burkdec33ab2017-01-17 14:48:16 -080022
Phil Burka4eb0d82017-04-12 15:44:06 -070023#include <aaudio/AAudio.h>
Phil Burkdec33ab2017-01-17 14:48:16 -080024
Phil Burk5ed503c2017-02-01 09:38:15 -080025namespace aaudio {
Phil Burkdec33ab2017-01-17 14:48:16 -080026
Phil Burkc0c70e32017-02-09 13:18:38 -080027/**
28 * Abstract class similar to Java Runnable.
29 */
Phil Burkdec33ab2017-01-17 14:48:16 -080030class Runnable {
31public:
32 Runnable() {};
33 virtual ~Runnable() = default;
34
Phil Burkc0c70e32017-02-09 13:18:38 -080035 virtual void run() = 0;
Phil Burkdec33ab2017-01-17 14:48:16 -080036};
37
38/**
Phil Burkc0c70e32017-02-09 13:18:38 -080039 * Abstraction for a host dependent thread.
Phil Burkdec33ab2017-01-17 14:48:16 -080040 */
Phil Burk5ed503c2017-02-01 09:38:15 -080041class AAudioThread
Phil Burkdec33ab2017-01-17 14:48:16 -080042{
43public:
Phil Burk5ed503c2017-02-01 09:38:15 -080044 AAudioThread();
Phil Burk55978892018-01-11 14:56:09 -080045
Phil Burk19e990e2018-03-22 13:59:34 -070046 explicit AAudioThread(const char *prefix);
Phil Burk55978892018-01-11 14:56:09 -080047
Phil Burkdd582922020-10-15 20:29:51 +000048 virtual ~AAudioThread();
Phil Burkdec33ab2017-01-17 14:48:16 -080049
50 /**
51 * Start the thread running.
52 */
Phil Burk5ed503c2017-02-01 09:38:15 -080053 aaudio_result_t start(Runnable *runnable = nullptr);
Phil Burkdec33ab2017-01-17 14:48:16 -080054
55 /**
56 * Join the thread.
57 * The caller must somehow tell the thread to exit before calling join().
58 */
Phil Burk5ed503c2017-02-01 09:38:15 -080059 aaudio_result_t stop();
Phil Burkdec33ab2017-01-17 14:48:16 -080060
61 /**
62 * This will get called in the thread.
63 * Override this or pass a Runnable to start().
64 */
65 virtual void run() {};
66
67 void dispatch(); // called internally from 'C' thread wrapper
68
69private:
Phil Burk55978892018-01-11 14:56:09 -080070
71 void setup(const char *prefix);
72
73 Runnable *mRunnable = nullptr;
74 bool mHasThread = false;
jiabin2a594622021-10-14 00:32:25 +000075 std::thread mThread;
Phil Burkdec33ab2017-01-17 14:48:16 -080076
Phil Burk55978892018-01-11 14:56:09 -080077 static std::atomic<uint32_t> mNextThreadIndex;
78 char mName[16]; // max length for a pthread_name
Phil Burkdec33ab2017-01-17 14:48:16 -080079};
80
Phil Burk5ed503c2017-02-01 09:38:15 -080081} /* namespace aaudio */
Phil Burkdec33ab2017-01-17 14:48:16 -080082
Phil Burk5ed503c2017-02-01 09:38:15 -080083#endif ///AAUDIO_THREAD_H