XRootD
XrdClJobManager.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_JOB_MANAGER_HH__
20 #define __XRD_CL_JOB_MANAGER_HH__
21 
22 #include <cstdint>
23 #include <vector>
24 #include <algorithm>
25 #include <pthread.h>
26 #include "XrdCl/XrdClSyncQueue.hh"
27 
28 namespace XrdCl
29 {
30  //----------------------------------------------------------------------------
32  //----------------------------------------------------------------------------
33  class Job
34  {
35  public:
36  //------------------------------------------------------------------------
38  //------------------------------------------------------------------------
39  virtual ~Job() {};
40 
41  //------------------------------------------------------------------------
43  //------------------------------------------------------------------------
44  virtual void Run( void *arg ) = 0;
45  };
46 
47  //----------------------------------------------------------------------------
49  //----------------------------------------------------------------------------
50  class JobManager
51  {
52  public:
53  //------------------------------------------------------------------------
55  //------------------------------------------------------------------------
56  JobManager( uint32_t workers )
57  {
58  pRunning = false;
59  pWorkers.resize( workers );
60  }
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
66  {
67  }
68 
69  //------------------------------------------------------------------------
71  //------------------------------------------------------------------------
72  bool Initialize();
73 
74  //------------------------------------------------------------------------
76  //------------------------------------------------------------------------
77  bool Finalize();
78 
79  //------------------------------------------------------------------------
81  //------------------------------------------------------------------------
82  bool Start();
83 
84  //------------------------------------------------------------------------
86  //------------------------------------------------------------------------
87  bool Stop();
88 
89  //------------------------------------------------------------------------
91  //------------------------------------------------------------------------
92  void QueueJob( Job *job, void *arg = 0 )
93  {
94  pJobs.Put( JobHelper( job, arg ) );
95  }
96 
97  //------------------------------------------------------------------------
99  //------------------------------------------------------------------------
100  void RunJobs();
101 
102  bool IsWorker()
103  {
104  pthread_t thread = pthread_self();
105  std::vector<pthread_t>::iterator itr =
106  std::find( pWorkers.begin(), pWorkers.end(), thread );
107  return itr != pWorkers.end();
108  }
109 
110  private:
111  //------------------------------------------------------------------------
113  //------------------------------------------------------------------------
114  void StopWorkers( uint32_t n );
115 
116  struct JobHelper
117  {
118  JobHelper( Job *j = 0, void *a = 0 ): job(j), arg(a) {}
119  Job *job;
120  void *arg;
121  };
122 
123  std::vector<pthread_t> pWorkers;
124  SyncQueue<JobHelper> pJobs;
125  XrdSysMutex pMutex;
126  bool pRunning;
127  };
128 }
129 
130 #endif // __XRD_CL_ANY_OBJECT_HH__
A synchronized queue.
bool Finalize()
Finalize the job manager, clear the queues.
bool Start()
Start the workers.
bool Initialize()
Initialize the job manager.
void RunJobs()
Run the jobs.
~JobManager()
Destructor.
bool Stop()
Stop the workers.
void QueueJob(Job *job, void *arg=0)
Add a job to be run.
JobManager(uint32_t workers)
Constructor.
Interface for a job to be run by the job manager.
virtual void Run(void *arg)=0
The job logic.
virtual ~Job()
Virtual destructor.
void Put(const Item &item)
Put the item in the queue.