Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:36

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
0003 // Author: Lukasz Janyst <ljanyst@cern.ch>
0004 //------------------------------------------------------------------------------
0005 // XRootD is free software: you can redistribute it and/or modify
0006 // it under the terms of the GNU Lesser General Public License as published by
0007 // the Free Software Foundation, either version 3 of the License, or
0008 // (at your option) any later version.
0009 //
0010 // XRootD is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU Lesser General Public License
0016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0017 //------------------------------------------------------------------------------
0018 
0019 #ifndef __XRD_CL_JOB_MANAGER_HH__
0020 #define __XRD_CL_JOB_MANAGER_HH__
0021 
0022 #include <cstdint>
0023 #include <vector>
0024 #include <algorithm>
0025 #include <pthread.h>
0026 #include "XrdCl/XrdClSyncQueue.hh"
0027 
0028 namespace XrdCl
0029 {
0030   //----------------------------------------------------------------------------
0031   //! Interface for a job to be run by the job manager
0032   //----------------------------------------------------------------------------
0033   class Job
0034   {
0035     public:
0036       //------------------------------------------------------------------------
0037       //! Virtual destructor
0038       //------------------------------------------------------------------------
0039       virtual ~Job() {};
0040 
0041       //------------------------------------------------------------------------
0042       //! The job logic
0043       //------------------------------------------------------------------------
0044       virtual void Run( void *arg ) = 0;
0045   };
0046 
0047   //----------------------------------------------------------------------------
0048   //! A synchronized queue
0049   //----------------------------------------------------------------------------
0050   class JobManager
0051   {
0052     public:
0053       //------------------------------------------------------------------------
0054       //! Constructor
0055       //------------------------------------------------------------------------
0056       JobManager( uint32_t workers )
0057       {
0058         pRunning = false;
0059         pWorkers.resize( workers );
0060       }
0061 
0062       //------------------------------------------------------------------------
0063       //! Destructor
0064       //------------------------------------------------------------------------
0065       ~JobManager()
0066       {
0067       }
0068 
0069       //------------------------------------------------------------------------
0070       //! Initialize the job manager
0071       //------------------------------------------------------------------------
0072       bool Initialize();
0073 
0074       //------------------------------------------------------------------------
0075       //! Finalize the job manager, clear the queues
0076       //------------------------------------------------------------------------
0077       bool Finalize();
0078 
0079       //------------------------------------------------------------------------
0080       //! Start the workers
0081       //------------------------------------------------------------------------
0082       bool Start();
0083 
0084       //------------------------------------------------------------------------
0085       //! Stop the workers
0086       //------------------------------------------------------------------------
0087       bool Stop();
0088 
0089       //------------------------------------------------------------------------
0090       //! Add a job to be run
0091       //------------------------------------------------------------------------
0092       void QueueJob( Job *job, void *arg = 0 )
0093       {
0094         pJobs.Put( JobHelper( job, arg ) );
0095       }
0096 
0097       //------------------------------------------------------------------------
0098       //! Run the jobs
0099       //------------------------------------------------------------------------
0100       void RunJobs();
0101 
0102       bool IsWorker()
0103       {
0104         pthread_t thread = pthread_self();
0105         std::vector<pthread_t>::iterator itr =
0106             std::find( pWorkers.begin(), pWorkers.end(), thread );
0107         return itr != pWorkers.end();
0108       }
0109 
0110     private:
0111       //------------------------------------------------------------------------
0112       //! Stop all workers up to n'th
0113       //------------------------------------------------------------------------
0114       void StopWorkers( uint32_t n );
0115 
0116       struct JobHelper
0117       {
0118         JobHelper( Job *j = 0, void *a = 0 ): job(j), arg(a) {}
0119         Job  *job;
0120         void *arg;
0121       };
0122 
0123       std::vector<pthread_t> pWorkers;
0124       SyncQueue<JobHelper>   pJobs;
0125       XrdSysMutex            pMutex;
0126       bool                   pRunning;
0127   };
0128 }
0129 
0130 #endif // __XRD_CL_ANY_OBJECT_HH__