1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HISTREAMER_FOUNDATION_OSAL_THREAD_H 17 #define HISTREAMER_FOUNDATION_OSAL_THREAD_H 18 19 #include <functional> 20 #include <memory> // NOLINT 21 #include <pthread.h> 22 #include <string> 23 #include "osal/task/mutex.h" 24 25 namespace OHOS { 26 namespace Media { 27 enum class ThreadPriority : int { 28 LOW = 9, 29 NORMAL = 16, 30 MIDDLE = 24, 31 HIGH = 32, 32 HIGHEST = 39, 33 }; 34 35 class Thread { 36 public: 37 explicit Thread(ThreadPriority priority = ThreadPriority::HIGH) noexcept; 38 39 Thread(const Thread&) = delete; 40 41 Thread& operator=(const Thread&) = delete; 42 43 Thread(Thread&& other) noexcept; 44 45 Thread& operator=(Thread&& other) noexcept; 46 47 virtual ~Thread() noexcept; 48 49 bool HasThread() const noexcept; 50 51 void SetName(const std::string& name); 52 53 bool CreateThread(const std::function<void()>& func); 54 55 bool IsRunningInSelf(); 56 57 void UpdateThreadPriority(const uint32_t newPriority, const std::string &strBundleName); 58 59 private: 60 struct State { 61 virtual ~State() = default; 62 std::function<void()> func{}; 63 std::string name; 64 }; 65 66 void SetNameInternal(); 67 static void* Run(void* arg); // NOLINT: void* 68 69 pthread_t id_{}; 70 std::string name_; 71 ThreadPriority priority_; 72 std::unique_ptr<State> state_{}; 73 mutable Mutex mutex_{}; 74 std::atomic<bool> isExistThread_{false}; 75 }; 76 } // namespace Media 77 } // namespace OHOS 78 #endif // HISTREAMER_FOUNDATION_OSAL_THREAD_H 79