1 /*
2  * Copyright (c) 2024 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 #ifndef THREAD_WRAPPER_H
16 #define THREAD_WRAPPER_H
17 
18 #include <string>
19 #include <memory>
20 #include <thread>
21 #include <mutex>
22 #include "nocopyable.h"
23 #include "ffrt_api.h"
24 
25 namespace OHOS {
26 namespace IntellVoiceUtils {
27 enum class TaskQoS {
28     INHERENT = 0,
29     BACKGROUND,
30     UTILITY,
31     DEFAULT,
32     USER_INITIATED,
33     DEADLINE_REQUEST,
34     USER_INTERACTIVE
35 };
36 
37 class ThreadWrapper {
38 public:
39     ThreadWrapper() = default;
40     virtual ~ThreadWrapper();
41     bool Start(const std::string &name, TaskQoS qos = TaskQoS::DEFAULT);
42     void Join();
43 
44 protected:
45     virtual void Run() = 0;
46 
47 private:
48     void RunInThread();
49 #ifdef USE_FFRT
50     ffrt::qos Convert2FfrtQos(TaskQoS taskqos);
51 #endif
52 
53 private:
54     std::unique_ptr<ffrt::thread> thread_ = nullptr;
55     ffrt::mutex mutex_;
56 
57     DISALLOW_COPY(ThreadWrapper);
58     DISALLOW_MOVE(ThreadWrapper);
59 };
60 }
61 }
62 #endif