• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 
24 namespace OHOS {
25 namespace Media {
26 namespace OSAL {
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 private:
56     struct State {
57         virtual ~State() = default;
58         std::function<void()> func{};
59         std::string name;
60     };
61 
62     void SetNameInternal();
63     static void* Run(void* arg); // NOLINT: void*
64 
65     pthread_t id_{};
66     std::string name_;
67     ThreadPriority priority_;
68     std::unique_ptr<State> state_{};
69 };
70 } // namespace OSAL
71 } // namespace Media
72 } // namespace OHOS
73 #endif // HISTREAMER_FOUNDATION_OSAL_THREAD_H
74