/// @brief task function which tries to stop the thread pool and then calls
/// @ref runAndWait
void runStopResetAndWait(ThreadPool<CallBack>* thread_pool) {
- EXPECT_THROW(thread_pool->stop(), InvalidOperation);
- EXPECT_THROW(thread_pool->reset(), InvalidOperation);
- EXPECT_NO_THROW(runAndWait());
+ static std::mutex lock;
+ {
+ std::lock_guard<std::mutex> lk(lock);
+ EXPECT_THROW(thread_pool->stop(), InvalidOperation);
+ EXPECT_THROW(thread_pool->reset(), InvalidOperation);
+ }
+ runAndWait();
}
/// @brief reset all counters and internal test state
// add items to running thread pool
for (uint32_t i = 0; i < items_count; ++i) {
- EXPECT_NO_THROW(thread_pool.add(boost::make_shared<CallBack>(call_back)));
+ thread_pool.add(boost::make_shared<CallBack>(call_back));
}
// wait for all items to be processed
clearReady(ERROR);
clearReady(READY);
clearReady(TERMINATE);
- {
- std::lock_guard<std::mutex> lock(last_error_mutex_);
- last_error_ = "no error";
- }
+ setErrorInternal("no error");
thread_.reset(new std::thread(thread_main));
}
clearReady(ERROR);
clearReady(READY);
- std::lock_guard<std::mutex> lock(last_error_mutex_);
- last_error_ = "thread stopped";
+ setErrorInternal("thread stopped");
+}
+
+void
+WatchedThread::setErrorInternal(const std::string& error_msg) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ last_error_ = error_msg;
}
void
WatchedThread::setError(const std::string& error_msg) {
- {
- std::lock_guard<std::mutex> lock(last_error_mutex_);
- last_error_ = error_msg;
- }
+ setErrorInternal(error_msg);
markReady(ERROR);
}
std::string
WatchedThread::getLastError() {
- std::lock_guard<std::mutex> lock(last_error_mutex_);
+ std::lock_guard<std::mutex> lock(mutex_);
return (last_error_);
}
-} // end of namespace isc::util
-} // end of namespace isc
+
+} // namespace util
+} // namespace isc
/// This records the given error message and sets the error watch
/// socket to ready.
///
- /// @param error_msg
+ /// @param error_msg to be set as last error
void setError(const std::string& error_msg);
/// @brief Fetches the error message text for the most recent error
/// @return string containing the error message
std::string getLastError();
+private:
+
+ /// @brief Sets the error state thread safe
+ ///
+ /// This records the given error message
+ ///
+ /// @param error_msg to be set as last error
+ void setErrorInternal(const std::string& error_msg);
+
/// @brief Error message of the last error encountered
std::string last_error_;
- /// @brief Mutex to protect last error message concurrent access
- std::mutex last_error_mutex_;
+ /// @brief Mutex to protect internal state
+ std::mutex mutex_;
/// @brief WatchSockets that are used to communicate with the owning thread
/// There are three:
/// @brief Defines a pointer to a WatchedThread
typedef boost::shared_ptr<WatchedThread> WatchedThreadPtr;
-}; // namespace isc::util
-}; // namespace isc
+} // namespace util
+} // namespace isc
#endif // WATCHED_THREAD_H