Kea  1.5.0
isc::util::thread::WatchedThread Class Reference

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. More...
 
virtual ~WatchedThread ()
 Virtual destructor. More...
 
void clearReady (WatchType watch_type)
 Sets a watch socket state to not ready. More...
 
std::string getLastError ()
 Fetches the error message text for the most recent error. More...
 
int getWatchFd (WatchType watch_type)
 Fetches the fd of a watch socket. More...
 
bool isReady (WatchType watch_type)
 Indicates if a watch socket state is ready. More...
 
bool isRunning ()
 Returns true if the thread is running. More...
 
void markReady (WatchType watch_type)
 Sets a watch socket state to ready. More...
 
void setError (const std::string &error_msg)
 Sets the error state. More...
 
bool shouldTerminate ()
 Checks if the thread should terminate. More...
 
void start (const boost::function< void()> &thread_main)
 Creates and runs the thread. More...
 
void stop ()
 Terminates the thread. More...
 

Public Attributes

std::string last_error_
 Error message of the last error encountered. More...
 
WatchSocket sockets_ [TERMINATE+1]
 WatchSockets that are used to communicate with the owning thread There are three: More...
 
thread::ThreadPtr thread_
 Current thread instance. More...
 

Detailed Description

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 26 of file watched_thread.h.

Member Enumeration Documentation

◆ WatchType

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 30 of file watched_thread.h.

Constructor & Destructor Documentation

◆ WatchedThread()

isc::util::thread::WatchedThread::WatchedThread ( )
inline

Constructor.

Definition at line 37 of file watched_thread.h.

◆ ~WatchedThread()

virtual isc::util::thread::WatchedThread::~WatchedThread ( )
inlinevirtual

Virtual destructor.

Definition at line 40 of file watched_thread.h.

Member Function Documentation

◆ clearReady()

void isc::util::thread::WatchedThread::clearReady ( WatchType  watch_type)

Sets a watch socket state to not ready.

Parameters
watch_typeindicates which watch socket to clear

Definition at line 39 of file watched_thread.cc.

References isc::util::WatchSocket::clearReady(), and sockets_.

Referenced by shouldTerminate(), start(), and stop().

+ Here is the call graph for this function:

◆ getLastError()

std::string isc::util::thread::WatchedThread::getLastError ( )

Fetches the error message text for the most recent error.

Returns
string containing the error message

Definition at line 73 of file watched_thread.cc.

References last_error_.

◆ getWatchFd()

int isc::util::thread::WatchedThread::getWatchFd ( WatchType  watch_type)

Fetches the fd of a watch socket.

Parameters
watch_typeindicates which watch socket
Returns
the watch socket's file descriptor

Definition at line 24 of file watched_thread.cc.

References sockets_.

◆ isReady()

bool isc::util::thread::WatchedThread::isReady ( WatchType  watch_type)

Indicates if a watch socket state is ready.

Parameters
watch_typeindicates which watch socket to mark
Returns
true if the watch socket is ready, false otherwise

Definition at line 34 of file watched_thread.cc.

References sockets_.

Referenced by shouldTerminate().

◆ isRunning()

bool isc::util::thread::WatchedThread::isRunning ( )
inline

Returns true if the thread is running.

Definition at line 81 of file watched_thread.h.

References thread_.

◆ markReady()

void isc::util::thread::WatchedThread::markReady ( WatchType  watch_type)

Sets a watch socket state to ready.

Parameters
watch_typeindicates which watch socket to mark

Definition at line 29 of file watched_thread.cc.

References isc::util::WatchSocket::markReady(), and sockets_.

Referenced by setError(), and stop().

+ Here is the call graph for this function:

◆ setError()

void isc::util::thread::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.

Parameters
error_msg

Definition at line 67 of file watched_thread.cc.

References ERROR, last_error_, and markReady().

+ Here is the call graph for this function:

◆ shouldTerminate()

bool isc::util::thread::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.

Returns
true if the terminate watch socket is ready

Definition at line 44 of file watched_thread.cc.

References clearReady(), isReady(), sockets_, and TERMINATE.

+ Here is the call graph for this function:

◆ start()

void isc::util::thread::WatchedThread::start ( const boost::function< void()> &  thread_main)

Creates and runs the thread.

Creates the thread, passing into it the given function to run.

Parameters
thread_mainfunction the thread should run

Definition at line 15 of file watched_thread.cc.

References clearReady(), ERROR, last_error_, READY, TERMINATE, and thread_.

+ Here is the call graph for this function:

◆ stop()

void isc::util::thread::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 54 of file watched_thread.cc.

References clearReady(), ERROR, last_error_, markReady(), READY, TERMINATE, and thread_.

+ Here is the call graph for this function:

Member Data Documentation

◆ last_error_

std::string isc::util::thread::WatchedThread::last_error_

Error message of the last error encountered.

Definition at line 106 of file watched_thread.h.

Referenced by getLastError(), setError(), start(), and stop().

◆ sockets_

WatchSocket isc::util::thread::WatchedThread::sockets_[TERMINATE+1]

WatchSockets that are used to communicate with the owning thread There are three:

  1. ERROR - Marked as ready by the thread when it experiences an error.
  2. READY - Marked as ready by the thread when it needs attention for a normal event (e.g. a thread used to receive data would mark READY when it has data available)
  3. TERMINATE - Marked as ready by WatchedThread owner to instruct the thread to terminate. Worker functions must monitor TERMINATE by periodically calling shouldTerminate

Definition at line 116 of file watched_thread.h.

Referenced by clearReady(), getWatchFd(), isReady(), markReady(), and shouldTerminate().

◆ thread_

thread::ThreadPtr isc::util::thread::WatchedThread::thread_

Current thread instance.

Definition at line 119 of file watched_thread.h.

Referenced by isRunning(), start(), and stop().


The documentation for this class was generated from the following files: