Kea 3.3.3
watched_thread.h
Go to the documentation of this file.
1// Copyright (C) 2018-2026 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#ifndef WATCHED_THREAD_H
8#define WATCHED_THREAD_H
9
10#include <util/watch_socket.h>
11
12#include <cstdint>
13#include <functional>
14#include <mutex>
15#include <thread>
16
17#include <boost/shared_ptr.hpp>
18
19namespace isc {
20namespace util {
21
23typedef boost::shared_ptr<std::thread> ThreadPtr;
24
33public:
36 enum WatchType : uint16_t {
37 ERROR = 0,
38 READY = 1,
40 };
41
44
46 virtual ~WatchedThread(){}
47
52 int getWatchFd(WatchType watch_type);
53
57 void markReady(WatchType watch_type);
58
63 bool isReady(WatchType watch_type);
64
68 void clearReady(WatchType watch_type);
69
77 bool shouldTerminate();
78
84 void start(const std::function<void()>& thread_main);
85
87 bool isRunning() {
88 return (thread_ != 0);
89 }
90
96 void stop();
97
104 void setError(const std::string& error_msg);
105
109 std::string getLastError();
110
111private:
112
118 void setErrorInternal(const std::string& error_msg);
119
121 std::string last_error_;
122
124 std::mutex mutex_;
125
134 WatchSocket sockets_[TERMINATE + 1];
135
137 ThreadPtr thread_ ;
138};
139
141typedef boost::shared_ptr<WatchedThread> WatchedThreadPtr;
142
143} // namespace util
144} // namespace isc
145
146#endif // WATCHED_THREAD_H
Provides an IO "ready" semaphore for use with select() or poll() WatchSocket exposes a single open fi...
void start(const std::function< void()> &thread_main)
Creates and runs the thread.
int getWatchFd(WatchType watch_type)
Fetches the fd of a watch socket.
bool shouldTerminate()
Checks if the thread should terminate.
void markReady(WatchType watch_type)
Sets a watch socket state to ready.
bool isReady(WatchType watch_type)
Indicates if a watch socket state is ready.
std::string getLastError()
Fetches the error message text for the most recent error.
void clearReady(WatchType watch_type)
Sets a watch socket state to not ready.
WatchType
Enumerates the list of watch sockets used to mark events These are used as arguments to watch socket ...
bool isRunning()
Returns true if the thread is running.
virtual ~WatchedThread()
Virtual destructor.
void stop()
Terminates the thread.
void setError(const std::string &error_msg)
Sets the error state.
boost::shared_ptr< std::thread > ThreadPtr
Thread pointer type.
boost::shared_ptr< WatchedThread > WatchedThreadPtr
Defines a pointer to a WatchedThread.
Defines the logger used by the top-level component of kea-lfc.
Defines the class, WatchSocket.