Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:14

0001 #ifndef FWCore_SharedMemory_WorkerMonitorThread_h
0002 #define FWCore_SharedMemory_WorkerMonitorThread_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/SharedMemory
0006 // Class  :     WorkerMonitorThread
0007 //
0008 /**\class WorkerMonitorThread WorkerMonitorThread.h " FWCore/SharedMemory/interface/WorkerMonitorThread.h"
0009 
0010  Description: Manages a thread that monitors the worker process for unix signals
0011 
0012  Usage:
0013       Allows a user settable action to happen in the case of a unix signal occuring.
0014 */
0015 //
0016 // Original Author:  Chris Jones
0017 //         Created:  21/01/2020
0018 //
0019 
0020 // system include files
0021 #include <atomic>
0022 #include <csignal>
0023 #include <functional>
0024 #include <thread>
0025 
0026 // user include files
0027 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0028 
0029 // forward declarations
0030 
0031 namespace edm::shared_memory {
0032   class WorkerMonitorThread {
0033   public:
0034     WorkerMonitorThread() {}
0035     ~WorkerMonitorThread() {
0036       if (not stopRequested_.load()) {
0037         stop();
0038       }
0039     }
0040     WorkerMonitorThread(const WorkerMonitorThread&) = delete;
0041     const WorkerMonitorThread& operator=(const WorkerMonitorThread&) = delete;
0042     WorkerMonitorThread(WorkerMonitorThread&&) = delete;
0043     const WorkerMonitorThread& operator=(WorkerMonitorThread&&) = delete;
0044 
0045     // ---------- const member functions ---------------------
0046 
0047     // ---------- member functions ---------------------------
0048     void setAction(std::function<void()> iFunc) {
0049       action_ = std::move(iFunc);
0050       actionSet_.store(true);
0051     }
0052 
0053     void startThread();
0054 
0055     ///Sets the unix signal handler which communicates with the thread.
0056     void setupSignalHandling();
0057 
0058     void stop();
0059 
0060   private:
0061     static void sig_handler(int sig, siginfo_t*, void*);
0062     void run();
0063     // ---------- member data --------------------------------
0064     std::atomic<bool> stopRequested_ = false;
0065     std::atomic<bool> helperReady_ = false;
0066     std::atomic<bool> actionSet_ = false;
0067     CMS_THREAD_GUARD(actionSet_) std::function<void()> action_;
0068 
0069     static std::atomic<int> s_pipeReadEnd;
0070     static std::atomic<int> s_pipeWriteEnd;
0071     static std::atomic<bool> s_helperThreadDone;
0072 
0073     std::thread helperThread_;
0074   };
0075 }  // namespace edm::shared_memory
0076 
0077 #endif