• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 EVENT_LOOP_IMPL_H
17 #define EVENT_LOOP_IMPL_H
18 
19 #include <list>
20 #include <set>
21 #include <thread>
22 #include "platform_specific.h"
23 #include "../include/ievent_loop.h"
24 #include "../include/ievent.h"
25 
26 #if defined EVLOOP_TIMER_ONLY
27 #define EVENT_LOOP_USE_SELECT
28 #else
29 #define EVENT_LOOP_USE_EPOLL
30 #endif
31 
32 namespace DistributedDB {
33 class EventImpl;
34 class EventRequest;
35 
36 class EventLoopImpl : public IEventLoop {
37 public:
38     EventLoopImpl();
39     ~EventLoopImpl() override;
40     DISABLE_COPY_ASSIGN_MOVE(EventLoopImpl);
41 
42     int Add(IEvent *event) override;
43     int Remove(IEvent *event) override;
44     int Run() override;
45     int Modify(EventImpl *event, bool isAdd, EventsMask events);
46     int Modify(EventImpl *event, EventTime time);
47 
48     // Initialize the loop, code removed from the constructor.
49     virtual int Initialize() = 0;
50     bool IsInLoopThread(bool &started) const;
51 
52 private:
53     virtual int Prepare(const std::set<EventImpl *> &polling) = 0;
54     virtual int Poll(EventTime sleepTime) = 0;
55     virtual int WakeUp() = 0;
56     virtual int Exit(const std::set<EventImpl *> &polling) = 0;
57     virtual int AddEvent(EventImpl *event) = 0;
58     virtual int RemoveEvent(EventImpl *event) = 0;
59     virtual int ModifyEvent(EventImpl *event, bool isAdd, EventsMask events) = 0;
60     virtual EventTime GetTime() const;
61 
62     template<typename T>
63     int QueueRequest(int type, EventImpl *event, T argument);
64     int SendRequestToLoop(EventRequest *eventRequest);
65     bool EventObjectExists(EventImpl *event) const;
66     bool EventFdExists(const EventImpl *event) const;
67     int AddEventObject(EventImpl *event, EventTime now);
68     int RemoveEventObject(EventImpl *event);
69     int ModifyEventObject(EventImpl *event, bool isAdd, EventsMask events);
70     int ModifyEventObject(EventImpl *event, EventTime timeout);
71     void ProcessRequest(std::list<EventRequest *> &requests);
72     int ProcessRequest();
73     EventTime CalSleepTime() const;
74     int DispatchAll();
75     void CleanLoop();
76     void OnKillLoop();
77 
78     std::list<EventRequest *> requests_;
79     std::set<EventImpl *> polling_;
80     bool pollingSetChanged_;
81     std::thread::id loopThread_;
82 };
83 }
84 
85 #endif // EVENT_LOOP_IMPL_H
86