xrootd
XrdClTaskManager.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 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_TASK_MANAGER_HH__
20 #define __XRD_CL_TASK_MANAGER_HH__
21 
22 #include <ctime>
23 #include <set>
24 #include <list>
25 #include <string>
26 #include <stdint.h>
27 #include <pthread.h>
28 #include "XrdSys/XrdSysPthread.hh"
29 
30 namespace XrdCl
31 {
32  //----------------------------------------------------------------------------
34  //----------------------------------------------------------------------------
35  class Task
36  {
37  public:
38  virtual ~Task() {};
39 
40  //------------------------------------------------------------------------
46  //------------------------------------------------------------------------
47  virtual time_t Run( time_t now ) = 0;
48 
49  //------------------------------------------------------------------------
51  //------------------------------------------------------------------------
52  const std::string &GetName() const
53  {
54  return pName;
55  }
56 
57  //------------------------------------------------------------------------
59  //------------------------------------------------------------------------
60  void SetName( const std::string &name )
61  {
62  pName = name;
63  }
64 
65  private:
66  std::string pName;
67  };
68 
69  //----------------------------------------------------------------------------
74  //----------------------------------------------------------------------------
76  {
77  public:
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  TaskManager();
82 
83  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  ~TaskManager();
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
91  bool Start();
92 
93  //------------------------------------------------------------------------
97  //------------------------------------------------------------------------
98  bool Stop();
99 
100  //------------------------------------------------------------------------
107  //------------------------------------------------------------------------
108  void RegisterTask( Task *task, time_t time, bool own = true );
109 
110  //------------------------------------------------------------------------
115  //------------------------------------------------------------------------
116  void UnregisterTask( Task *task );
117 
118  //------------------------------------------------------------------------
120  //------------------------------------------------------------------------
121  void RunTasks();
122 
123  private:
124 
125  //------------------------------------------------------------------------
126  // Task set helpers
127  //------------------------------------------------------------------------
128  struct TaskHelper
129  {
130  TaskHelper( Task *tsk, time_t tme, bool ow = true ):
131  task(tsk), execTime(tme), own(ow) {}
133  time_t execTime;
134  bool own;
135  };
136 
138  {
139  bool operator () ( const TaskHelper &th1, const TaskHelper &th2 ) const
140  {
141  return th1.execTime < th2.execTime;
142  }
143  };
144 
145  typedef std::multiset<TaskHelper, TaskHelperCmp> TaskSet;
146  typedef std::list<Task*> TaskList;
147 
148  //------------------------------------------------------------------------
149  // Private variables
150  //------------------------------------------------------------------------
151  uint16_t pResolution;
154  pthread_t pRunnerThread;
155  bool pRunning;
158  };
159 }
160 
161 #endif // __XRD_CL_TASK_MANAGER_HH__
bool pRunning
Definition: XrdClTaskManager.hh:155
void RegisterTask(Task *task, time_t time, bool own=true)
TaskHelper(Task *tsk, time_t tme, bool ow=true)
Definition: XrdClTaskManager.hh:130
void RunTasks()
Run the tasks - this loops infinitely.
~TaskManager()
Destructor.
std::list< Task * > TaskList
Definition: XrdClTaskManager.hh:146
TaskSet pTasks
Definition: XrdClTaskManager.hh:152
void SetName(const std::string &name)
Set name of the task.
Definition: XrdClTaskManager.hh:60
Definition: XrdSysPthread.hh:165
Task * task
Definition: XrdClTaskManager.hh:132
virtual time_t Run(time_t now)=0
Interface for a task to be run by the TaskManager.
Definition: XrdClTaskManager.hh:35
Definition: XrdClTaskManager.hh:128
Definition: XrdClTaskManager.hh:137
Definition: XrdClAnyObject.hh:25
XrdSysMutex pOpMutex
Definition: XrdClTaskManager.hh:157
TaskList pToBeUnregistered
Definition: XrdClTaskManager.hh:153
uint16_t pResolution
Definition: XrdClTaskManager.hh:151
TaskManager()
Constructor.
std::multiset< TaskHelper, TaskHelperCmp > TaskSet
Definition: XrdClTaskManager.hh:145
void UnregisterTask(Task *task)
bool own
Definition: XrdClTaskManager.hh:134
bool operator()(const TaskHelper &th1, const TaskHelper &th2) const
Definition: XrdClTaskManager.hh:139
bool Start()
Start the manager.
virtual ~Task()
Definition: XrdClTaskManager.hh:38
const std::string & GetName() const
Name of the task.
Definition: XrdClTaskManager.hh:52
pthread_t pRunnerThread
Definition: XrdClTaskManager.hh:154
XrdSysMutex pMutex
Definition: XrdClTaskManager.hh:156
Definition: XrdClTaskManager.hh:75
time_t execTime
Definition: XrdClTaskManager.hh:133
std::string pName
Definition: XrdClTaskManager.hh:66