Kea  2.3.5-git
watched_thread.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 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 <boost/shared_ptr.hpp>
13 
14 #include <functional>
15 #include <mutex>
16 #include <thread>
17 
18 namespace isc {
19 namespace util {
20 
22 typedef boost::shared_ptr<std::thread> ThreadPtr;
23 
32 public:
35  enum WatchType {
36  ERROR = 0,
37  READY = 1,
39  };
40 
43 
45  virtual ~WatchedThread(){}
46 
51  int getWatchFd(WatchType watch_type);
52 
56  void markReady(WatchType watch_type);
57 
62  bool isReady(WatchType watch_type);
63 
67  void clearReady(WatchType watch_type);
68 
76  bool shouldTerminate();
77 
83  void start(const std::function<void()>& thread_main);
84 
86  bool isRunning() {
87  return (thread_ != 0);
88  }
89 
95  void stop();
96 
103  void setError(const std::string& error_msg);
104 
108  std::string getLastError();
109 
110 private:
111 
117  void setErrorInternal(const std::string& error_msg);
118 
120  std::string last_error_;
121 
123  std::mutex mutex_;
124 
133  WatchSocket sockets_[TERMINATE + 1];
134 
136  ThreadPtr thread_ ;
137 };
138 
140 typedef boost::shared_ptr<WatchedThread> WatchedThreadPtr;
141 
142 } // namespace util
143 } // namespace isc
144 
145 #endif // WATCHED_THREAD_H
bool isRunning()
Returns true if the thread is running.
Provides a thread and controls for monitoring its activities.
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.
bool shouldTerminate()
Checks if the thread should terminate.
WatchedThread()
Constructor.
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.
Defines the class, WatchSocket.
boost::shared_ptr< WatchedThread > WatchedThreadPtr
Defines a pointer to a WatchedThread.
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 ...
virtual ~WatchedThread()
Virtual destructor.
boost::shared_ptr< std::thread > ThreadPtr
Thread pointer type.
Provides an IO "ready" semaphore for use with select() or poll() WatchSocket exposes a single open fi...
Definition: watch_socket.h:47
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.