1 /*
2  * Copyright (c) 2020 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 /**
17  * @addtogroup AbilityKit
18  * @{
19  *
20  * @brief Provides ability-related functions, including ability lifecycle callbacks and functions for connecting to or
21  *        disconnecting from Particle Abilities.
22  *
23  * Abilities are classified into Feature Abilities and Particle Abilities. Feature Abilities support the Page template,
24  * and Particle Abilities support the Service template. An ability using the Page template is called a Page ability for
25  * short and that using the Service template is called a Service ability.
26  *
27  * @since 1.0
28  * @version 1.0
29  */
30 
31 /**
32  * @file ability_event_handler.h
33  *
34  * @brief Declares functions for performing operations during inter-thread communication, including running and
35  *        quitting the event loop of the current thread and posting tasks to an asynchronous thread.
36  *
37  * @since 1.0
38  * @version 1.0
39  */
40 
41 #ifndef OHOS_ABILITY_EVENT_HANDLER_H
42 #define OHOS_ABILITY_EVENT_HANDLER_H
43 
44 #include <functional>
45 #include <pthread.h>
46 #include <queue>
47 
48 namespace OHOS {
49 /**
50  * @brief Declares functions for performing operations during inter-thread communication, including running and
51  *        quitting the event loop of the current thread and posting tasks to an asynchronous thread.
52  *
53  * @since 1.0
54  * @version 1.0
55  */
56 class AbilityEventHandler {
57 public:
58     using Task = std::function<void()>;
59 
60     AbilityEventHandler();
61     ~AbilityEventHandler();
62 
63     /**
64      * @brief Starts running the event loop of the current thread.
65      */
66     void Run();
67 
68     /**
69      * @brief Posts a task to an asynchronous thread.
70      *
71      * @param task Indicates the task to post.
72      */
73     void PostTask(const Task &task);
74 
75     /**
76      * @brief Quits the event loop of the current thread.
77      */
78     void PostQuit();
79 
80     /**
81      * @brief Obtains the event handler of the current thread.
82      *
83      * @return Returns the pointer to the {@link AbilityEventHandler} object of the current thread.
84      */
85     static AbilityEventHandler* GetCurrentHandler();
86 private:
87     std::queue<Task> taskQueue_;
88 
89     pthread_cond_t pthreadCond_ = PTHREAD_COND_INITIALIZER;
90     pthread_mutex_t queueMutex_ = PTHREAD_MUTEX_INITIALIZER;
91 
92     bool quit_ { false };
93 
94     AbilityEventHandler(const AbilityEventHandler&) = delete;
95     AbilityEventHandler& operator = (const AbilityEventHandler&) = delete;
96     AbilityEventHandler(AbilityEventHandler&&) = delete;
97     AbilityEventHandler& operator= (AbilityEventHandler&&) = delete;
98 };
99 }  // namespace OHOS
100 #endif  // OHOS_ABILITY_EVENT_HANDLER_H
101