Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
AutoSyncFileFlow.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_AUTOSYNCFILEFLOW_H_
36#define _UTILS_AUTOSYNCFILEFLOW_H_
37
38#include "executor/Service.hxx"
41
45{
46public:
54 , int sync_fd
55 , uint64_t interval=SEC_TO_NSEC(1))
57 , fd_(sync_fd)
58 , interval_(interval)
59 , name_(StringPrintf("AutoSyncFileFlow(%d)", fd_))
60 {
61 HASSERT(fd_ >= 0);
62 HASSERT(interval_ > 0);
63 start_flow(STATE(sleep_and_call_sync));
64 }
65
70 {
71 shutdown_ = true;
72 std::swap(shutdownDone_, n);
73 if (n)
74 {
75 n->notify();
76 }
77 timer_.ensure_triggered();
78 }
79
80private:
81 const int fd_;
82 const uint64_t interval_;
83 const std::string name_;
84 StateFlowTimer timer_{this};
85 bool shutdown_{false};
86 Notifiable *shutdownDone_{nullptr};
87
88 Action sleep_and_call_sync()
89 {
90 return sleep_and_call(&timer_, interval_, STATE(sync));
91 }
92
93 Action sync()
94 {
95 if (shutdown_)
96 {
97 AutoNotify n(shutdownDone_);
98 return exit();
99 }
100 ERRNOCHECK(name_.c_str(), fsync(fd_));
101 return call_immediately(STATE(sleep_and_call_sync));
102 }
103};
104
105#endif // _UTILS_AUTOSYNCFILEFLOW_H_
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
This class sends a notification in its destructor.
Simple state flow to configure automatic calls to fsync on a single file handle at regular intervals.
AutoSyncFileFlow(Service *service, int sync_fd, uint64_t interval=SEC_TO_NSEC(1))
Constructor.
void shutdown(Notifiable *n)
Requests the discontinuation of automatic calls to fsync.
An object that can schedule itself on an executor to run.
virtual void notify()=0
Generic callback.
Collection of related state machines that pend on incoming messages.
Base class for state machines.
Service * service()
Return a pointer to the service I am bound to.
Action exit()
Terminate current StateFlow activity.
void start_flow(Callback c)
Resets the flow to the specified state and starts it.
Action call_immediately(Callback c)
Imediately call the next state upon return.
Action sleep_and_call(::Timer *timer, long long timeout_nsec, Callback c)
Suspends execution of this control flow for a specified time.
void ensure_triggered()
Triggers the timer if it is not expired yet.
Definition Timer.hxx:249
#define ERRNOCHECK(where, x...)
Calls the function x, and if the return value is negative, prints errno as error message to stderr an...
Definition logging.h:174
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define SEC_TO_NSEC(_sec)
Convert a second value to a nanosecond value.
Definition os.h:286