00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_TASK_MANAGER_HH__ 00020 #define __XRD_CL_TASK_MANAGER_HH__ 00021 00022 #include <ctime> 00023 #include <set> 00024 #include <list> 00025 #include <string> 00026 #include <stdint.h> 00027 #include <pthread.h> 00028 #include "XrdSys/XrdSysPthread.hh" 00029 00030 namespace XrdCl 00031 { 00032 //---------------------------------------------------------------------------- 00034 //---------------------------------------------------------------------------- 00035 class Task 00036 { 00037 public: 00038 virtual ~Task() {}; 00039 00040 //------------------------------------------------------------------------ 00046 //------------------------------------------------------------------------ 00047 virtual time_t Run( time_t now ) = 0; 00048 00049 //------------------------------------------------------------------------ 00051 //------------------------------------------------------------------------ 00052 const std::string &GetName() const 00053 { 00054 return pName; 00055 } 00056 00057 //------------------------------------------------------------------------ 00059 //------------------------------------------------------------------------ 00060 void SetName( const std::string &name ) 00061 { 00062 pName = name; 00063 } 00064 00065 private: 00066 std::string pName; 00067 }; 00068 00069 //---------------------------------------------------------------------------- 00074 //---------------------------------------------------------------------------- 00075 class TaskManager 00076 { 00077 public: 00078 //------------------------------------------------------------------------ 00080 //------------------------------------------------------------------------ 00081 TaskManager(); 00082 00083 //------------------------------------------------------------------------ 00085 //------------------------------------------------------------------------ 00086 ~TaskManager(); 00087 00088 //------------------------------------------------------------------------ 00090 //------------------------------------------------------------------------ 00091 bool Start(); 00092 00093 //------------------------------------------------------------------------ 00097 //------------------------------------------------------------------------ 00098 bool Stop(); 00099 00100 //------------------------------------------------------------------------ 00107 //------------------------------------------------------------------------ 00108 void RegisterTask( Task *task, time_t time, bool own = true ); 00109 00110 //------------------------------------------------------------------------ 00115 //------------------------------------------------------------------------ 00116 void UnregisterTask( Task *task ); 00117 00118 //------------------------------------------------------------------------ 00120 //------------------------------------------------------------------------ 00121 void RunTasks(); 00122 00123 private: 00124 00125 //------------------------------------------------------------------------ 00126 // Task set helpers 00127 //------------------------------------------------------------------------ 00128 struct TaskHelper 00129 { 00130 TaskHelper( Task *tsk, time_t tme, bool ow = true ): 00131 task(tsk), execTime(tme), own(ow) {} 00132 Task *task; 00133 time_t execTime; 00134 bool own; 00135 }; 00136 00137 struct TaskHelperCmp 00138 { 00139 bool operator () ( const TaskHelper &th1, const TaskHelper &th2 ) const 00140 { 00141 return th1.execTime < th2.execTime; 00142 } 00143 }; 00144 00145 typedef std::multiset<TaskHelper, TaskHelperCmp> TaskSet; 00146 typedef std::list<Task*> TaskList; 00147 00148 //------------------------------------------------------------------------ 00149 // Private variables 00150 //------------------------------------------------------------------------ 00151 uint16_t pResolution; 00152 TaskSet pTasks; 00153 TaskList pToBeUnregistered; 00154 pthread_t pRunnerThread; 00155 bool pRunning; 00156 XrdSysMutex pMutex; 00157 XrdSysMutex pOpMutex; 00158 }; 00159 } 00160 00161 #endif // __XRD_CL_TASK_MANAGER_HH__