• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_MANAGER_H
17 #define OHOS_SHARING_MANAGER_H
18 
19 #include <atomic>
20 #include <chrono>
21 #include <functional>
22 #include <future>
23 #include <list>
24 #include <memory>
25 #include <mutex>
26 #include <queue>
27 #include <thread>
28 #include <unordered_map>
29 #include "event_base.h"
30 #include "nocopyable.h"
31 #include "singleton.h"
32 #include "taskpool.h"
33 
34 namespace OHOS {
35 namespace Sharing {
36 
37 class EventManager : public Singleton<EventManager>,
38                      public TaskPool {
39 public:
40     EventManager();
41     ~EventManager();
42 
43     int32_t Init();
44     int32_t StartEventLoop();
45     int32_t DrainAllListeners();
46     void StopEventLoop();
47 
48     int32_t PushEvent(const SharingEvent &event);
49     int32_t PushSyncEvent(const SharingEvent &event);
50 
51     int32_t AddListener(std::shared_ptr<EventListener> listener);
52     int32_t DelListener(std::shared_ptr<EventListener> listener);
53 
54 private:
55     void ProcessEvent();
56 
57 private:
58     std::mutex mutex_;
59     std::queue<SharingEvent> events_;
60     std::condition_variable hasEvent_;
61     std::unique_ptr<std::thread> eventLoop_ = nullptr;
62     std::unique_ptr<std::thread> eventThread_ = nullptr;
63     std::unordered_map<ClassType, std::list<std::shared_ptr<EventListener>>> listeners_;
64 };
65 
66 } // namespace Sharing
67 } // namespace OHOS
68 #endif
69