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 <cstdint>
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) {}
132  Task *task;
133  time_t execTime;
134  bool own;
135  };
136 
137  struct TaskHelperCmp
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;
152  TaskSet pTasks;
153  TaskList pToBeUnregistered;
154  pthread_t pRunnerThread;
155  bool pRunning;
156  XrdSysMutex pMutex;
157  XrdSysMutex pOpMutex;
158  };
159 }
160 
161 #endif // __XRD_CL_TASK_MANAGER_HH__
void RegisterTask(Task *task, time_t time, bool own=true)
void RunTasks()
Run the tasks - this loops infinitely.
TaskManager()
Constructor.
bool Start()
Start the manager.
~TaskManager()
Destructor.
void UnregisterTask(Task *task)
Interface for a task to be run by the TaskManager.
virtual time_t Run(time_t now)=0
const std::string & GetName() const
Name of the task.
void SetName(const std::string &name)
Set name of the task.
virtual ~Task()