Phil Burk | dec33ab | 2017-01-17 14:48:16 -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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #ifndef AAUDIO_THREAD_H |
| 18 | #define AAUDIO_THREAD_H |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 19 | |
| 20 | #include <atomic> |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 21 | #include <thread> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 22 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 23 | #include <aaudio/AAudio.h> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 24 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 25 | namespace aaudio { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 26 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Abstract class similar to Java Runnable. |
| 29 | */ |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 30 | class Runnable { |
| 31 | public: |
| 32 | Runnable() {}; |
| 33 | virtual ~Runnable() = default; |
| 34 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 35 | virtual void run() = 0; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 39 | * Abstraction for a host dependent thread. |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 40 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 41 | class AAudioThread |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 44 | AAudioThread(); |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 45 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 46 | explicit AAudioThread(const char *prefix); |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 47 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 48 | virtual ~AAudioThread(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Start the thread running. |
| 52 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 53 | aaudio_result_t start(Runnable *runnable = nullptr); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Join the thread. |
| 57 | * The caller must somehow tell the thread to exit before calling join(). |
| 58 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 59 | aaudio_result_t stop(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 60 | |
| 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 | |
| 69 | private: |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 70 | |
| 71 | void setup(const char *prefix); |
| 72 | |
| 73 | Runnable *mRunnable = nullptr; |
| 74 | bool mHasThread = false; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 75 | std::thread mThread; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 76 | |
Phil Burk | 5597889 | 2018-01-11 14:56:09 -0800 | [diff] [blame] | 77 | static std::atomic<uint32_t> mNextThreadIndex; |
| 78 | char mName[16]; // max length for a pthread_name |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 81 | } /* namespace aaudio */ |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 82 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 83 | #endif ///AAUDIO_THREAD_H |