diff --git a/include/datahandlinglibs/FakeCardReaderBase.hpp b/include/datahandlinglibs/FakeCardReaderBase.hpp index c743102..1f21c67 100644 --- a/include/datahandlinglibs/FakeCardReaderBase.hpp +++ b/include/datahandlinglibs/FakeCardReaderBase.hpp @@ -26,7 +26,7 @@ #include "appfwk/ModuleConfiguration.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include "datahandlinglibs/utils/FileSourceBuffer.hpp" #include "nlohmann/json.hpp" diff --git a/include/datahandlinglibs/concepts/RecorderConcept.hpp b/include/datahandlinglibs/concepts/RecorderConcept.hpp index ac5000c..b09da4f 100644 --- a/include/datahandlinglibs/concepts/RecorderConcept.hpp +++ b/include/datahandlinglibs/concepts/RecorderConcept.hpp @@ -12,7 +12,7 @@ #include "datahandlinglibs/ReadoutTypes.hpp" //#include "datahandlinglibs/recorderconfig/Structs.hpp" #include "datahandlinglibs/utils/BufferedFileWriter.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include "appmodel/DataRecorderModule.hpp" #include diff --git a/include/datahandlinglibs/models/DataHandlingModel.hpp b/include/datahandlinglibs/models/DataHandlingModel.hpp index 1fee072..03956dd 100644 --- a/include/datahandlinglibs/models/DataHandlingModel.hpp +++ b/include/datahandlinglibs/models/DataHandlingModel.hpp @@ -43,7 +43,7 @@ #include "datahandlinglibs/concepts/RequestHandlerConcept.hpp" #include "datahandlinglibs/DataHandlingIssues.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include #include @@ -176,7 +176,7 @@ class DataHandlingModel : public DataHandlingConcept std::atomic m_num_payloads_overwritten{ 0 }; // CONSUMER - ReusableThread m_consumer_thread; + utilities::ReusableThread m_consumer_thread; // RAW RECEIVER std::chrono::milliseconds m_raw_receiver_timeout_ms; @@ -197,7 +197,7 @@ class DataHandlingModel : public DataHandlingConcept // TIME-SYNC using timesync_sender_ct = iomanager::SenderConcept; // no timeout -> published std::shared_ptr m_timesync_sender; - ReusableThread m_timesync_thread; + utilities::ReusableThread m_timesync_thread; std::string m_timesync_connection_name; uint32_t m_pid_of_current_process; diff --git a/include/datahandlinglibs/models/DataSubscriberModel.hpp b/include/datahandlinglibs/models/DataSubscriberModel.hpp index ccfe14f..09177e9 100644 --- a/include/datahandlinglibs/models/DataSubscriberModel.hpp +++ b/include/datahandlinglibs/models/DataSubscriberModel.hpp @@ -15,7 +15,7 @@ #include "iomanager/Sender.hpp" #include "iomanager/Receiver.hpp" #include "logging/Logging.hpp" -//#include "datahandlinglibs/utils/ReusableThread.hpp" +//#include "utilities/ReusableThread.hpp" #include "confmodel/Connection.hpp" diff --git a/include/datahandlinglibs/models/DefaultRequestHandlerModel.hpp b/include/datahandlinglibs/models/DefaultRequestHandlerModel.hpp index c7ff653..635f1e2 100644 --- a/include/datahandlinglibs/models/DefaultRequestHandlerModel.hpp +++ b/include/datahandlinglibs/models/DefaultRequestHandlerModel.hpp @@ -12,7 +12,7 @@ #include "datahandlinglibs/DataHandlingIssues.hpp" #include "datahandlinglibs/concepts/RequestHandlerConcept.hpp" #include "datahandlinglibs/utils/BufferedFileWriter.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include "datahandlinglibs/opmon/datahandling_info.pb.h" @@ -227,10 +227,10 @@ class DefaultRequestHandlerModel : public RequestHandlerConcept m_buffered_writer; - ReusableThread m_recording_thread; + utilities::ReusableThread m_recording_thread; - ReusableThread m_cleanup_thread; - ReusableThread m_periodic_transmission_thread; + utilities::ReusableThread m_cleanup_thread; + utilities::ReusableThread m_periodic_transmission_thread; // Bookkeeping of OOB requests std::map m_request_counter; diff --git a/include/datahandlinglibs/models/RecorderModel.hpp b/include/datahandlinglibs/models/RecorderModel.hpp index 9ed5a18..a37d6c4 100644 --- a/include/datahandlinglibs/models/RecorderModel.hpp +++ b/include/datahandlinglibs/models/RecorderModel.hpp @@ -14,7 +14,7 @@ #include "datahandlinglibs/ReadoutTypes.hpp" #include "datahandlinglibs/concepts/RecorderConcept.hpp" #include "datahandlinglibs/utils/BufferedFileWriter.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include "datahandlinglibs/opmon/datahandling_info.pb.h" @@ -68,7 +68,7 @@ class RecorderModel : public RecorderConcept BufferedFileWriter<> m_buffered_writer; // Threading - ReusableThread m_work_thread; + utilities::ReusableThread m_work_thread; std::atomic m_run_marker; // Stats diff --git a/include/datahandlinglibs/models/SourceEmulatorModel.hpp b/include/datahandlinglibs/models/SourceEmulatorModel.hpp index 202394b..9002b1c 100644 --- a/include/datahandlinglibs/models/SourceEmulatorModel.hpp +++ b/include/datahandlinglibs/models/SourceEmulatorModel.hpp @@ -21,7 +21,7 @@ #include "datahandlinglibs/utils/ErrorBitGenerator.hpp" #include "datahandlinglibs/utils/FileSourceBuffer.hpp" #include "datahandlinglibs/utils/RateLimiter.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include "datahandlinglibs/opmon/datahandling_info.pb.h" @@ -140,7 +140,7 @@ class SourceEmulatorModel : public SourceEmulatorConcept std::unique_ptr m_file_source; ErrorBitGenerator m_error_bit_generator; - ReusableThread m_producer_thread; + utilities::ReusableThread m_producer_thread; std::string m_name; bool m_is_configured = false; diff --git a/include/datahandlinglibs/models/TaskRawDataProcessorModel.hpp b/include/datahandlinglibs/models/TaskRawDataProcessorModel.hpp index 1397ca9..d7c3679 100644 --- a/include/datahandlinglibs/models/TaskRawDataProcessorModel.hpp +++ b/include/datahandlinglibs/models/TaskRawDataProcessorModel.hpp @@ -24,7 +24,7 @@ #include "appmodel/DataHandlerConf.hpp" #include "appmodel/DataProcessor.hpp" -#include "datahandlinglibs/utils/ReusableThread.hpp" +#include "utilities/ReusableThread.hpp" #include @@ -113,7 +113,7 @@ class TaskRawDataProcessorModel : public RawDataProcessorConcept // Post-processing functions and their corresponding threads std::vector> m_post_process_functions; std::vector>> m_items_to_postprocess_queues; - std::vector> m_post_process_threads; + std::vector> m_post_process_threads; // Internals size_t m_postprocess_queue_sizes; diff --git a/include/datahandlinglibs/models/detail/TaskRawDataProcessorModel.hxx b/include/datahandlinglibs/models/detail/TaskRawDataProcessorModel.hxx index 1713bc6..6daf292 100644 --- a/include/datahandlinglibs/models/detail/TaskRawDataProcessorModel.hxx +++ b/include/datahandlinglibs/models/detail/TaskRawDataProcessorModel.hxx @@ -80,7 +80,7 @@ template void TaskRawDataProcessorModel::add_postprocess_task(Task&& task) { - m_post_process_threads.emplace_back(std::make_unique(0)); + m_post_process_threads.emplace_back(std::make_unique(0)); m_post_process_functions.push_back(std::forward(task)); } diff --git a/include/datahandlinglibs/utils/ReusableThread.hpp b/include/datahandlinglibs/utils/ReusableThread.hpp deleted file mode 100644 index 2eb2723..0000000 --- a/include/datahandlinglibs/utils/ReusableThread.hpp +++ /dev/null @@ -1,123 +0,0 @@ -/** - * @file ReusableThread.hpp Reusable thread wrapper - * The same thread instance can be used with different tasks to be executed - * Inspired by: - * https://codereview.stackexchange.com/questions/134214/reuseable-c11-thread - * - * This is part of the DUNE DAQ , copyright 2020. - * Licensing/copyright details are in the COPYING file that you should have - * received with this code. - */ -#ifndef DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_REUSABLETHREAD_HPP_ -#define DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_REUSABLETHREAD_HPP_ - -#include -#include -#include -#include -#include -#include -#include - -namespace dunedaq { -namespace datahandlinglibs { - -class ReusableThread -{ -public: - explicit ReusableThread(int threadid=0) - : m_thread_id(threadid) - , m_task_executed(true) - , m_task_assigned(false) - , m_thread_quit(false) - , m_worker_done(false) - , m_thread(&ReusableThread::thread_worker, this) - {} - - ~ReusableThread() - { - while (m_task_assigned) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - m_thread_quit = true; - while (!m_worker_done) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - m_cv.notify_all(); - } - m_thread.join(); - } - - ReusableThread(const ReusableThread&) = delete; ///< ReusableThread is not copy-constructible - ReusableThread& operator=(const ReusableThread&) = delete; ///< ReusableThread is not copy-assginable - ReusableThread(ReusableThread&&) = delete; ///< ReusableThread is not move-constructible - ReusableThread& operator=(ReusableThread&&) = delete; ///< ReusableThread is not move-assignable - - // Set thread ID - void set_thread_id(int tid) { m_thread_id = tid; } - - // Get thread ID - int get_thread_id() const { return m_thread_id; } - - // Set name for pthread handle - void set_name(const std::string& name, int tid) - { - set_thread_id(tid); - char tname[16]; - snprintf(tname, 16, "%s-%d", name.c_str(), tid); // NOLINT - auto handle = m_thread.native_handle(); - pthread_setname_np(handle, tname); - } - - // Check for completed task execution - bool get_readiness() const { return m_task_executed; } - - // Set task to be executed - template - bool set_work(Function&& f, Args&&... args) - { - if (!m_task_assigned && m_task_executed.exchange(false)) { - m_task = std::bind(f, args...); - m_task_assigned = true; - m_cv.notify_all(); - return true; - } - return false; - } - -private: - // Internals - int m_thread_id; - std::atomic m_task_executed; - std::atomic m_task_assigned; - std::atomic m_thread_quit; - std::atomic m_worker_done; - std::function m_task; - - // Locks - std::mutex m_mtx; - std::condition_variable m_cv; - std::thread m_thread; - - // Actual worker thread - void thread_worker() - { - std::unique_lock lock(m_mtx); - - while (!m_thread_quit) { - if (!m_task_executed && m_task_assigned) { - m_task(); - m_task_executed = true; - m_task_assigned = false; - } else { - m_cv.wait(lock); - } - } - - m_worker_done = true; - } -}; - -} // namespace datahandlinglibs -} // namespace dunedaq - -#endif // DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_REUSABLETHREAD_HPP_