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 <stdint.h>
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) {}
120  void *arg;
121  };
122 
123  std::vector<pthread_t> pWorkers;
126  bool pRunning;
127  };
128 }
129 
130 #endif // __XRD_CL_ANY_OBJECT_HH__
bool Finalize()
Finalize the job manager, clear the queues.
A synchronized queue.
Definition: XrdClJobManager.hh:50
bool Stop()
Stop the workers.
JobManager(uint32_t workers)
Constructor.
Definition: XrdClJobManager.hh:56
void StopWorkers(uint32_t n)
Stop all workers up to n&#39;th.
JobHelper(Job *j=0, void *a=0)
Definition: XrdClJobManager.hh:118
bool Initialize()
Initialize the job manager.
std::vector< pthread_t > pWorkers
Definition: XrdClJobManager.hh:123
A synchronized queue.
Definition: XrdClSyncQueue.hh:32
void QueueJob(Job *job, void *arg=0)
Add a job to be run.
Definition: XrdClJobManager.hh:92
SyncQueue< JobHelper > pJobs
Definition: XrdClJobManager.hh:124
Definition: XrdSysPthread.hh:165
XrdSysMutex pMutex
Definition: XrdClJobManager.hh:125
Definition: XrdClAnyObject.hh:25
virtual void Run(void *arg)=0
The job logic.
bool Start()
Start the workers.
bool IsWorker()
Definition: XrdClJobManager.hh:102
~JobManager()
Destructor.
Definition: XrdClJobManager.hh:65
void * arg
Definition: XrdClJobManager.hh:120
Interface for a job to be run by the job manager.
Definition: XrdClJobManager.hh:33
Definition: XrdClJobManager.hh:116
bool pRunning
Definition: XrdClJobManager.hh:126
virtual ~Job()
Virtual destructor.
Definition: XrdClJobManager.hh:39
Job * job
Definition: XrdClJobManager.hh:119
void RunJobs()
Run the jobs.