scclib
Stable Cloud Computing C++ Library
event.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SYS_UTIL_EVENT_H
32 #define _SYS_UTIL_EVENT_H
33 
34 #include <stdint.h>
35 
36 namespace scc::util
37 {
38 
78 class Event
79 {
80  int m_fd;
81  int m_flags;
82 
83  void close();
84 public:
86  enum EventFlag
87  {
89  semaphore = 2
90  };
91 
95  Event(int = 0);
96  virtual ~Event();
97 
99  Event(const Event&) = delete;
101  void operator=(const Event&) = delete;
102 
104  Event(Event&& other) : m_fd{other.m_fd}, m_flags{other.m_flags}
105  {
106  other.m_fd = -1;
107  other.m_flags = 0;
108  }
110  const Event& operator=(Event&& other)
111  {
112  m_fd = other.m_fd;
113  m_flags = other.m_flags;
114  other.m_fd = -1;
115  other.m_flags = 0;
116  return *this;
117  }
118 
120  operator int() const { return m_fd; }
122  int fd() const { return m_fd; }
123 
130  void reset(int = -1);
131 
140  uint64_t read();
141 
145  void write(uint64_t);
146 };
149 }
150 
151 #endif
Signaling kernel event counter.
Definition: event.h:79
uint64_t read()
Read from (decrement) the event counter.
Definition: event.cc:82
Event(Event &&other)
Move constructor.
Definition: event.h:104
void operator=(const Event &)=delete
No copy.
void reset(int=-1)
Reset the event.
Definition: event.cc:63
int fd() const
Return file descriptor.
Definition: event.h:122
const Event & operator=(Event &&other)
Move assignment.
Definition: event.h:110
Event(int=0)
Construct an event.
Definition: event.cc:45
void write(uint64_t)
Write to (increment) the event counter.
Definition: event.cc:94
EventFlag
Event flags.
Definition: event.h:87
@ semaphore
Semaphore mode. Event reads decrement the counter by 1. Otherwise, reads set the counter to 0.
Definition: event.h:89
@ nonblocking
Non-blocking mode. If an event is read while unsignalled, throws and exception.
Definition: event.h:88
Event(const Event &)=delete
No copy.