1 /** 2 * Copyright (C) 2022 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 17 #ifndef IIMS_MEDIA_THREAD 18 #define IIMS_MEDIA_THREAD 19 20 #include <mutex> 21 22 #define MAX_EVENTHANDLER_NAME 256 23 24 // Thread priority value used with SCH_FIFO scheduling policy to set Real-Time priority. 25 #define THREAD_PRIORITY_REALTIME 2 26 27 /** 28 * @class IImsMediaThread 29 * @brief Base class of thread 30 * - Child class should implement run() method. 31 * - Call StartThread() method to start thread. 32 */ 33 class IImsMediaThread 34 { 35 public: 36 IImsMediaThread(); 37 virtual ~IImsMediaThread(); 38 39 /** 40 * @brief Starts the new thread execution and detaches from the calling thread. 41 * 42 * @param name Optional param. If passed, used to set name for the thread. 43 * Helpful for debugging. 44 */ 45 bool StartThread(const char* name = nullptr); 46 47 /** 48 * @brief Sets given thread priority for a given thread. Used to set realtime thread priority 49 * for time critical thread in the audio graph. 50 * 51 * @param pid Id of the current process 52 * @param tid Id of the thread whose priority should be modified. 53 * @param priority Thread priority value used with SCH_FIFO scheduling policy. 54 * THREAD_PRIORITY_REALTIME can be used to set realtime priority. 55 */ 56 static void SetThreadPriority(pid_t pid, pid_t tid, int priority); 57 58 /** 59 * @brief Sets the stopped flag and the thread's run method should read and return. 60 * Method is asynchronous and returns immediately without waiting for the thread's run 61 * method to return. 62 */ 63 void StopThread(); 64 65 /** 66 * @brief Used to check if the thread is running. 67 */ 68 bool IsThreadStopped(); 69 70 /** 71 * @brief Should be implemented by the derived call. Called by new thread when StartThread is 72 * invoked. 73 */ 74 virtual void* run() = 0; 75 76 private: 77 std::mutex mThreadMutex; 78 bool mThreadStopped; 79 }; 80 81 #endif