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 <algorithm> 00025 #include <pthread.h> 00026 #include "XrdCl/XrdClSyncQueue.hh" 00027 00028 namespace XrdCl 00029 { 00030 //---------------------------------------------------------------------------- 00032 //---------------------------------------------------------------------------- 00033 class Job 00034 { 00035 public: 00036 //------------------------------------------------------------------------ 00038 //------------------------------------------------------------------------ 00039 virtual ~Job() {}; 00040 00041 //------------------------------------------------------------------------ 00043 //------------------------------------------------------------------------ 00044 virtual void Run( void *arg ) = 0; 00045 }; 00046 00047 //---------------------------------------------------------------------------- 00049 //---------------------------------------------------------------------------- 00050 class JobManager 00051 { 00052 public: 00053 //------------------------------------------------------------------------ 00055 //------------------------------------------------------------------------ 00056 JobManager( uint32_t workers ) 00057 { 00058 pRunning = false; 00059 pWorkers.resize( workers ); 00060 } 00061 00062 //------------------------------------------------------------------------ 00064 //------------------------------------------------------------------------ 00065 ~JobManager() 00066 { 00067 } 00068 00069 //------------------------------------------------------------------------ 00071 //------------------------------------------------------------------------ 00072 bool Initialize(); 00073 00074 //------------------------------------------------------------------------ 00076 //------------------------------------------------------------------------ 00077 bool Finalize(); 00078 00079 //------------------------------------------------------------------------ 00081 //------------------------------------------------------------------------ 00082 bool Start(); 00083 00084 //------------------------------------------------------------------------ 00086 //------------------------------------------------------------------------ 00087 bool Stop(); 00088 00089 //------------------------------------------------------------------------ 00091 //------------------------------------------------------------------------ 00092 void QueueJob( Job *job, void *arg = 0 ) 00093 { 00094 pJobs.Put( JobHelper( job, arg ) ); 00095 } 00096 00097 //------------------------------------------------------------------------ 00099 //------------------------------------------------------------------------ 00100 void RunJobs(); 00101 00102 bool IsWorker() 00103 { 00104 pthread_t thread = pthread_self(); 00105 std::vector<pthread_t>::iterator itr = 00106 std::find( pWorkers.begin(), pWorkers.end(), thread ); 00107 return itr != pWorkers.end(); 00108 } 00109 00110 private: 00111 //------------------------------------------------------------------------ 00113 //------------------------------------------------------------------------ 00114 void StopWorkers( uint32_t n ); 00115 00116 struct JobHelper 00117 { 00118 JobHelper( Job *j = 0, void *a = 0 ): job(j), arg(a) {} 00119 Job *job; 00120 void *arg; 00121 }; 00122 00123 std::vector<pthread_t> pWorkers; 00124 SyncQueue<JobHelper> pJobs; 00125 XrdSysMutex pMutex; 00126 bool pRunning; 00127 }; 00128 } 00129 00130 #endif // __XRD_CL_ANY_OBJECT_HH__