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