Kea: watched_thread.cc Source File

| Kea 2.2.0

watched_thread.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <util/watched_thread.h>
10#include <signal.h>
11
12namespace isc {
13namespace util {
14
15void
16WatchedThread::start(const std::function<void()>& thread_main) {
17 clearReady(ERROR);
18 clearReady(READY);
19 clearReady(TERMINATE);
20 setErrorInternal("no error");
21 // Protect us against signals
22 sigset_t sset;
23 sigset_t osset;
24 sigemptyset(&sset);
25 sigaddset(&sset, SIGCHLD);
26 sigaddset(&sset, SIGINT);
27 sigaddset(&sset, SIGHUP);
28 sigaddset(&sset, SIGTERM);
29 pthread_sigmask(SIG_BLOCK, &sset, &osset);
30 try {
31 thread_.reset(new std::thread(thread_main));
32 } catch (...) {
33 // Restore signal mask.
34 pthread_sigmask(SIG_SETMASK, &osset, 0);
35 throw;
36 }
37 // Restore signal mask.
38 pthread_sigmask(SIG_SETMASK, &osset, 0);
39}
40
41int
42WatchedThread::getWatchFd(WatchType watch_type) {
43 return(sockets_[watch_type].getSelectFd());
44}
45
46void
47WatchedThread::markReady(WatchType watch_type) {
48 sockets_[watch_type].markReady();
49}
50
51bool
52WatchedThread::isReady(WatchType watch_type) {
53 return (sockets_[watch_type].isReady());
54}
55
56void
57WatchedThread::clearReady(WatchType watch_type) {
58 sockets_[watch_type].clearReady();
59}
60
61bool
62WatchedThread::shouldTerminate() {
63 if (sockets_[TERMINATE].isReady()) {
64 clearReady(TERMINATE);
65 return (true);
66 }
67
68 return (false);
69}
70
71void
72WatchedThread::stop() {
73 if (thread_) {
74 markReady(TERMINATE);
75 thread_->join();
76 thread_.reset();
77 }
78
79 clearReady(ERROR);
80 clearReady(READY);
81 setErrorInternal("thread stopped");
82}
83
84void
85WatchedThread::setErrorInternal(const std::string& error_msg) {
86 std::lock_guard<std::mutex> lock(mutex_);
87 last_error_ = error_msg;
88}
89
90void
91WatchedThread::setError(const std::string& error_msg) {
92 setErrorInternal(error_msg);
93 markReady(ERROR);
94}
95
96std::string
97WatchedThread::getLastError() {
98 std::lock_guard<std::mutex> lock(mutex_);
99 return (last_error_);
100}
101
102} // namespace util
103} // namespace isc
isc::util::WatchSocket::clearReady
void clearReady()
Clears the socket's ready to read marker.
Definition: watch_socket.cc:103
isc::util::WatchSocket::markReady
void markReady()
Marks the select-fd as ready to read.
Definition: watch_socket.cc:64
isc::util::WatchedThread::start
void start(const std::function< void()> &thread_main)
Creates and runs the thread.
Definition: watched_thread.cc:16
isc::util::WatchedThread::getWatchFd
int getWatchFd(WatchType watch_type)
Fetches the fd of a watch socket.
Definition: watched_thread.cc:42
isc::util::WatchedThread::shouldTerminate
bool shouldTerminate()
Checks if the thread should terminate.
Definition: watched_thread.cc:62
isc::util::WatchedThread::markReady
void markReady(WatchType watch_type)
Sets a watch socket state to ready.
Definition: watched_thread.cc:47
isc::util::WatchedThread::isReady
bool isReady(WatchType watch_type)
Indicates if a watch socket state is ready.
Definition: watched_thread.cc:52
isc::util::WatchedThread::getLastError
std::string getLastError()
Fetches the error message text for the most recent error.
Definition: watched_thread.cc:97
isc::util::WatchedThread::clearReady
void clearReady(WatchType watch_type)
Sets a watch socket state to not ready.
Definition: watched_thread.cc:57
isc::util::WatchedThread::WatchType
WatchType
Enumerates the list of watch sockets used to mark events These are used as arguments to watch socket ...
Definition: watched_thread.h:35
isc::util::WatchedThread::TERMINATE
@ TERMINATE
Definition: watched_thread.h:38
isc::util::WatchedThread::ERROR
@ ERROR
Definition: watched_thread.h:36
isc::util::WatchedThread::READY
@ READY
Definition: watched_thread.h:37
isc::util::WatchedThread::stop
void stop()
Terminates the thread.
Definition: watched_thread.cc:72
isc::util::WatchedThread::setError
void setError(const std::string &error_msg)
Sets the error state.
Definition: watched_thread.cc:91
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
watched_thread.h