xrootd
XrdClJobManager.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef __XRD_CL_JOB_MANAGER_HH__
00020 #define __XRD_CL_JOB_MANAGER_HH__
00021 
00022 #include <stdint.h>
00023 #include <vector>
00024 #include <pthread.h>
00025 #include "XrdCl/XrdClSyncQueue.hh"
00026 
00027 namespace XrdCl
00028 {
00029   //----------------------------------------------------------------------------
00031   //----------------------------------------------------------------------------
00032   class Job
00033   {
00034     public:
00035       //------------------------------------------------------------------------
00037       //------------------------------------------------------------------------
00038       virtual ~Job() {};
00039 
00040       //------------------------------------------------------------------------
00042       //------------------------------------------------------------------------
00043       virtual void Run( void *arg ) = 0;
00044   };
00045 
00046   //----------------------------------------------------------------------------
00048   //----------------------------------------------------------------------------
00049   class JobManager
00050   {
00051     public:
00052       //------------------------------------------------------------------------
00054       //------------------------------------------------------------------------
00055       JobManager( uint32_t workers )
00056       {
00057         pRunning = false;
00058         pWorkers.resize( workers );
00059       }
00060 
00061       //------------------------------------------------------------------------
00063       //------------------------------------------------------------------------
00064       ~JobManager()
00065       {
00066       }
00067 
00068       //------------------------------------------------------------------------
00070       //------------------------------------------------------------------------
00071       bool Initialize();
00072 
00073       //------------------------------------------------------------------------
00075       //------------------------------------------------------------------------
00076       bool Finalize();
00077 
00078       //------------------------------------------------------------------------
00080       //------------------------------------------------------------------------
00081       bool Start();
00082 
00083       //------------------------------------------------------------------------
00085       //------------------------------------------------------------------------
00086       bool Stop();
00087 
00088       //------------------------------------------------------------------------
00090       //------------------------------------------------------------------------
00091       void QueueJob( Job *job, void *arg = 0 )
00092       {
00093         pJobs.Put( JobHelper( job, arg ) );
00094       }
00095 
00096       //------------------------------------------------------------------------
00098       //------------------------------------------------------------------------
00099       void RunJobs();
00100 
00101     private:
00102       //------------------------------------------------------------------------
00104       //------------------------------------------------------------------------
00105       void StopWorkers( uint32_t n );
00106 
00107       struct JobHelper
00108       {
00109         JobHelper( Job *j = 0, void *a = 0 ): job(j), arg(a) {}
00110         Job  *job;
00111         void *arg;
00112       };
00113 
00114       std::vector<pthread_t> pWorkers;
00115       SyncQueue<JobHelper>   pJobs;
00116       XrdSysMutex            pMutex;
00117       bool                   pRunning;
00118   };
00119 }
00120 
00121 #endif // __XRD_CL_ANY_OBJECT_HH__