Skip to content

Commit

Permalink
Move member variables to the private context of the Monitor class
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Aug 5, 2023
1 parent a45cee1 commit f342532
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ auto main() -> int
, [] (const finalcut::Monitor* monitor, short)
{
uint8_t Char{0};
const ssize_t bytes = ::read( monitor->getFd(), &Char, 1);
const ssize_t bytes = ::read(monitor->getFileDescriptor(), &Char, 1);

if ( bytes > 0 )
std::cout << "typed in: '" << Char << "'"
Expand Down
2 changes: 1 addition & 1 deletion final/eventloop/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ inline void EventLoop::processNextEvents()
{
if ( monitor->isActive() )
{
fds[fd_count] = { monitor->getFd(), monitor->getEvents(), 0 };
fds[fd_count] = { monitor->getFileDescriptor(), monitor->getEvents(), 0 };
lookup_table[fd_count] = monitor;
fd_count++;
}
Expand Down
8 changes: 4 additions & 4 deletions final/eventloop/io_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ IoMonitor::~IoMonitor() noexcept = default; // destructor
void IoMonitor::init ( int file_descriptor, short ev
, handler_t hdl, void* uc )
{
fd = file_descriptor;
events = ev;
handler = std::move(hdl);
user_context = uc;
setFileDescriptor (file_descriptor);
setEvents (ev);
setHandler (std::move(hdl));
setUserContext (uc);
}

} // namespace finalcut
17 changes: 8 additions & 9 deletions final/eventloop/kqueue_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,18 @@ KqueueTimer::~KqueueTimer() noexcept // destructor
//----------------------------------------------------------------------
void KqueueTimer::init (handler_t hdl, void* uc)
{
if ( already_initialized )
if ( isInitialized() )
throw monitor_error{"This instance has already been initialised."};

timer_id = getTimerID();
fd = getKqueue();
events = POLLIN;
setFileDescriptor (getKqueue());
setEvents (POLLIN);
setHandler (std::move(KqueueHandler()));
setUserContext (uc);
timer_id = getTimerID();
timer_handler = std::move(hdl);
user_context = uc;
handler = KqueueHandler();
time_events.emplace_back();
timer_nodes.emplace_back(timer_id, this);

already_initialized = true;
setInitialized();
}

//----------------------------------------------------------------------
Expand All @@ -236,7 +235,7 @@ void KqueueTimer::setInterval ( std::chrono::nanoseconds first,
EV_SET(&ev_set, timer_id, EVFILT_TIMER, kq_flags, 0, ms, nullptr);

// Register event with kqueue
if ( ::kevent(fd, &ev_set, 1, nullptr, 0, nullptr) != 0 )
if ( ::kevent(getFileDescriptor(), &ev_set, 1, nullptr, 0, nullptr) != 0 )
throw monitor_error{"Cannot register event in kqueue."};
}

Expand Down
47 changes: 40 additions & 7 deletions final/eventloop/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Monitor

// Accessors
auto getEvents() const -> short;
auto getFd() const -> int;
auto getFileDescriptor() const -> int;
auto getUserContext() const -> void*;

// Inquiry
Expand All @@ -115,20 +115,28 @@ class Monitor
// Constants
static constexpr int NO_FILE_DESCRIPTOR{-1};

// Mutators
void setFileDescriptor (int);
void setEvents (short);
void setHandler (handler_t&&);
void setUserContext (void*);
void setInitialized();

// Inquiry
auto isInitialized() -> bool;

// Methods
virtual void trigger (short);

private:
// Data member
bool active{false};
EventLoop* eventloop{};
int fd{NO_FILE_DESCRIPTOR};
short events{0};
handler_t handler{};
void* user_context{nullptr};
bool already_initialized{false};

private:
// Data member
bool active{false};
bool monitor_initialized{false};

// Friend classes
friend class EventLoop;
Expand All @@ -140,9 +148,10 @@ inline auto Monitor::getEvents() const -> short
{ return events; }

//----------------------------------------------------------------------
inline auto Monitor::getFd() const -> int
inline auto Monitor::getFileDescriptor() const -> int
{ return fd; }


//----------------------------------------------------------------------
inline auto Monitor::getUserContext() const -> void*
{ return user_context; }
Expand All @@ -166,6 +175,30 @@ inline void Monitor::trigger (short return_events)
handler (this, return_events);
}

