Kea 2.7.5
|
Provides a thread and controls for monitoring its activities. More...
#include <watched_thread.h>
Public Types | |
enum | WatchType { ERROR = 0 , READY = 1 , TERMINATE = 2 } |
Enumerates the list of watch sockets used to mark events These are used as arguments to watch socket accessor methods. More... | |
Public Member Functions | |
WatchedThread () | |
Constructor. | |
virtual | ~WatchedThread () |
Virtual destructor. | |
void | clearReady (WatchType watch_type) |
Sets a watch socket state to not ready. | |
std::string | getLastError () |
Fetches the error message text for the most recent error. | |
int | getWatchFd (WatchType watch_type) |
Fetches the fd of a watch socket. | |
bool | isReady (WatchType watch_type) |
Indicates if a watch socket state is ready. | |
bool | isRunning () |
Returns true if the thread is running. | |
void | markReady (WatchType watch_type) |
Sets a watch socket state to ready. | |
void | setError (const std::string &error_msg) |
Sets the error state. | |
bool | shouldTerminate () |
Checks if the thread should terminate. | |
void | start (const std::function< void()> &thread_main) |
Creates and runs the thread. | |
void | stop () |
Terminates the thread. | |
Provides a thread and controls for monitoring its activities.
Given a "worker function", this class creates a thread which runs the function and provides the means to monitor the thread for "error" and "ready" conditions, and finally to stop the thread. It uses three WatchSockets: one to indicate an error, one to indicate data is ready, and a third to monitor as a shut-down command.
Definition at line 31 of file watched_thread.h.
Enumerates the list of watch sockets used to mark events These are used as arguments to watch socket accessor methods.
Enumerator | |
---|---|
ERROR | |
READY | |
TERMINATE |
Definition at line 35 of file watched_thread.h.
|
inline |
Constructor.
Definition at line 42 of file watched_thread.h.
|
inlinevirtual |
Virtual destructor.
Definition at line 45 of file watched_thread.h.
void isc::util::WatchedThread::clearReady | ( | WatchType | watch_type | ) |
Sets a watch socket state to not ready.
watch_type | indicates which watch socket to clear |
Definition at line 57 of file watched_thread.cc.
References isc::util::WatchSocket::clearReady().
Referenced by shouldTerminate(), start(), and stop().
std::string isc::util::WatchedThread::getLastError | ( | ) |
Fetches the error message text for the most recent error.
Definition at line 97 of file watched_thread.cc.
int isc::util::WatchedThread::getWatchFd | ( | WatchType | watch_type | ) |
Fetches the fd of a watch socket.
watch_type | indicates which watch socket |
Definition at line 42 of file watched_thread.cc.
bool isc::util::WatchedThread::isReady | ( | WatchType | watch_type | ) |
Indicates if a watch socket state is ready.
watch_type | indicates which watch socket to mark |
Definition at line 52 of file watched_thread.cc.
References isReady().
Referenced by isReady(), and shouldTerminate().
|
inline |
Returns true if the thread is running.
Definition at line 86 of file watched_thread.h.
void isc::util::WatchedThread::markReady | ( | WatchType | watch_type | ) |
Sets a watch socket state to ready.
watch_type | indicates which watch socket to mark |
Definition at line 47 of file watched_thread.cc.
References isc::util::WatchSocket::markReady().
Referenced by setError(), and stop().
void isc::util::WatchedThread::setError | ( | const std::string & | error_msg | ) |
Sets the error state.
This records the given error message and sets the error watch socket to ready.
error_msg | to be set as last error |
Definition at line 91 of file watched_thread.cc.
References ERROR, and markReady().
bool isc::util::WatchedThread::shouldTerminate | ( | ) |
Checks if the thread should terminate.
Performs a "one-shot" check of the terminate watch socket. If it is ready, return true and then clear it, otherwise return false.
Definition at line 62 of file watched_thread.cc.
References clearReady(), isReady(), and TERMINATE.
void isc::util::WatchedThread::start | ( | const std::function< void()> & | thread_main | ) |
Creates and runs the thread.
Creates the thread, passing into it the given function to run.
thread_main | function the thread should run |
Definition at line 16 of file watched_thread.cc.
References clearReady(), ERROR, READY, and TERMINATE.
void isc::util::WatchedThread::stop | ( | ) |
Terminates the thread.
It marks the terminate watch socket ready, and then waits for the thread to stop. At this point, the thread is defunct. This is not done in the destructor to avoid race conditions.
Definition at line 72 of file watched_thread.cc.
References clearReady(), ERROR, markReady(), READY, and TERMINATE.