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_IMPL_H 17 #define EVENT_IMPL_H 18 19 #include <climits> 20 #include <condition_variable> 21 #include "ievent.h" 22 23 namespace DistributedDB { 24 class EventLoopImpl; 25 26 class EventImpl : public IEvent { 27 public: 28 explicit EventImpl(EventTime timeout); 29 EventImpl(const EventFd &fd, EventsMask events, EventTime timeout); 30 ~EventImpl() override; 31 32 int SetAction(const EventAction &action, const EventFinalizer &finalizer) override; 33 int AddEvents(EventsMask events) override; 34 int RemoveEvents(EventsMask events) override; 35 int SetTimeout(EventTime timeout) override; 36 int Detach(bool wait) override; 37 void IgnoreFinalizer() override; 38 39 int CheckStatus() const; 40 bool IsTimer() const; 41 bool IsValidFd() const; 42 EventFd GetEventFd() const; 43 EventsMask GetEvents() const; 44 bool SetLoop(EventLoopImpl *loop); 45 void Wait(); 46 bool Attached(const EventLoopImpl *loop, bool &isLoopConfused) const; 47 void SetEvents(bool isAdd, EventsMask events); 48 void SetRevents(EventsMask events); 49 void SetTimeoutPeriod(EventTime timeout); 50 void SetStartTime(EventTime startTime); 51 bool GetTimeoutPoint(EventTime &timePoint) const; 52 void UpdateElapsedTime(EventTime now); 53 int Dispatch(); 54 bool IsValidArg(EventsMask events) const; 55 bool IsValidArg(EventTime timeout) const; 56 DISABLE_COPY_ASSIGN_MOVE(EventImpl); 57 58 static constexpr int MAX_TIME_VALUE = INT_MAX / 2; // half of the max 59 60 private: 61 DECLARE_OBJECT_TAG(EventImpl); 62 63 EventFd fd_; 64 EventsMask events_; 65 EventsMask revents_; 66 EventTime timeout_; // should not < 0 67 EventTime start_; 68 EventLoopImpl *loop_; 69 EventAction action_; 70 EventFinalizer finalizer_; 71 bool ignoreFinalizer_; 72 std::condition_variable detached_; 73 }; 74 } 75 76 #endif // EVENT_IMPL_H 77