Kea  2.3.5-git
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 
12 namespace isc {
13 namespace util {
14 
15 void
16 WatchedThread::start(const std::function<void()>& thread_main) {
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 
41 int
43  return(sockets_[watch_type].getSelectFd());
44 }
45 
46 void
48  sockets_[watch_type].markReady();
49 }
50 
51 bool
53  return (sockets_[watch_type].isReady());
54 }
55 
56 void
58  sockets_[watch_type].clearReady();
59 }
60 
61 bool
63  if (sockets_[TERMINATE].isReady()) {
65  return (true);
66  }
67 
68  return (false);
69 }
70 
71 void
73  if (thread_) {
75  thread_->join();
76  thread_.reset();
77  }
78 
81  setErrorInternal("thread stopped");
82 }
83 
84 void
85 WatchedThread::setErrorInternal(const std::string& error_msg) {
86  std::lock_guard<std::mutex> lock(mutex_);
87  last_error_ = error_msg;
88 }
89 
90 void
91 WatchedThread::setError(const std::string& error_msg) {
92  setErrorInternal(error_msg);
94 }
95 
96 std::string
98  std::lock_guard<std::mutex> lock(mutex_);
99  return (last_error_);
100 }
101 
102 } // namespace util
103 } // namespace isc
std::string getLastError()
Fetches the error message text for the most recent error.
void stop()
Terminates the thread.
int getWatchFd(WatchType watch_type)
Fetches the fd of a watch socket.
void clearReady()
Clears the socket&#39;s ready to read marker.
bool shouldTerminate()
Checks if the thread should terminate.
void start(const std::function< void()> &thread_main)
Creates and runs the thread.
bool isReady(WatchType watch_type)
Indicates if a watch socket state is ready.
void setError(const std::string &error_msg)
Sets the error state.
WatchType
Enumerates the list of watch sockets used to mark events These are used as arguments to watch socket ...
void markReady()
Marks the select-fd as ready to read.
Definition: watch_socket.cc:64
Defines the logger used by the top-level component of kea-lfc.
void clearReady(WatchType watch_type)
Sets a watch socket state to not ready.
void markReady(WatchType watch_type)
Sets a watch socket state to ready.