AAudioThread.h revision 5ed503c7a66c90f93759c90237a9b432dbd93f9f
1dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk/* 2dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Copyright (C) 2016 The Android Open Source Project 3dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * 4dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Licensed under the Apache License, Version 2.0 (the "License"); 5dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * you may not use this file except in compliance with the License. 6dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * You may obtain a copy of the License at 7dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * 8dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * http://www.apache.org/licenses/LICENSE-2.0 9dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * 10dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Unless required by applicable law or agreed to in writing, software 11dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * distributed under the License is distributed on an "AS IS" BASIS, 12dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * See the License for the specific language governing permissions and 14dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * limitations under the License. 15dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk */ 16dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#ifndef AAUDIO_THREAD_H 185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define AAUDIO_THREAD_H 19dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 20dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk#include <atomic> 21dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk#include <pthread.h> 22dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudioDefinitions.h> 24dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 255ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio { 26dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 27dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkclass Runnable { 28dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkpublic: 29dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk Runnable() {}; 30dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk virtual ~Runnable() = default; 31dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 32dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk virtual void run() {} 33dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk}; 34dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 35dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk/** 36dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Abstraction for a host thread. 37dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk */ 385ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkclass AAudioThread 39dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk{ 40dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkpublic: 415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk AAudioThread(); 425ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk AAudioThread(Runnable *runnable); 435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk virtual ~AAudioThread() = default; 44dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 45dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk /** 46dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Start the thread running. 47dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk */ 485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t start(Runnable *runnable = nullptr); 49dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 50dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk /** 51dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Join the thread. 52dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * The caller must somehow tell the thread to exit before calling join(). 53dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk */ 545ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t stop(); 55dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 56dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk /** 57dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * This will get called in the thread. 58dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk * Override this or pass a Runnable to start(). 59dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk */ 60dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk virtual void run() {}; 61dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 62dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk void dispatch(); // called internally from 'C' thread wrapper 63dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 64dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkprivate: 65dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk Runnable* mRunnable = nullptr; // TODO make atomic with memory barrier? 66dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk bool mHasThread = false; 67dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk pthread_t mThread; // initialized in constructor 68dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 69dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk}; 70dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */ 72dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk 735ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif ///AAUDIO_THREAD_H 74