Kea  1.5.0
watched_thread.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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>
9 
10 namespace isc {
11 namespace util {
12 namespace thread {
13 
14 void
15 WatchedThread::start(const boost::function<void()>& thread_main) {
19  last_error_ = "no error";
20  thread_.reset(new Thread(thread_main));
21 }
22 
23 int
25  return(sockets_[watch_type].getSelectFd());
26 }
27 
28 void
30  sockets_[watch_type].markReady();
31 }
32 
33 bool
35  return (sockets_[watch_type].isReady());
36 }
37 
38 void
40  sockets_[watch_type].clearReady();
41 }
42 
43 bool
45  if (sockets_[TERMINATE].isReady()) {
47  return (true);
48  }
49 
50  return (false);
51 }
52 
53 void
55  if (thread_) {
57  thread_->wait();
58  thread_.reset();
59  }
60 
63  last_error_ = "thread stopped";
64 }
65 
66 void
67 WatchedThread::setError(const std::string& error_msg) {
68  last_error_ = error_msg;
70 }
71 
72 std::string
74  return (last_error_);
75 }
76 } // end of namespace isc::util::thread
77 } // end of namespace isc::util
78 } // end of namespace isc
isc::util::thread::WatchedThread::last_error_
std::string last_error_
Error message of the last error encountered.
Definition: watched_thread.h:106
isc::util::thread::WatchedThread::READY
@ READY
Definition: watched_thread.h:32
isc::util::thread::WatchedThread::shouldTerminate
bool shouldTerminate()
Checks if the thread should terminate.
Definition: watched_thread.cc:44
isc::util::thread::WatchedThread::clearReady
void clearReady(WatchType watch_type)
Sets a watch socket state to not ready.
Definition: watched_thread.cc:39
isc::util::thread::WatchedThread::isReady
bool isReady(WatchType watch_type)
Indicates if a watch socket state is ready.
Definition: watched_thread.cc:34
isc::util::WatchSocket::clearReady
void clearReady()
Clears the socket's ready to read marker.
Definition: watch_socket.cc:103
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::util::thread::Thread
A separate thread.
Definition: thread.h:36
isc::util::thread::WatchedThread::sockets_
WatchSocket sockets_[TERMINATE+1]
WatchSockets that are used to communicate with the owning thread There are three:
Definition: watched_thread.h:116
isc::util::thread::WatchedThread::ERROR
@ ERROR
Definition: watched_thread.h:31
isc::util::thread::WatchedThread::getWatchFd
int getWatchFd(WatchType watch_type)
Fetches the fd of a watch socket.
Definition: watched_thread.cc:24
isc::util::thread::WatchedThread::start
void start(const boost::function< void()> &thread_main)
Creates and runs the thread.
Definition: watched_thread.cc:15
isc::util::thread::WatchedThread::setError
void setError(const std::string &error_msg)
Sets the error state.
Definition: watched_thread.cc:67
isc::util::thread::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:30
isc::util::thread::WatchedThread::TERMINATE
@ TERMINATE
Definition: watched_thread.h:33
isc::util::thread::WatchedThread::thread_
thread::ThreadPtr thread_
Current thread instance.
Definition: watched_thread.h:119
isc::util::thread::WatchedThread::stop
void stop()
Terminates the thread.
Definition: watched_thread.cc:54
watched_thread.h
isc::util::WatchSocket::markReady
void markReady()
Marks the select-fd as ready to read.
Definition: watch_socket.cc:64
isc::util::thread::WatchedThread::markReady
void markReady(WatchType watch_type)
Sets a watch socket state to ready.
Definition: watched_thread.cc:29
isc::util::thread::WatchedThread::getLastError
std::string getLastError()
Fetches the error message text for the most recent error.
Definition: watched_thread.cc:73