1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 #ifndef EVENT_NOTIFIER_H 16 #define EVENT_NOTIFIER_H 17 18 #include <cstdint> 19 #include <memory> 20 #include "nocopyable.h" 21 22 class EventNotifier; 23 24 using EventNotifierPtr = std::shared_ptr<EventNotifier>; 25 26 class EventNotifier { 27 public: 28 enum { 29 NONBLOCK = (1u << 0), 30 SEMAPHORE = (1u << 1), 31 }; 32 33 static EventNotifierPtr Create(unsigned int initValue = 0, unsigned int mask = 0); 34 static EventNotifierPtr CreateWithFd(int fd); 35 36 int GetFd() const; 37 38 uint64_t Take() const; 39 bool Post(uint64_t value) const; 40 41 EventNotifier(unsigned int initValue, unsigned int mask); 42 EventNotifier(int fd); 43 ~EventNotifier(); 44 45 private: 46 int fd_; 47 unsigned int flags_; 48 49 DISALLOW_COPY_AND_MOVE(EventNotifier); 50 }; 51 52 #endif // EVENT_NOTIFIER_H