• 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 #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     bool IsNonBlocking() const;
39     bool IsSemaphore() const;
40 
41     uint64_t Take() const;
42     bool Post(uint64_t value) const;
43 
44     EventNotifier(unsigned int initValue, unsigned int mask);
45     EventNotifier(int fd);
46     ~EventNotifier();
47 
48 private:
49     int fd_;
50     unsigned int flags_;
51 
52     DISALLOW_COPY_AND_MOVE(EventNotifier);
53 };
54 
55 #endif // EVENT_NOTIFIER_H