1 /*
2  * Copyright (c) 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 HGM_TASK_HANDLE_THREAD_H
17 #define HGM_TASK_HANDLE_THREAD_H
18 
19 #include "event_handler.h"
20 
21 namespace OHOS::Rosen {
22 class HgmTaskHandleThread {
23 public:
24     static HgmTaskHandleThread& Instance();
GetRunner()25     const std::shared_ptr<AppExecFwk::EventRunner>& GetRunner() const { return runner_; }
26     std::shared_ptr<AppExecFwk::EventHandler> CreateHandler();
27     void PostTask(const std::function<void()>& task, int64_t delayTime = 0);
28     bool PostSyncTask(const std::function<void()>& task);
29     void PostEvent(std::string eventId, const std::function<void()>& task, int64_t delayTime = 0);
30     void DetectMultiThreadingCalls();
31     void RemoveEvent(std::string eventId);
32 
33 private:
34     HgmTaskHandleThread();
35     ~HgmTaskHandleThread() = default;
36     HgmTaskHandleThread(const HgmTaskHandleThread&);
37     HgmTaskHandleThread(const HgmTaskHandleThread&&);
38     HgmTaskHandleThread& operator=(const HgmTaskHandleThread&);
39     HgmTaskHandleThread& operator=(const HgmTaskHandleThread&&);
40 
41     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
42     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
43     int32_t curThreadId_ = -1;
44 };
45 }
46 #endif // HGM_TASK_HANDLE_THREAD_H
47