//----------------------------------------------------------------------
inline void Monitor::setFileDescriptor (int file_descriptor)
{ fd = file_descriptor; }

//----------------------------------------------------------------------
inline void Monitor::setEvents (short ev)
{ events = ev; }

//----------------------------------------------------------------------
inline void Monitor::setHandler (handler_t&& hdl)
{ handler = std::move(hdl); }

//----------------------------------------------------------------------
inline void Monitor::setUserContext (void* uc)
{ user_context = uc; }

//----------------------------------------------------------------------
inline void Monitor::setInitialized()
{ monitor_initialized = true; }

//----------------------------------------------------------------------
inline auto Monitor::isInitialized() -> bool
{ return monitor_initialized; }

} // namespace finalcut

#endif // MONITOR_H
14 changes: 7 additions & 7 deletions final/eventloop/posix_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ PosixTimer::~PosixTimer() noexcept // destructor
//----------------------------------------------------------------------
void PosixTimer::init (handler_t hdl, void* uc)
{
if ( already_initialized )
if ( isInitialized() )
throw monitor_error{"This instance has already been initialised."};

handler = std::move(hdl);
user_context = uc;
events = POLLIN;
setEvents (POLLIN);
setHandler (std::move(hdl));
setUserContext (uc);

if ( ::pipe(alarm_pipe_fd.data()) != 0 )
throw monitor_error{"No pipe could be set up for the timer."};

fd = alarm_pipe_fd[0]; // Read end of pipe
setFileDescriptor(alarm_pipe_fd[0]); // Read end of pipe

struct sigevent sig_event{};
sig_event.sigev_notify = SIGEV_SIGNAL;
Expand All @@ -209,7 +209,7 @@ void PosixTimer::init (handler_t hdl, void* uc)
throw monitor_error{"No POSIX timer could be reserved."};
}

already_initialized = true;
setInitialized();
}

//----------------------------------------------------------------------
Expand All @@ -231,7 +231,7 @@ void PosixTimer::setInterval ( std::chrono::nanoseconds first,
//----------------------------------------------------------------------
void PosixTimer::trigger (short return_events)
{
drainPipe(fd);
drainPipe(getFileDescriptor());
Monitor::trigger(return_events);
}

Expand Down
14 changes: 7 additions & 7 deletions final/eventloop/signal_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ SignalMonitor::~SignalMonitor() noexcept // destructor
//----------------------------------------------------------------------
void SignalMonitor::init (int sn, handler_t hdl, void* uc)
{
if ( already_initialized )
if ( isInitialized() )
throw monitor_error{"This instance has already been initialised."};

handler = std::move(hdl);
user_context = uc;
events = POLLIN;
signal_number = sn;
setEvents (POLLIN);
setHandler (std::move(hdl));
setUserContext (uc);

// SIGALRM is handled by the timer monitor
if ( SIGALRM == sn )
Expand All @@ -126,7 +126,7 @@ void SignalMonitor::init (int sn, handler_t hdl, void* uc)
throw monitor_error{"No pipe could be set up for the signal monitor."};
}

fd = signal_pipe_fd[0]; // Read end of pipe
setFileDescriptor(signal_pipe_fd[0]); // Read end of pipe

// Install signal handler
struct sigaction sig_action{};
Expand All @@ -146,7 +146,7 @@ void SignalMonitor::init (int sn, handler_t hdl, void* uc)

// Enter the monitor instance in the assignment table
signal_monitors[sn] = this;
already_initialized = true;
setInitialized();
}

//----------------------------------------------------------------------
Expand Down Expand Up @@ -176,7 +176,7 @@ void SignalMonitor::onSignal (int signal_number)
//----------------------------------------------------------------------
void SignalMonitor::trigger (short return_events)
{
drainPipe(fd);
drainPipe(getFileDescriptor());
Monitor::trigger(return_events);
}

Expand Down

0 comments on commit f342532

Please sign in to comment.