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> |
| 21 | #include <pthread.h> |
| 22 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 23 | #include <aaudio/AAudioDefinitions.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 | |
| 27 | class Runnable { |
| 28 | public: |
| 29 | Runnable() {}; |
| 30 | virtual ~Runnable() = default; |
| 31 | |
| 32 | virtual void run() {} |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Abstraction for a host thread. |
| 37 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 38 | class AAudioThread |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 41 | AAudioThread(); |
| 42 | AAudioThread(Runnable *runnable); |
| 43 | virtual ~AAudioThread() = default; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Start the thread running. |
| 47 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 48 | aaudio_result_t start(Runnable *runnable = nullptr); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Join the thread. |
| 52 | * The caller must somehow tell the thread to exit before calling join(). |
| 53 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 54 | aaudio_result_t stop(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * This will get called in the thread. |
| 58 | * Override this or pass a Runnable to start(). |
| 59 | */ |
| 60 | virtual void run() {}; |
| 61 | |
| 62 | void dispatch(); // called internally from 'C' thread wrapper |
| 63 | |
| 64 | private: |
| 65 | Runnable* mRunnable = nullptr; // TODO make atomic with memory barrier? |
| 66 | bool mHasThread = false; |
| 67 | pthread_t mThread; // initialized in constructor |
| 68 | |
| 69 | }; |
| 70 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 71 | } /* namespace aaudio */ |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 72 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 73 | #endif ///AAUDIO_THREAD_H |