Kea: isc::util::WatchedThread Class Reference

| Kea 3.0.3-git

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

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

## Member Enumeration Documentation

## ◆ WatchType

enum isc::util::WatchedThread::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 35 of file watched_thread.h.

## Constructor & Destructor Documentation

## ◆ WatchedThread()

isc::util::WatchedThread::WatchedThread | ( |  | ) |
| inline

Constructor.

Definition at line 42 of file watched_thread.h.

## ◆ ~WatchedThread()

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

Virtual destructor.

Definition at line 45 of file watched_thread.h.

## Member Function Documentation

## ◆ clearReady()

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

Sets a watch socket state to not ready.

Parameters

  watch_type | indicates which watch socket to clear

Definition at line 57 of file watched_thread.cc.

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

## ◆ getLastError()

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

Fetches the error message text for the most recent error.

Returns
  string containing the error message

Definition at line 97 of file watched_thread.cc.

## ◆ getWatchFd()

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

Fetches the fd of a watch socket.

Parameters

  watch_type | indicates which watch socket

Returns
  the watch socket's file descriptor

Definition at line 42 of file watched_thread.cc.

## ◆ isReady()

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

Indicates if a watch socket state is ready.

Parameters

  watch_type | indicates which watch socket to mark

Returns
  true if the watch socket is ready, false otherwise

Definition at line 52 of file watched_thread.cc.

References isReady().

Referenced by isReady(), and shouldTerminate().

Here is the call graph for this function:

## ◆ isRunning()

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

Returns true if the thread is running.

Definition at line 86 of file watched_thread.h.

## ◆ markReady()

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

Sets a watch socket state to ready.

Parameters

  watch_type | indicates which watch socket to mark

Definition at line 47 of file watched_thread.cc.

Referenced by setError(), and stop().

## ◆ setError()

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.

Parameters

  error_msg | to be set as last error

Definition at line 91 of file watched_thread.cc.

References ERROR, and markReady().

Here is the call graph for this function:

## ◆ shouldTerminate()

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.

Returns
  true if the terminate watch socket is ready

Definition at line 62 of file watched_thread.cc.

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

Here is the call graph for this function:

## ◆ start()

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.

Parameters

  thread_main | function the thread should run

Definition at line 16 of file watched_thread.cc.

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

Here is the call graph for this function:

## ◆ stop()

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.

Here is the call graph for this function:

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

- watched_thread.h
- watched_thread.cc