WorkerMonitorThread

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#ifndef FWCore_SharedMemory_WorkerMonitorThread_h
#define FWCore_SharedMemory_WorkerMonitorThread_h
// -*- C++ -*-
//
// Package:     FWCore/SharedMemory
// Class  :     WorkerMonitorThread
//
/**\class WorkerMonitorThread WorkerMonitorThread.h " FWCore/SharedMemory/interface/WorkerMonitorThread.h"

 Description: Manages a thread that monitors the worker process for unix signals

 Usage:
      Allows a user settable action to happen in the case of a unix signal occuring.
*/
//
// Original Author:  Chris Jones
//         Created:  21/01/2020
//

// system include files
#include <atomic>
#include <csignal>
#include <functional>
#include <thread>

// user include files
#include "FWCore/Utilities/interface/thread_safety_macros.h"

// forward declarations

namespace edm::shared_memory {
  class WorkerMonitorThread {
  public:
    WorkerMonitorThread() {}
    ~WorkerMonitorThread() {
      if (not stopRequested_.load()) {
        stop();
      }
    }
    WorkerMonitorThread(const WorkerMonitorThread&) = delete;
    const WorkerMonitorThread& operator=(const WorkerMonitorThread&) = delete;
    WorkerMonitorThread(WorkerMonitorThread&&) = delete;
    const WorkerMonitorThread& operator=(WorkerMonitorThread&&) = delete;

    // ---------- const member functions ---------------------

    // ---------- member functions ---------------------------
    void setAction(std::function<void()> iFunc) {
      action_ = std::move(iFunc);
      actionSet_.store(true);
    }

    void startThread();

    ///Sets the unix signal handler which communicates with the thread.
    void setupSignalHandling();

    void stop();

  private:
    static void sig_handler(int sig, siginfo_t*, void*);
    void run();
    // ---------- member data --------------------------------
    std::atomic<bool> stopRequested_ = false;
    std::atomic<bool> helperReady_ = false;
    std::atomic<bool> actionSet_ = false;
    CMS_THREAD_GUARD(actionSet_) std::function<void()> action_;

    static std::atomic<int> s_pipeReadEnd;
    static std::atomic<int> s_pipeWriteEnd;
    static std::atomic<bool> s_helperThreadDone;

    std::thread helperThread_;
  };
}  // namespace edm::shared_memory

#endif