scclib
Stable Cloud Computing C++ Library
event.cc
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 #include <util/event.h>
32 #include <sys/eventfd.h>
33 #include <system_error>
34 #include <cerrno>
35 #include <sstream>
36 #include <util/safe_clib.h>
37 
43 using namespace scc::util;
44 
45 Event::Event(int flags) : m_fd(-1), m_flags(flags)
46 {
47  reset();
48 }
49 
50 Event::~Event()
51 {
52  close();
53 }
54 
55 void Event::close()
56 {
57  if (m_fd != -1)
58  {
59  safe_close(m_fd);
60  }
61 }
62 
63 void Event::reset(int flags)
64 {
65  close();
66  if (flags >= 0)
67  {
68  m_flags = flags;
69  }
70  int m = 0;
71  if ((m_flags & nonblocking) == nonblocking)
72  {
73  m |= EFD_NONBLOCK;
74  }
75  if ((m_flags & semaphore) == semaphore)
76  {
77  m |= EFD_SEMAPHORE;
78  }
79  m_fd = ::eventfd(0, m);
80 }
81 
82 uint64_t Event::read()
83 {
84  uint64_t val;
85  if (::eventfd_read(m_fd, &val) == -1)
86  {
87  std::stringstream st;
88  st << "eventfd_read()";
89  throw std::system_error(errno, std::system_category(), st.str());
90  }
91  return val;
92 }
93 
94 void Event::write(uint64_t v)
95 {
96  if (::eventfd_write(m_fd, v) == -1)
97  {
98  std::stringstream st;
99  st << "eventfd_write()";
100  throw std::system_error(errno, std::system_category(), st.str());
101  }
102 }
uint64_t read()
Read from (decrement) the event counter.
Definition: event.cc:82
void reset(int=-1)
Reset the event.
Definition: event.cc:63
Event(int=0)
Construct an event.
Definition: event.cc:45
void write(uint64_t)
Write to (increment) the event counter.
Definition: event.cc:94
@ 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
Signaling kernel event counter.
int safe_close(int fd)
Signal safe close.
Definition: safe_clib.cc:95
Signal-safe C library wrapper.