Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:01:50

0001 #ifndef FWCore_Concurrency_TaskBase_h
0002 #define FWCore_Concurrency_TaskBase_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Concurrency
0006 // Class  :     TaskBase
0007 //
0008 /**\class TaskBase TaskBase.h FWCore/Concurrency/interface/TaskBase.h
0009 
0010  Description: Base class for tasks.
0011 
0012  Usage:
0013     Used as a callback to happen after a task has been completed.
0014 */
0015 //
0016 // Original Author:  Chris Jones
0017 //         Created:  Tue Jan 5 13:46:31 CST 2020
0018 // $Id$
0019 //
0020 
0021 // system include files
0022 #include <atomic>
0023 #include <exception>
0024 #include <memory>
0025 
0026 // user include files
0027 
0028 // forward declarations
0029 
0030 namespace edm {
0031   class TaskBase {
0032   public:
0033     friend class TaskSentry;
0034 
0035     ///Constructor
0036     TaskBase() : m_refCount{0} {}
0037     virtual ~TaskBase() = default;
0038 
0039     virtual void execute() = 0;
0040 
0041     void increment_ref_count() { ++m_refCount; }
0042     unsigned int decrement_ref_count() { return --m_refCount; }
0043 
0044   private:
0045     virtual void recycle() { delete this; }
0046 
0047     std::atomic<unsigned int> m_refCount{0};
0048   };
0049 
0050   class TaskSentry {
0051   public:
0052     TaskSentry(TaskBase* iTask) : m_task{iTask} {}
0053     ~TaskSentry() { m_task->recycle(); }
0054     TaskSentry() = delete;
0055     TaskSentry(TaskSentry const&) = delete;
0056     TaskSentry(TaskSentry&&) = delete;
0057     TaskSentry operator=(TaskSentry const&) = delete;
0058     TaskSentry operator=(TaskSentry&&) = delete;
0059 
0060   private:
0061     TaskBase* m_task;
0062   };
0063 }  // namespace edm
0064 
0065 #endif