1 /*
2  * Copyright (c) 2022 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 OHOS_ABILITY_RUNTIME_DELEGATOR_THREAD_H
17 #define OHOS_ABILITY_RUNTIME_DELEGATOR_THREAD_H
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include "event_handler.h"
23 #include "event_runner.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class DelegatorThread {
28 public:
29     /**
30      * Definition of thread task.
31      */
32     using DTask = std::function<void()>;
33 
34 public:
35     /**
36      * A constructor used to create a DelegatorThread instance with the input parameter passed.
37      *
38      * @param isMain Indicates whether to use main thread.
39      */
40     explicit DelegatorThread(bool isMain = false);
41 
42     /**
43      * Default deconstructor used to deconstruct.
44      */
45     ~DelegatorThread() = default;
46 
47     /**
48      * Runs specified task.
49      *
50      * @param task, Indicates the specified task.
51      * @return true if succeed, false otherwise.
52      */
53     bool Run(const DTask &task);
54 
55     /**
56      * Obtains the name of the thread.
57      *
58      * @return the name of the thread.
59      */
60     std::string GetThreadName() const;
61 
62 private:
63     std::string threadName_;
64     std::shared_ptr<EventRunner> runner_;
65     std::shared_ptr<EventHandler> handler_;
66 };
67 }  // namespace AppExecFwk
68 }  // namespace OHOS
69 
70 #endif  // OHOS_ABILITY_RUNTIME_DELEGATOR_THREAD_H
71