AAudioThread.h revision c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fb
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
27c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk/**
28c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * Abstract class similar to Java Runnable.
29c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk */
30dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkclass Runnable {
31dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkpublic:
32dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    Runnable() {};
33dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    virtual ~Runnable() = default;
34dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
35c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    virtual void run() = 0;
36dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk};
37dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
38dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk/**
39c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * Abstraction for a host dependent thread.
40c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * TODO Consider using Android "Thread" class or std::thread instead.
41dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk */
425ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkclass AAudioThread
43dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk{
44dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkpublic:
455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    AAudioThread();
465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    AAudioThread(Runnable *runnable);
475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual ~AAudioThread() = default;
48dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
49dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    /**
50dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     * Start the thread running.
51dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     */
525ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t start(Runnable *runnable = nullptr);
53dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
54dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    /**
55dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     * Join the thread.
56dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     * The caller must somehow tell the thread to exit before calling join().
57dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     */
585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t stop();
59dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
60dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    /**
61dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     * This will get called in the thread.
62dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     * Override this or pass a Runnable to start().
63dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk     */
64dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    virtual void run() {};
65dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
66dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk    void dispatch(); // called internally from 'C' thread wrapper
67dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
68dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkprivate:
69c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    Runnable    *mRunnable;
70c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    bool         mHasThread;
71c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    pthread_t    mThread; // initialized in constructor
72dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
73dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk};
74dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
76dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk
775ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif ///AAUDIO_THREAD_H
78