Changeset 1374:501ccac7f3c0
- Timestamp:
- 10/17/09 00:56:43 (2 years ago)
- Branch:
- default
- Children:
- 1375:7463cec1af99, 1380:3d4feefdfb45
- Files:
-
- 2 added
- 9 edited
- 8 moved
-
.hgignore (modified) (2 diffs)
-
trunk/include/libdodo/exceptionBasic.h (modified) (1 diff)
-
trunk/include/libdodo/pc.h (modified) (1 diff)
-
trunk/include/libdodo/pcExecutionJob.h (added)
-
trunk/include/libdodo/pcExecutionManager.h (moved) (moved from trunk/include/libdodo/pcJobManager.h) (6 diffs)
-
trunk/include/libdodo/pcExecutionManager.inline (moved) (moved from trunk/src/pcJobManager.cc) (2 diffs)
-
trunk/include/libdodo/pcExecutionProcess.h (moved) (moved from trunk/include/libdodo/pcJobProcessManager.h) (5 diffs)
-
trunk/include/libdodo/pcExecutionProcessEx.h (moved) (moved from trunk/include/libdodo/pcJobProcessManagerEx.h) (3 diffs)
-
trunk/include/libdodo/pcExecutionThread.h (moved) (moved from trunk/include/libdodo/pcJobThreadManager.h) (5 diffs)
-
trunk/include/libdodo/pcExecutionThreadEx.h (moved) (moved from trunk/include/libdodo/pcJobThreadManagerEx.h) (3 diffs)
-
trunk/src/exceptionBasic.cc (modified) (5 diffs)
-
trunk/src/pcExecutionJob.cc (added)
-
trunk/src/pcExecutionProcess.cc (moved) (moved from trunk/src/pcJobProcessManager.cc) (6 diffs)
-
trunk/src/pcExecutionThread.cc (moved) (moved from trunk/src/pcJobThreadManager.cc) (10 diffs)
-
trunk/tests/ionetwork_pcthreads_test/test.cc (modified) (3 diffs)
-
trunk/tests/iopipe_iofilefifo_pcthreadcollection_test/test.cc (modified) (8 diffs)
-
trunk/tests/pcjobcollection_test/test.cc (modified) (3 diffs)
-
trunk/tests/pcprocesscollection_test/test.cc (modified) (3 diffs)
-
trunk/tests/pcthreadcollection_test/test.cc (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
.hgignore
r1110 r1374 6 6 .settings 7 7 *.so* 8 *.a 8 9 *.cgi 9 10 *.fcgi … … 14 15 *.png 15 16 *.swp 17 *.d 16 18 test 17 19 module -
trunk/include/libdodo/exceptionBasic.h
r1373 r1374 97 97 MODULE_IONETWORKHTTP, 98 98 MODULE_IOEVENTMANAGER, 99 MODULE_PCJOBPROCESSMANAGER, 100 MODULE_PCJOBTHREADMANAGER, 99 MODULE_PCEXECUTIONMANAGER, 100 MODULE_PCEXECUTIONPROCESS, 101 MODULE_PCEXECUTIONTHREAD, 101 102 MODULE_PCSYNCTHREADDATASINGLE, 102 103 MODULE_PCSYNCPROCESSDATASINGLE, -
trunk/include/libdodo/pc.h
r1373 r1374 31 31 #define _PC_H_ 1 32 32 33 #include <libdodo/pcJobManager.h> 34 #include <libdodo/pcJobProcessManager.h> 35 #include <libdodo/pcJobProcessManagerEx.h> 36 #include <libdodo/pcJobThreadManager.h> 37 #include <libdodo/pcJobThreadManagerEx.h> 33 #include <libdodo/pcExecutionJob.h> 34 #include <libdodo/pcExecutionThread.h> 35 #include <libdodo/pcExecutionThreadEx.h> 36 #include <libdodo/pcExecutionProcess.h> 37 #include <libdodo/pcExecutionProcessEx.h> 38 #include <libdodo/pcExecutionManager.h> 38 39 #include <libdodo/pcSyncDataCollection.h> 39 40 #include <libdodo/pcSyncDataSingle.h> -
trunk/include/libdodo/pcExecutionManager.h
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobManager.h2 * pcExecutionManager.h 3 3 * 4 4 * Mon Mar 05 2007 … … 28 28 */ 29 29 30 #ifndef _PC JOBMANAGER_H_31 #define _PC JOBMANAGER_H_ 130 #ifndef _PCEXECUTIONMANAGER_H_ 31 #define _PCEXECUTIONMANAGER_H_ 1 32 32 33 33 #include <libdodo/directives.h> 34 34 35 35 #include <libdodo/types.h> 36 #include <libdodo/pcSyncSection.h> 36 37 37 38 namespace dodo { 38 39 namespace pc { 39 namespace job { 40 /** 41 * @typedef routine 42 * @brief defines job function 43 * @param data defines user data 44 */ 45 typedef int (*routine)(void *data); 46 47 /** 48 * @enum onDestructionEnum defines action with processes on object destruction 49 */ 50 enum onDestructionEnum { 51 ON_DESTRUCTION_KEEP_ALIVE, 52 ON_DESTRUCTION_STOP, ///< send SIGINT to process 53 ON_DESTRUCTION_WAIT 54 }; 55 40 namespace execution { 56 41 /** 57 42 * @class manager 58 43 * @brief provides interface for jobs management 59 44 */ 45 template <typename T> 60 46 class manager { 61 47 public: 48 49 /** 50 * constructor 51 */ 52 manager(); 53 62 54 /** 63 55 * destructor 64 56 */ 65 virtual ~manager() = 0;57 virtual ~manager(); 66 58 67 59 /** 68 * add function asa job60 * add a job 69 61 * @return job identificator 70 * @param func defines function to execute 71 * @param data defines job data 72 * @param action defines action on object destruction if process is running, @see job::onDestructionEnum 62 * @param job defines job for managing 73 63 */ 74 virtual unsigned long add(routine func, 75 void *data, 76 short action) = 0; 77 78 /** 79 * add function as a job 80 * @return job identificator 81 * @param func defines function to execute 82 * @param data defines job data 83 * @param action defines action on object destruction if process is running, @see job::onDestructionEnum 84 * @note this will immediately execute the job 85 */ 86 virtual unsigned long addNRun(routine func, 87 void *data, 88 short action) = 0; 64 virtual unsigned long add(const T &job); 89 65 90 66 /** 91 67 * remove registered job 92 68 * @param id defines job identificator 93 * @param terminate defines termination condition ; if true and job is running stop execution of the job69 * @param terminate defines termination condition 94 70 */ 95 71 virtual void remove(unsigned long id, 96 bool terminate = false) = 0;72 bool terminate = false); 97 73 98 74 /** … … 101 77 * @param force defines run condition; if true and job is running run job anyway 102 78 */ 103 virtual void run(unsigned long id, 104 bool force = false) = 0; 79 virtual void run(unsigned long id); 105 80 106 81 /** … … 108 83 * @param id defines job identificator 109 84 */ 110 virtual void stop(unsigned long id) = 0;85 virtual void stop(unsigned long id); 111 86 112 87 /** 113 88 * stop all registered jobs 114 89 */ 115 virtual void stop() = 0;90 virtual void stop(); 116 91 117 92 /** … … 120 95 * @param id defines job identificator 121 96 */ 122 virtual int wait(unsigned long id) = 0;97 virtual int wait(unsigned long id); 123 98 124 99 /** 125 100 * wait for all registered jobs termination 126 101 */ 127 virtual void wait() = 0;102 virtual void wait(); 128 103 129 104 /** … … 131 106 * @param id defines job identificator 132 107 */ 133 virtual bool isRunning(unsigned long id) const = 0;108 virtual bool isRunning(unsigned long id) const; 134 109 135 110 /** 136 111 * @return amount of running jobs 137 112 */ 138 virtual unsigned long running() const = 0;113 virtual unsigned long running() const; 139 114 140 115 /** 141 116 * @return list of jobs in object 142 117 */ 143 virtual dodoList<unsigned long> jobs() = 0;118 virtual dodoList<unsigned long> jobs(); 144 119 145 #ifdef DL_EXT 146 /** 147 * add function as a job from library 148 * @return job identificator 149 * @param module defines path to the library[if not in ldconfig db] or library name 150 * @param data defines job data 151 * @param toInit defines library init data 152 */ 153 virtual unsigned long add(const dodoString &module, 154 void *data, 155 void *toInit = NULL) = 0; 156 #endif 120 protected: 121 122 dodoMap<unsigned long, T> handles; ///< managed jobs 123 124 unsigned long counter; ///< job id counter 125 126 sync::section *keeper; ///< section locker 157 127 }; 158 128 }; 159 129 }; 160 130 }; 131 132 #include <libdodo/pcExecutionManager.inline> 133 161 134 #endif -
trunk/include/libdodo/pcExecutionManager.inline
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobManager.cc2 * pcExecutionManager.inline 3 3 * 4 4 * Sun Oct 30 2007 … … 30 30 #include <libdodo/directives.h> 31 31 32 #include <libdodo/pcJobManager.h> 33 34 using namespace dodo::pc::job; 35 36 manager::~manager() 37 { 38 } 39 40 //------------------------------------------------------------------- 32 #include <libdodo/pcExecutionManager.h> 33 #include <libdodo/types.h> 34 #include <libdodo/pcSyncSection.h> 35 #include <libdodo/pcSyncThreadSection.h> 36 #include <libdodo/pcSyncProtector.h> 37 38 template <typename T> 39 dodo::pc::execution::manager<T>::manager() : counter(0), 40 keeper(new pc::sync::thread::section) 41 { 42 } 43 44 //------------------------------------------------------------------- 45 46 template <typename T> 47 dodo::pc::execution::manager<T>::~manager() 48 { 49 delete keeper; 50 } 51 52 //------------------------------------------------------------------- 53 54 template <typename T> 55 unsigned long 56 dodo::pc::execution::manager<T>::add(const T &job) 57 { 58 pc::sync::protector tg(keeper); 59 60 handles.insert(make_pair(counter, job)); 61 62 return counter++; 63 } 64 65 //------------------------------------------------------------------- 66 67 template <typename T> 68 void 69 dodo::pc::execution::manager<T>::remove(unsigned long id, 70 bool terminate) 71 { 72 pc::sync::protector tg(keeper); 73 74 typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 75 76 if (job == handles.end()) 77 return; 78 79 if (terminate && job->second.isRunning()) 80 job->second.stop(); 81 82 handles.erase(job); 83 } 84 85 //------------------------------------------------------------------- 86 87 template <typename T> 88 void 89 dodo::pc::execution::manager<T>::run(unsigned long id) 90 { 91 pc::sync::protector tg(keeper); 92 93 typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 94 95 if (job == handles.end()) 96 throw; 97 98 job->second.run(); 99 } 100 101 //------------------------------------------------------------------- 102 103 template <typename T> 104 void 105 dodo::pc::execution::manager<T>::stop(unsigned long id) 106 { 107 pc::sync::protector tg(keeper); 108 109 typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 110 111 if (job == handles.end()) 112 throw; 113 114 job->second.stop(); 115 } 116 117 //------------------------------------------------------------------- 118 119 template <typename T> 120 void 121 dodo::pc::execution::manager<T>::stop() 122 { 123 pc::sync::protector tg(keeper); 124 125 typename dodoMap<unsigned long, T>::iterator i = handles.begin(), j = handles.end(); 126 127 for (;i!=j;++i) 128 i->second.stop(); 129 } 130 131 //------------------------------------------------------------------- 132 133 template <typename T> 134 int 135 dodo::pc::execution::manager<T>::wait(unsigned long id) 136 { 137 pc::sync::protector tg(keeper); 138 139 typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 140 141 if (job == handles.end()) 142 throw; 143 144 return job->second.wait(); 145 } 146 147 //------------------------------------------------------------------- 148 149 template <typename T> 150 void 151 dodo::pc::execution::manager<T>::wait() 152 { 153 pc::sync::protector tg(keeper); 154 155 typename dodoMap<unsigned long, T>::iterator i = handles.begin(), j = handles.end(); 156 157 for (;i!=j;++i) 158 i->second.wait(); 159 } 160 161 //------------------------------------------------------------------- 162 163 template <typename T> 164 bool 165 dodo::pc::execution::manager<T>::isRunning(unsigned long id) const 166 { 167 pc::sync::protector tg(keeper); 168 169 typename dodoMap<unsigned long, T>::const_iterator job = handles.find(id); 170 171 if (job == handles.end()) 172 throw; 173 174 return job->second.isRunning(); 175 } 176 177 //------------------------------------------------------------------- 178 179 template <typename T> 180 unsigned long 181 dodo::pc::execution::manager<T>::running() const 182 { 183 pc::sync::protector tg(keeper); 184 185 unsigned long jobs; 186 187 typename dodoMap<unsigned long, T>::const_iterator i = handles.begin(), j = handles.end(); 188 189 for (;i!=j;++i) 190 if (i->second.isRunning()) 191 ++jobs; 192 193 return jobs; 194 } 195 196 //------------------------------------------------------------------- 197 198 template <typename T> 199 dodoList<unsigned long> 200 dodo::pc::execution::manager<T>::jobs() 201 { 202 pc::sync::protector tg(keeper); 203 204 dodoList<unsigned long> jobs; 205 206 typename dodoMap<unsigned long, T>::const_iterator i = handles.begin(), j = handles.end(); 207 208 for (;i!=j;++i) 209 jobs.push_back(i->first); 210 211 return jobs; 212 } 213 214 //------------------------------------------------------------------- -
trunk/include/libdodo/pcExecutionProcess.h
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobProcessManager.h2 * pcExecutionProcess.h 3 3 * 4 * Tue Feb 27 20075 * Copyright 200 7Ni@m4 * Wed Oct 08 2009 5 * Copyright 2009 Ni@m 6 6 * niam.niam@gmail.com 7 7 ****************************************************************************/ … … 28 28 */ 29 29 30 #ifndef _PC JOBPROCESSMANAGER_H_31 #define _PC JOBPROCESSMANAGER_H_ 130 #ifndef _PCEXECUTIONPROCESS_H_ 31 #define _PCEXECUTIONPROCESS_H_ 1 32 32 33 33 #include <libdodo/directives.h> 34 34 35 35 #include <libdodo/types.h> 36 #include <libdodo/pc JobManager.h>36 #include <libdodo/pcExecutionJob.h> 37 37 38 38 namespace dodo { 39 39 namespace pc { 40 namespace job { 41 namespace process { 42 struct __process__; 40 namespace execution { 41 struct __process__; 42 43 /** 44 * @class process 45 * @brief provides process management functionality 46 */ 47 class process : public job { 48 public: 49 50 /** 51 * constructor 52 * @param func defines function to execute 53 * @param data defines process data 54 * @param action defines action on object destruction if process is running, @see onDestructionEnum 55 * @note func must not call `exit` family call 56 */ 57 process(routine func, 58 void *data, 59 short action); 60 61 #ifdef DL_EXT 62 /** 63 * constructor 64 * @param module defines path to the library[if not in ldconfig db] or library name 65 * @param data defines process data 66 * @param toInit defines library init data 67 */ 68 process(const dodoString &module, 69 void *data, 70 void *toInit = NULL); 71 #endif 72 73 /** 74 * copy constructor 75 */ 76 process(const process &); 77 78 /** 79 * destructor 80 */ 81 virtual ~process(); 82 83 /** 84 * execute process 85 */ 86 virtual void run(); 87 88 /** 89 * stop all registered processes 90 */ 91 virtual void stop(); 92 93 /** 94 * wait for all registered processes termination 95 * @return exit status 96 */ 97 virtual int wait(); 98 99 /** 100 * @return true if process is running 101 */ 102 virtual bool isRunning() const; 43 103 44 104 #ifdef DL_EXT … … 51 111 char discription[256]; ///< discription of module 52 112 char hook[64]; ///< name of function in module that will be a hook 53 short action; ///< action on object destruction, @see job::onDestructionEnum113 short action; ///< action on object destruction, @see onDestructionEnum 54 114 }; 55 115 … … 58 118 * @brief defines type of init function for library 59 119 * @param data defines user data 60 * @note name in the library must be initPc JobProcessManagerModule120 * @note name in the library must be initPcExecutionProcessModule 61 121 */ 62 122 typedef __module__ (*initModule)(void *data); … … 65 125 * @typedef deinitModule 66 126 * @brief defines type of deinit function for library 67 * @note name in the library must be deinitPc JobProcessManagerModule127 * @note name in the library must be deinitPcExecutionProcessModule 68 128 */ 69 129 typedef void (*deinitModule)(); 130 131 /** 132 * @return info about library 133 * @param module defines path to the library[if not in ldconfig db] or library name 134 * @param toInit defines library init data 135 */ 136 static __module__ module(const dodoString &module, 137 void *toInit = NULL); 70 138 #endif 71 139 72 /** 73 * @class manager 74 * @brief provides processes management functionality 75 */ 76 class manager : public job::manager { 77 private: 140 private: 78 141 79 /** 80 * copy constructor 81 * @note to prevent copying 82 */ 83 manager(manager &); 84 85 public: 86 87 /** 88 * constructor 89 */ 90 manager(); 91 92 /** 93 * destructor 94 */ 95 virtual ~manager(); 96 97 /** 98 * add function as a process 99 * @return process identificator 100 * @param func defines function to execute 101 * @param data defines process data 102 * @param action defines action on object destruction if process is running, @see job::onDestructionEnum 103 * @note func must not call `exit` family call 104 */ 105 virtual unsigned long add(job::routine func, 106 void *data, 107 short action); 108 109 /** 110 * add function as a process 111 * @return process identificator 112 * @param func defines function to execute 113 * @param data defines process data 114 * @param action defines action on object destruction if process is running, @see job::onDestructionEnum 115 * @note func must not call `exit` family call 116 * this will immediately execute the process 117 */ 118 virtual unsigned long addNRun(job::routine func, 119 void *data, 120 short action); 121 122 /** 123 * remove registered process 124 * @param id defines process identificator 125 * @param terminate defines termination condition; if true and process is running stop execution of the process 126 */ 127 virtual void remove(unsigned long id, 128 bool terminate = false); 129 130 /** 131 * execute process 132 * @param id defines process identificator 133 * @param force defines run condition; if true and job is running run process anyway 134 */ 135 virtual void run(unsigned long id, 136 bool force = false); 137 138 /** 139 * stop process 140 * @param id defines process identificator 141 * @note sends SIGKILL to process 142 */ 143 virtual void stop(unsigned long id); 144 145 /** 146 * stop all registered processes 147 */ 148 virtual void stop(); 149 150 /** 151 * waits for process termination 152 * @return status of the job 153 * @param id defines process identificator 154 */ 155 virtual int wait(unsigned long id); 156 157 /** 158 * wait for all registered processes termination 159 */ 160 virtual void wait(); 161 162 /** 163 * @return true if process is running 164 * @param id defines process identificator 165 */ 166 virtual bool isRunning(unsigned long id) const; 167 168 /** 169 * @return amount of running processes 170 */ 171 virtual unsigned long running() const; 172 173 /** 174 * @return list of processes in object 175 */ 176 virtual dodoList<unsigned long> jobs(); 177 178 #ifdef DL_EXT 179 /** 180 * add function as a process from library 181 * @return process identificator 182 * @param module defines path to the library[if not in ldconfig db] or library name 183 * @param data defines process data 184 * @param toInit defines library init data 185 */ 186 virtual unsigned long add(const dodoString &module, 187 void *data, 188 void *toInit = NULL); 189 190 /** 191 * @return info about library 192 * @param module defines path to the library[if not in ldconfig db] or library name 193 * @param toInit defines library init data 194 */ 195 static __module__ module(const dodoString &module, 196 void *toInit = NULL); 197 #endif 198 199 protected: 200 201 /** 202 * @return true if process is running 203 * @param id defines process identificator 204 */ 205 bool _isRunning(dodoList<__process__ *>::iterator &id) const; 206 207 /** 208 * search processes by identificator 209 * @return true if process has been found 210 * @param id defines process identificator 211 * @note this sets internal class parameter 'current' to found process 212 */ 213 bool getProcess(unsigned long id) const; 214 215 unsigned long processNum; ///< number of registered processes 216 217 mutable dodoList<__process__ *> processes; ///< identificators of processes 218 219 mutable dodoList<__process__ *>::iterator current; ///< iterator for list of processes[for matched with getProcess method] 220 }; 142 mutable __process__ *handle; ///< process handle 221 143 }; 222 144 }; -
trunk/include/libdodo/pcExecutionProcessEx.h
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobProcessManagerEx.h2 * pcExecutionProcessEx.h 3 3 * 4 * Tue Feb 27 20075 * Copyright 200 7Ni@m4 * Wed Oct 08 2009 5 * Copyright 2009 Ni@m 6 6 * niam.niam@gmail.com 7 7 ****************************************************************************/ … … 28 28 */ 29 29 30 #ifndef _PC JOBPROCESSMANAGEREX_H_31 #define _PC JOBPROCESSMANAGEREX_H_ 130 #ifndef _PCEXECUTIONPROCESSEX_H_ 31 #define _PCEXECUTIONPROCESSEX_H_ 1 32 32 33 33 #include <libdodo/directives.h> … … 37 37 namespace dodo { 38 38 namespace pc { 39 namespace job { 40 namespace process { 41 /** 42 * libdodo defined errors 43 */ 44 enum managerExR { 45 MANAGEREX_ISALREADYRUNNING, 46 MANAGEREX_ISNOTLAUNCHED, 47 MANAGEREX_NOTFOUND, 48 }; 39 namespace execution { 40 /** 41 * libdodo defined errors 42 */ 43 enum processExR { 44 PROCESSEX_ISALREADYRUNNING, 45 PROCESSEX_ISNOTLAUNCHED, 46 }; 49 47 50 /** 51 * explanations for libdodo defined errors 52 */ 53 #define PCJOBPROCESSMANAGEREX_ISALREADYRUNNING_STR "Process is currently running" 54 #define PCJOBPROCESSMANAGEREX_ISNOTLAUNCHED_STR "Process is not launched" 55 #define PCJOBPROCESSMANAGEREX_NOTFOUND_STR "Process not found" 48 /** 49 * explanations for libdodo defined errors 50 */ 51 #define PCEXECUTIONPROCESSEX_ISALREADYRUNNING_STR "Process is currently running" 52 #define PCEXECUTIONPROCESSEX_ISNOTLAUNCHED_STR "Process is not launched" 56 53 57 /** 58 * IDs of functions where exception might be thrown 59 */ 60 enum managerFunctionsID { 61 MANAGEREX__ISRUNNING, 62 MANAGEREX_ADDNRUN, 63 MANAGEREX_RUN, 64 MANAGEREX_REMOVE, 65 MANAGEREX_STOP, 66 MANAGEREX_WAIT, 67 MANAGEREX_ISRUNNING, 54 /** 55 * IDs of functions where exception might be thrown 56 */ 57 enum processFunctionsID { 58 PROCESSEX_ISRUNNING, 59 PROCESSEX_RUN, 60 PROCESSEX_STOP, 61 PROCESSEX_WAIT, 68 62 #ifdef DL_EXT 69 MANAGEREX_MODULE,70 MANAGEREX_ADD,63 PROCESSEX_MODULE, 64 PROCESSEX_CONSTRUCTOR, 71 65 #endif 72 };73 66 }; 74 67 }; -
trunk/include/libdodo/pcExecutionThread.h
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobThreadManager.h2 * pcExecutionThread.h 3 3 * 4 * Tue Nov 29 20055 * Copyright 200 5Ni@m4 * Wed Oct 07 2009 5 * Copyright 2009 Ni@m 6 6 * niam.niam@gmail.com 7 7 ****************************************************************************/ … … 28 28 */ 29 29 30 #ifndef _PC JOBTHREADMANAGER_H_31 #define _PC JOBTHREADMANAGER_H_ 130 #ifndef _PCEXECUTIONTHREAD_H_ 31 #define _PCEXECUTIONTHREAD_H_ 1 32 32 33 33 #include <libdodo/directives.h> 34 34 35 #include <libdodo/pc JobManager.h>35 #include <libdodo/pcExecutionJob.h> 36 36 #include <libdodo/types.h> 37 37 38 38 namespace dodo { 39 39 namespace pc { 40 namespace job { 41 namespace thread { 42 struct __thread__; 40 namespace execution { 41 struct __thread__; 42 43 /** 44 * @class thread 45 * @brief provides thread management functionality 46 */ 47 class thread : public job { 48 public: 49 50 /** 51 * constructor 52 * @param func defines function to execute 53 * @param data defines process data 54 * @param action defines action on object destruction if thread is running, @see onDestructionEnum 55 * @param detached defines whether the thread will be detached 56 * @param stackSize defines stack size of the thread in bytes 57 */ 58 thread(routine func, 59 void *data, 60 short action, 61 bool detached=false, 62 unsigned long stackSize=8388608); 63 64 #ifdef DL_EXT 65 /** 66 * constructor 67 * @param module defines path to the library[if not in ldconfig db] or library name 68 * @param data defines thread data 69 * @param toInit defines library init data 70 */ 71 thread(const dodoString &module, 72 void *data, 73 void *toInit = NULL); 74 #endif 75 76 /** 77 * copy constructor 78 */ 79 thread(const thread &); 80 81 /** 82 * destructor 83 */ 84 virtual ~thread(); 85 86 /** 87 * execute thread 88 */ 89 virtual void run(); 90 91 /** 92 * stop thread 93 */ 94 virtual void stop(); 95 96 /** 97 * wait for thread termination 98 * @return status of the job 99 */ 100 virtual int wait(); 101 102 /** 103 * @return true if thread is running 104 */ 105 virtual bool isRunning() const; 43 106 44 107 #ifdef DL_EXT … … 52 115 char hook[64]; ///< name of function in module that will be a hook 53 116 bool detached; ///< true if thread is detached 54 int stackSize; ///< size of stack for thread [in bytes]55 short action; ///< action on object destruction, @see job::onDestructionEnum117 int stackSize; ///< size of stack for thread in bytes 118 short action; ///< action on object destruction, @see onDestructionEnum 56 119 }; 57 120 … … 60 123 * @brief defines type of init function for library 61 124 * @param data defines user data 62 * @note name in the library must be initPc JobThreadManagerModule125 * @note name in the library must be initPcExecutionThreadModule 63 126 */ 64 127 typedef __module__ (*initModule)(void *data); … … 67 130 * @typedef deinitModule 68 131 * @brief defines type of deinit function for library 69 * @note name in the library must be deinitPc JobThreadManagerModule132 * @note name in the library must be deinitPcExecutionThreadModule 70 133 */ 71 134 typedef void (*deinitModule)(); 72 #endif73 135 74 136 /** 75 * @class manager 76 * @brief provides threads management functionality 137 * @return info about library 138 * @param module defines path to the library[if not in ldconfig db] or library name 139 * @param toInit defines library init data 77 140 */ 78 class manager : public job::manager { 79 private: 141 static __module__ module(const dodoString &module, 142 void *toInit = NULL); 143 #endif 144 private: 80 145 81 /** 82 * copy constructor 83 * @note to prevent copying 84 */ 85 manager(manager &); 86 87 public: 88 89 /** 90 * constructor 91 */ 92 manager(); 93 94 /** 95 * destructor 96 */ 97 virtual ~manager(); 98 99 /** 100 * add function to run as a thread 101 * @return thread identificator 102 * @param func defines function to execute 103 * @param data defines process data 104 * @param action defines action on object destruction if thread is running, @see job::onDestructionEnum 105 */ 106 virtual unsigned long add(job::routine func, 107 void *data, 108 short action); 109 110 /** 111 * add function to run as a thread 112 * @return thread identificator 113 * @param func defines function to execute 114 * @param data defines process data 115 * @param action defines action on object destruction if thread is running, @see job::onDestructionEnum 116 * @note this will immediately execute the process 117 */ 118 virtual unsigned long addNRun(job::routine func, 119 void *data, 120 short action); 121 122 /** 123 * add function to run as a thread 124 * @return thread identificator 125 * @param func defines function to execute 126 * @param data defines process data 127 * @param action defines action on object destruction if thread is running, @see job::onDestructionEnum 128 * @note this will immediately execute the process 129 * the thread will be detached 130 */ 131 virtual unsigned long addNRunDetached(job::routine func, 132 void *data, 133 short action); 134 135 /** 136 * remove registered thread 137 * @param id defines thread identificator 138 * @param terminate defines termination condition; if true and thread is running stop execution of the process 139 */ 140 virtual void remove(unsigned long id, 141 bool terminate = false); 142 143 /** 144 * execute thread 145 * @param id defines thread identificator 146 * @param force defines run condition; if true and thread is running run thread anyway 147 */ 148 virtual void run(unsigned long id, 149 bool force = false); 150 151 /** 152 * stop thread 153 * @param id defines thread identificator 154 */ 155 virtual void stop(unsigned long id); 156 157 /** 158 * stop all registered threads 159 */ 160 virtual void stop(); 161 162 /** 163 * wait for thread termination 164 * @return status of the job 165 * @param id defines thread identificator 166 */ 167 virtual int wait(unsigned long id); 168 169 /** 170 * wait for all registered threads termination 171 */ 172 virtual void wait(); 173 174 /** 175 * @return true if thread is running 176 * @param id defines thread identificator 177 */ 178 virtual bool isRunning(unsigned long id) const; 179 180 /** 181 * @return amount of running threads 182 */ 183 virtual unsigned long running() const; 184 185 /** 186 * @return list of threads in object 187 */ 188 virtual dodoList<unsigned long> jobs(); 189 190 /** 191 * set maximum stack size 192 * @param id defines thread identificator 193 * @param size defines stack size of the thread 194 */ 195 virtual void setStackSize(unsigned long id, 196 unsigned long size); 197 198 199 #ifdef DL_EXT 200 /** 201 * add function as a thread from library 202 * @return thread identificator 203 * @param module defines path to the library[if not in ldconfig db] or library name 204 * @param data defines thread data 205 * @param toInit defines library init data 206 */ 207 virtual unsigned long add(const dodoString &module, 208 void *data, 209 void *toInit = NULL); 210 211 /** 212 * @return info about library 213 * @param module defines path to the library[if not in ldconfig db] or library name 214 * @param toInit defines library init data 215 */ 216 static __module__ module(const dodoString &module, 217 void *toInit = NULL); 218 #endif 219 220 /** 221 * block signals to thread 222 * @param signals defines signals to block/unblock, @see tools::osSignalsEnum 223 * @param block defines block condition 224 */ 225 static void blockSignal(int signals, 226 bool block = true); 227 228 protected: 229 230 /** 231 * add function to run as a thread 232 * @return thread identificator 233 * @param func defines function to execute 234 * @param data defines process data 235 * @param action defines action on object destruction if thread is running, @see job::onDestructionEnum 236 * @param detached defines whether thread will be detached or not 237 * @note this will immediately execute the process 238 */ 239 unsigned long addNRun(job::routine func, 240 void *data, 241 short action, 242 bool detached); 243 244 245 /** 246 * @return true if thread is running 247 * @param id indicates for what thread to set limit 248 */ 249 bool _isRunning(dodoList<__thread__ *>::iterator &id) const; 250 251 /** 252 * search threads by identificator 253 * @return true if thread has been found 254 * @param id defines thread identificator 255 * @note this sets internal class parameter 'current' to found thread 256 */ 257 bool getThread(unsigned long id) const; 258 259 mutable dodoList<__thread__ *> threads; ///< identificators of threads 260 261 unsigned long threadNum; ///< number of registered threads 262 263 #ifdef PTHREAD_EXT 264 pthread_attr_t attr; ///< thread join attribute 265 #endif 266 267 mutable dodoList<__thread__ *>::iterator current; ///< iterator for list of threads[for matched with getThread method] 268 }; 146 mutable __thread__ *handle; ///< thread handle 269 147 }; 270 148 }; -
trunk/include/libdodo/pcExecutionThreadEx.h
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobThreadManagerEx.h2 * pcExecutionThreadEx.h 3 3 * 4 * Wed Oct 5 20055 * Copyright 200 5Ni@m4 * Wed Oct 07 2009 5 * Copyright 2009 Ni@m 6 6 * niam.niam@gmail.com 7 7 ****************************************************************************/ … … 28 28 */ 29 29 30 #ifndef _PC JOBTHREADMANAGEREX_H_31 #define _PC JOBTHREADMANAGEREX_H_ 130 #ifndef _PCEXECUTIONTHREADEX_H_ 31 #define _PCEXECUTIONTHREADEX_H_ 1 32 32 33 33 #include <libdodo/directives.h> … … 37 37 namespace dodo { 38 38 namespace pc { 39 namespace job { 40 namespace thread { 41 /** 42 * libdodo defined errors 43 */ 44 enum managerExR { 45 MANAGEREX_ISALREADYRUNNING, 46 MANAGEREX_ISNOTLAUNCHED, 47 MANAGEREX_NOTFOUND, 48 MANAGEREX_ISDETACHED, 49 }; 39 namespace execution { 40 /** 41 * libdodo defined errors 42 */ 43 enum threadExR { 44 THREADEX_ISALREADYRUNNING, 45 THREADEX_ISNOTLAUNCHED, 46 THREADEX_ISDETACHED, 47 }; 50 48 51 /** 52 * libdodo defined errors' explanation 53 */ 54 #define PCJOBTHREADMANAGEREX_ISALREADYRUNNING_STR "Thread is currently running" 55 #define PCJOBTHREADMANAGEREX_ISNOTLAUNCHED_STR "Thread is not launched" 56 #define PCJOBTHREADMANAGEREX_NOTFOUND_STR "Thread not found" 57 #define PCJOBTHREADMANAGEREX_ISDETACHED_STR "Thread is detached" 49 /** 50 * libdodo defined errors explanation 51 */ 52 #define PCEXECUTIONTHREADEX_ISALREADYRUNNING_STR "Thread is currently running" 53 #define PCEXECUTIONTHREADEX_ISNOTLAUNCHED_STR "Thread is not launched" 54 #define PCEXECUTIONTHREADEX_ISDETACHED_STR "Thread is detached" 58 55 59 /** 60 * ID of function where exception was thrown 61 */ 62 enum managerFunctionsID { 63 MANAGEREX_RUN, 64 MANAGEREX_ADDNRUN, 65 MANAGEREX_REMOVE, 66 MANAGEREX_WAIT, 67 MANAGEREX_STOP, 68 MANAGEREX_ISRUNNING, 69 MANAGEREX_CONSTRUCTOR, 70 MANAGEREX__ISRUNNING, 71 MANAGEREX_SETSTACKSIZE, 56 /** 57 * ID of function where exception was thrown 58 */ 59 enum threadFunctionsID { 60 THREADEX_RUN, 61 THREADEX_WAIT, 62 THREADEX_STOP, 63 THREADEX_ISRUNNING, 64 THREADEX_CONSTRUCTOR, 72 65 #ifdef DL_EXT 73 MANAGEREX_MODULE, 74 MANAGEREX_ADD, 66 THREADEX_MODULE, 75 67 #endif 76 };77 68 }; 78 69 }; -
trunk/src/exceptionBasic.cc
r1373 r1374 102 102 false, 103 103 false, 104 false,105 104 false 106 105 }; … … 158 157 NULL, 159 158 NULL, 160 NULL,161 159 NULL 162 160 }; … … 214 212 NULL, 215 213 NULL, 216 NULL,217 214 NULL 218 215 }; … … 270 267 false, 271 268 false, 272 false,273 269 false 274 270 }; … … 277 273 278 274 void *basic::handles[] = { 279 NULL,280 275 NULL, 281 276 NULL, -
trunk/src/pcExecutionProcess.cc
r1373 r1374 1 1 /*************************************************************************** 2 * pc JobProcessManager.cc2 * pcExecutionProcess.cc 3 3 * 4 * Tue Feb 27 20075 * Copyright 200 7Ni@m4 * Wed Oct 08 2009 5 * Copyright 2009 Ni@m 6 6 * niam.niam@gmail.com 7 7 ****************************************************************************/ … … 41 41 #include <string.h> 42 42 43 #include <libdodo/pcJobManager.h>44 45 43 namespace dodo { 46 44 namespace pc { 47 namespace job { 48 namespace process { 45 namespace execution { 46 /** 47 * @struct __process__ 48 * @brief defines process information 49 */ 50 struct __process__ { 49 51 /** 50 * @struct __process__ 51 * @brief defines process information 52 * constuctor 52 53 */ 53 struct __process__ { 54 /** 55 * constuctor 56 */ 57 __process__(); 58 59 pid_t pid; ///< process pid 60 void *data; ///< process data 61 bool isRunning; ///< true if the process is running 62 bool joined; ///< true if the process was joined 63 int status; ///< process exit status 64 unsigned long position; ///< identificator 65 job::routine func; ///< function to execute 66 short action; ///< action on object destruction[@see job::onDestructionEnum] 67 #ifdef DL_EXT 68 void *handle; ///< handle to library 69 #endif 70 }; 54 __process__(): executed(false), 55 joined(false), 56 status(0) 57 { 58 } 59 60 pid_t pid; ///< process pid 61 void *data; ///< process data 62 bool executed; ///< true if the process was executed 63 bool joined; ///< true if the process was joined 64 int status; ///< process exit status 65 void *func; ///< function to execute 66 short action; ///< action on object destruction[@see onDestructionEnum] 67 #ifdef DL_EXT 68 void *handle; ///< handle to library 69 #endif 71 70 }; 72 71 }; … … 74 73 }; 75 74 76 #include <libdodo/pc JobProcessManager.h>75 #include <libdodo/pcExecutionProcess.h> 77 76 #include <libdodo/types.h> 78 #include <libdodo/pcJobManager.h> 79 #include <libdodo/pcJobProcessManagerEx.h> 80 81 using namespace dodo::pc::job::process; 82 83 __process__::__process__() : isRunning(false), 84 joined(false), 85 status(0) 86 { 87 } 88 89 //------------------------------------------------------------------- 90 91 manager::manager(manager &sp) 92 { 93 } 94 95 //------------------------------------------------------------------- 96 97 manager::manager() : processNum(0) 98 { 99 } 100 101 //------------------------------------------------------------------- 102 103 manager::~manager() 104 { 105 dodoList<__process__ *>::iterator i(processes.begin()), j(processes.end()); 106 107 #ifdef DL_EXT 108 deinitModule deinit; 109 #endif 110 111 for (; i != j; ++i) { 112 if (!_isRunning(i)) { 113 delete *i; 114 115 continue; 77 #include <libdodo/pcExecutionJob.h> 78 #include <libdodo/pcExecutionProcessEx.h> 79 80 using namespace dodo::pc::execution; 81 82 process::process(const process &p) : job(p), 83 handle(new __process__) 84 { 85 *handle = *p.handle; 86 } 87 88 //------------------------------------------------------------------- 89 90 process::process(routine func, 91 void *data, 92 short action) : job(), 93 handle(new __process__) 94 { 95 handle->data = data; 96 handle->func = (void *)func; 97 handle->action = action; 98 99 #ifdef DL_EXT 100 handle->handle = NULL; 101 #endif 102 } 103 104 //------------------------------------------------------------------- 105 106 #ifdef DL_EXT 107 process::process(const dodoString &module, 108 void *data, 109 void *toInit) 110 try : job(), 111 handle(NULL) { 112 handle = new __process__; 113 114 handle->data = data; 115 116 #ifdef DL_FAST 117 handle->handle = dlopen(module.data(), RTLD_LAZY | RTLD_NODELETE); 118 #else 119 handle->handle = dlopen(module.data(), RTLD_LAZY); 120 #endif 121 if (handle->handle == NULL) 122 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_CONSTRUCTOR, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 123 124 initModule init = (initModule)dlsym(handle->handle, "initPcExecutionProcessModule"); 125 if (init == NULL) 126 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_CONSTRUCTOR, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 127 128 __module__ m = init(toInit); 129 130 void *f = dlsym(handle->handle, m.hook); 131 if (f == NULL) 132 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_CONSTRUCTOR, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 133 134 handle->action = m.action; 135 handle->func = f; 136 } catch (...) { 137 if (handle) { 138 if (handle->handle) 139 dlclose(handle->handle); 140 141 delete handle; 142 } 143 } 144 #endif 145 146 //------------------------------------------------------------------- 147 148 process::~process() 149 { 150 if (!cloned) { 151 #ifdef DL_EXT 152 deinitModule deinit; 153 #endif 154 155 if (isRunning()) { 156 switch (handle->action) { 157 case ON_DESTRUCTION_KEEP_ALIVE: 158 159 waitpid(handle->pid, NULL, WNOHANG); 160 161 break; 162 163 case ON_DESTRUCTION_STOP: 164 165 kill(handle->pid, 2); 166 167 break; 168 169 case ON_DESTRUCTION_WAIT: 170 default: 171 172 waitpid(handle->pid, NULL, 0); 173 } 116 174 } 117 175 118 switch ((*i)->action) { 119 case job::ON_DESTRUCTION_KEEP_ALIVE: 120 121 waitpid((*i)->pid, NULL, WNOHANG); 122 123 break; 124 125 case job::ON_DESTRUCTION_STOP: 126 127 kill((*i)->pid, 2); 128 129 break; 130 131 case job::ON_DESTRUCTION_WAIT: 132 default: 133 134 waitpid((*i)->pid, NULL, 0); 135 } 136 137 #ifdef DL_EXT 138 if ((*i)->handle != NULL) { 139 deinit = (deinitModule)dlsym((*i)->handle, "deinitPcJobProcessManagerModule"); 176 #ifdef DL_EXT 177 if (handle->handle != NULL) { 178 deinit = (deinitModule)dlsym(handle->handle, "deinitPcExecutionProcessModule"); 140 179 if (deinit != NULL) 141 180 deinit(); 142 181 143 182 #ifndef DL_FAST 144 dlclose( (*i)->handle);183 dlclose(handle->handle); 145 184 #endif 146 185 } 147 186 #endif 148 149 delete *i;150 187 } 151 } 152 153 //------------------------------------------------------------------- 154 155 unsigned long 156 manager::add(job::routine func, 157 void *data, 158 short action) 159 { 160 __process__ *process = new __process__; 161 162 process->data = data; 163 process->func = func; 164 process->position = ++processNum; 165 process->action = action; 166 167 #ifdef DL_EXT 168 process->handle = NULL; 169 #endif 170 171 processes.push_back(process); 172 173 return process->position; 174 } 175 176 //------------------------------------------------------------------- 177 178 unsigned long 179 manager::addNRun(job::routine func, 180 void *data, 181 short action) 182 { 183 __process__ *process = new __process__; 184 185 process->data = data; 186 process->func = func; 187 process->position = ++processNum; 188 process->action = action; 189 190 #ifdef DL_EXT 191 process->handle = NULL; 192 #endif 193 194 processes.push_back(process); 188 189 delete handle; 190 } 191 192 //------------------------------------------------------------------- 193 194 bool 195 process::isRunning() const 196 { 197 if (!handle->executed) 198 return false; 199 200 int res = kill(handle->pid, 0); 201 if (res != 0) { 202 if (errno == ESRCH) 203 return false; 204 205 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_ISRUNNING, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 206 } 207 208 return true; 209 } 210 211 //------------------------------------------------------------------- 212 213 void 214 process::run() 215 { 216 if (isRunning()) 217 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_RUN, exception::ERRNO_LIBDODO, PROCESSEX_ISALREADYRUNNING, PCEXECUTIONPROCESSEX_ISALREADYRUNNING_STR, __LINE__, __FILE__); 195 218 196 219 pid_t pid = fork(); 197 220 198 if (pid == 0) 199 _exit( func(data));200 else {221 if (pid == 0) { 222 _exit(((routine)handle->func)(handle->data)); 223 } else { 201 224 if (pid == -1) 202 throw exception::basic(exception::MODULE_PC JOBPROCESSMANAGER, MANAGEREX_ADDNRUN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);225 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_RUN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 203 226 else 204 process->pid = pid;227 handle->pid = pid; 205 228 } 206 229 207 process->isRunning = true; 208 209 return process->position; 230 handle->executed = true; 231 handle->joined = false; 210 232 } 211 233 … … 213 235 214 236 void 215 manager::remove(unsigned long position, 216 bool force) 217 { 218 if (getProcess(position)) { 219 if (_isRunning(current)) { 220 if (!force) 221 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_REMOVE, exception::ERRNO_LIBDODO, MANAGEREX_ISALREADYRUNNING, PCJOBPROCESSMANAGEREX_ISALREADYRUNNING_STR, __LINE__, __FILE__); 222 else if (kill((*current)->pid, 2) == -1) 223 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_REMOVE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 224 225 } 226 227 #ifdef DL_EXT 228 if ((*current)->handle != NULL) { 229 deinitModule deinit; 230 231 deinit = (deinitModule)dlsym((*current)->handle, "deinitPcJobProcessManagerModule"); 232 if (deinit != NULL) 233 deinit(); 234 235 #ifndef DL_FAST 236 if (dlclose((*current)->handle) != 0) 237 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_REMOVE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 238 239 #endif 240 } 241 #endif 242 243 delete *current; 244 245 processes.erase(current); 246 } else 247 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_REMOVE, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCJOBPROCESSMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 248 } 249 250 //------------------------------------------------------------------- 251 252 bool 253 manager::getProcess(unsigned long position) const 254 { 255 dodoList<__process__ *>::iterator i(processes.begin()), j(processes.end()); 256 for (; i != j; ++i) { 257 if ((*i)->position == position) { 258 current = i; 259 260 return true; 261 } 262 } 263 264 return false; 265 } 266 267 //------------------------------------------------------------------- 268 269 bool 270 manager::_isRunning(dodoList<__process__ *>::iterator &position) const 271 { 272 if (!(*position)->isRunning) 273 return false; 274 275 int res = kill((*position)->pid, 0); 276 if (res != 0) { 277 if (errno == ESRCH) { 278 (*position)->isRunning = false; 279 280 return false; 281 } 282 283 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX__ISRUNNING, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 284 } 285 286 return true; 287 } 288 289 //------------------------------------------------------------------- 290 291 void 292 manager::run(unsigned long position, 293 bool force) 294 { 295 if (getProcess(position)) { 296 if (_isRunning(current) && !force) 297 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_RUN, exception::ERRNO_LIBDODO, MANAGEREX_ISALREADYRUNNING, PCJOBPROCESSMANAGEREX_ISALREADYRUNNING_STR, __LINE__, __FILE__); 298 299 pid_t pid = fork(); 300 301 if (pid == 0) 302 _exit((*current)->func((*current)->data)); 303 else { 304 if (pid == -1) 305 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_RUN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 306 else 307 (*current)->pid = pid; 308 } 309 310 (*current)->isRunning = true; 311 } else 312 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_RUN, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCJOBPROCESSMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 313 } 314 315 //------------------------------------------------------------------- 316 317 void 318 manager::stop(unsigned long position) 319 { 320 if (getProcess(position)) { 321 if (kill((*current)->pid, 9) == -1) 322 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_STOP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 323 324 (*current)->isRunning = false; 325 } else 326 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_STOP, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCJOBPROCESSMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 327 } 328 329 //------------------------------------------------------------------- 330 331 void 332 manager::stop() 333 { 334 dodoList<__process__ *>::iterator i(processes.begin()), j(processes.end()); 335 for (; i != j; ++i) { 336 if (!_isRunning(i)) 337 continue; 338 339 if (kill((*i)->pid, 9) == -1) 340 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_STOP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 341 342 (*i)->isRunning = false; 343 } 237 process::stop() 238 { 239 if (kill(handle->pid, 9) == -1) 240 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_STOP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 241 242 handle->executed = false; 344 243 } 345 244 … … 347 246 348 247 int 349 manager::wait(unsigned long position) 350 { 351 if (getProcess(position)) { 352 if ((*current)->joined) 353 return (*current)->status; 354 355 if (!(*current)->isRunning) 356 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_WAIT, exception::ERRNO_LIBDODO, MANAGEREX_ISNOTLAUNCHED, PCJOBPROCESSMANAGEREX_ISNOTLAUNCHED_STR, __LINE__, __FILE__); 357 358 int status; 359 360 if (waitpid((*current)->pid, &status, 0) == -1) 361 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_WAIT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 362 363 if (WIFEXITED(status)) 364 (*current)->status = WEXITSTATUS(status); 365 (*current)->isRunning = false; 366 (*current)->joined = true; 367 368 return (*current)->status; 369 } else 370 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_WAIT, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCJOBPROCESSMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 371 } 372 373 //------------------------------------------------------------------- 374 375 void 376 manager::wait() 377 { 248 process::wait() 249 { 250 if (handle->joined) 251 return handle->status; 252 253 if (!handle->executed) 254 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_WAIT, exception::ERRNO_LIBDODO, PROCESSEX_ISNOTLAUNCHED, PCEXECUTIONPROCESSEX_ISNOTLAUNCHED_STR, __LINE__, __FILE__); 255 378 256 int status; 379 257 380 dodoList<__process__ *>::iterator i(processes.begin()), j(processes.end()); 381 for (; i != j; ++i) { 382 if ((*i)->joined) 383 continue; 384 385 if (!(*i)->isRunning) 386 continue; 387 388 if (waitpid((*i)->pid, &status, 0) == -1) 389 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_WAIT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 390 391 if (WIFEXITED(status)) 392 (*i)->status = WEXITSTATUS(status); 393 394 (*i)->isRunning = false; 395 (*i)->joined = true; 396 } 397 } 398 399 //------------------------------------------------------------------- 400 401 bool 402 manager::isRunning(unsigned long position) const 403 { 404 if (getProcess(position)) 405 return _isRunning(current); 406 else 407 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_ISRUNNING, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCJOBPROCESSMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 408 } 409 410 //------------------------------------------------------------------- 411 412 unsigned long 413 manager::running() const 414 { 415 unsigned long amount(0); 416 417 dodoList<__process__ *>::iterator i(processes.begin()), j(processes.end()); 418 for (; i != j; ++i) 419 if (_isRunning(i)) 420 ++amount; 421 422 return amount; 423 } 424 425 //------------------------------------------------------------------- 426 427 #ifdef DL_EXT 428 __module__ 429 manager::module(const dodoString &module, 258 if (waitpid(handle->pid, &status, 0) == -1) 259 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_WAIT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 260 261 if (WIFEXITED(status)) 262 handle->status = WEXITSTATUS(status); 263 264 handle->executed = false; 265 handle->joined = true; 266 267 return handle->status; 268 } 269 270 //------------------------------------------------------------------- 271 272 #ifdef DL_EXT 273 process::__module__ 274 process::module(const dodoString &module, 430 275 void *toInit) 431 276 { … … 436 281 #endif 437 282 if (handle == NULL) 438 throw exception::basic(exception::MODULE_PC JOBPROCESSMANAGER, MANAGEREX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__);439 440 initModule init = (initModule)dlsym(handle, "initPc JobProcessManagerModule");283 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 284 285 initModule init = (initModule)dlsym(handle, "initPcExecutionProcessModule"); 441 286 if (init == NULL) 442 throw exception::basic(exception::MODULE_PC JOBPROCESSMANAGER, MANAGEREX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__);287 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 443 288 444 289 __module__ mod = init(toInit); 290 291 deinitModule deinit = (deinitModule)dlsym(handle, "deinitPcExecutionProcessModule"); 292 if (deinit != NULL) 293 deinit(); 445 294 446 295 #ifndef DL_FAST 447 296 if (dlclose(handle) != 0) 448 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 449 297 throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 450 298 #endif 451 299 452 300 return mod; 453 301 } 454 455 //------------------------------------------------------------------- 456 457 unsigned long 458 manager::add(const dodoString &module, 459 void *data, 460 void *toInit) 461 { 462 __process__ *process = new __process__; 463 464 process->data = data; 465 process->position = ++processNum; 466 467 #ifdef DL_FAST 468 process->handle = dlopen(module.data(), RTLD_LAZY | RTLD_NODELETE); 469 #else 470 process->handle = dlopen(module.data(), RTLD_LAZY); 471 #endif 472 if (process->handle == NULL) 473 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_ADD, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 474 475 initModule init = (initModule)dlsym(process->handle, "initPcJobProcessManagerModule"); 476 if (init == NULL) 477 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_ADD, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 478 479 __module__ temp = init(toInit); 480 481 job::routine in = (job::routine)dlsym(process->handle, temp.hook); 482 if (in == NULL) 483 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_ADD, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 484 485 process->action = temp.action; 486 process->func = in; 487 488 processes.push_back(process); 489 490 return process->position; 491 } 492 #endif 493 494 //------------------------------------------------------------------- 495 496 dodoList<unsigned long> 497 manager::jobs() 498 { 499 dodoList<unsigned long> ids; 500 501 dodoList<__process__ *>::iterator i(processes.begin()), j(processes.end()); 502 for (; i != j; ++i) 503 ids.push_back((*i)->position); 504 505 return ids; 506 } 507 508 //------------------------------------------------------------------- 509 302 #endif 303 304 //------------------------------------------------------------------- 305 -
trunk/src/pcExecutionThread.cc
r1373 r1374 1 1 /*************************************************************************** 2 * pcJobThread Manager.cc2 * pcJobThreadThread.cc 3 3 * 4 * Wed Nov 30 20055 * Copyright 200 5Ni@m4 * Wed Oct 08 2009 5 * Copyright 2009 Ni@m 6 6 * niam.niam@gmail.com 7 7 ****************************************************************************/ … … 40 40 #include <string.h> 41 41 42 #include <libdodo/pcJobThreadManager.h>43 #include <libdodo/pcJobManager.h>44 #include <libdodo/toolsOs.h>45 #include <libdodo/pcJobThreadManagerEx.h>46 #include <libdodo/types.h>47 48 42 namespace dodo { 49 43 namespace pc { 50 namespace job { 51 namespace thread { 44 namespace execution { 45 /** 46 * @struct __thread__ 47 * @brief defines process information 48 */ 49 struct __thread__ { 52 50 /** 53 * @struct __thread__ 54 * @brief defines process information 51 * contructor 55 52 */ 56 struct __thread__ { 57 /** 58 * contructor 59 */ 60 __thread__(); 61 62 #ifdef PTHREAD_EXT 63 pthread_t thread; ///< thread descriptor 64 65 /** 66 * @return thread exit status 67 * @param data defines user data 68 */ 69 static void *routine(void *data); 70 #endif 71 72 void *data; ///< thread data 73 bool isRunning; ///< true if thread is running 74 bool joined; ///< true if the thread was joined 75 int status; ///< thread exit status 76 bool detached; ///< true if thread is detached 77 unsigned long id; ///< identificator 78 job::routine func; ///< function to execute 79 int stackSize; ///< size of stack for thread[in bytes] 80 short action; ///< action on object destruction[@see job::onDestructionEnum] 81 82 #ifdef DL_EXT 83 void *handle; ///< handle to library 84 #endif 85 }; 53 __thread__() : 54 #ifdef PTHREAD_EXT 55 thread(0), 56 #endif 57 executed(false), 58 joined(false), 59 status(0) 60 { 61 } 62 63 #ifdef PTHREAD_EXT 64 pthread_t thread; ///< thread descriptor 65 pthread_attr_t attr; ///< thread attribute 66 67 /** 68 * @return thread exit status 69 * @param data defines user data 70 */ 71 static void *routine(void *data); 72 #endif 73 74 void *data; ///< thread data 75 bool executed; ///< true if thread is running 76 bool joined; ///< true if the thread was joined 77 int status; ///< thread exit status 78 bool detached; ///< true if thread is detached 79 void *func; ///< function to execute 80 short action; ///< action on object destruction[@see onDestructionEnum] 81 #ifdef DL_EXT 82 void *handle; ///< handle to library 83 #endif 86 84 }; 87 85 }; … … 89 87 }; 90 88 91 using namespace dodo::pc::job::thread; 92 93 __thread__::__thread__() : 94 #ifdef PTHREAD_EXT 95 thread(0), 96 #endif 97 isRunning(false), 98 joined(false), 99 status(0) 100 { 101 } 102 103 //------------------------------------------------------------------- 89 #include <libdodo/pcExecutionThread.h> 90 #include <libdodo/pcExecutionJob.h> 91 #include <libdodo/toolsOs.h> 92 #include <libdodo/pcExecutionThreadEx.h> 93 #include <libdodo/types.h> 94 95 using namespace dodo::pc::execution; 104 96 105 97 #ifdef PTHREAD_EXT … … 109 101 __thread__ *ti = (__thread__ *)data; 110 102 111 ti->status = ti->func(ti->data);103 ti->status = ((execution::routine)ti->func)(ti->data); 112 104 113 105 return NULL; … … 117 109 //------------------------------------------------------------------- 118 110 119 manager::manager(manager &st) 120 { 121 } 122 123 //------------------------------------------------------------------- 124 125 manager::manager() : threadNum(0) 126 { 127 #ifdef PTHREAD_EXT 128 pthread_attr_init(&attr); 129 #endif 130 } 131 132 //------------------------------------------------------------------- 133 134 manager::~manager() 135 { 136 #ifdef PTHREAD_EXT 137 pthread_attr_destroy(&attr); 138 #endif 139 140 dodoList<__thread__ *>::iterator i(threads.begin()), j(threads.end()); 141 142 #ifdef DL_EXT 143 deinitModule deinit; 144 #endif 145 146 for (; i != j; ++i) { 147 if (!_isRunning(i) || (*i)->detached) { 148 delete *i; 149 150 continue; 151 } 152 153 switch ((*i)->action) { 154 case job::ON_DESTRUCTION_KEEP_ALIVE: 155 156 #ifdef PTHREAD_EXT 157 pthread_detach((*i)->thread); 111 thread::thread(const thread &t) : job(t), 112 handle(new __thread__) 113 { 114 *handle = *t.handle; 115 } 116 117 //------------------------------------------------------------------- 118 119 thread::thread(routine func, 120 void *data, 121 short action, 122 bool detached, 123 unsigned long stackSize) 124 try : job(), 125 handle(NULL) { 126 handle = new __thread__; 127 128 #ifdef PTHREAD_EXT 129 pthread_attr_init(&handle->attr); 130 #endif 131 132 handle->detached = detached; 133 handle->data = data; 134 handle->func = (void *)func; 135 handle->action = action; 136 137 #ifdef DL_EXT 138 handle->handle = NULL; 139 #endif 140 141 #ifdef PTHREAD_EXT 142 if (detached) 143 pthread_attr_setdetachstate(&handle->attr, PTHREAD_CREATE_DETACHED); 144 else 145 pthread_attr_setdetachstate(&handle->attr, PTHREAD_CREATE_JOINABLE); 146 147 errno = pthread_attr_setstacksize(&handle->attr, stackSize); 148 if (errno != 0) 149 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 150 #endif 151 152 #ifdef PTHREAD_EXT 153 pthread_attr_destroy(&handle->attr); 154 #endif 155 } catch (...) { 156 if (handle) { 157 #ifdef PTHREAD_EXT 158 pthread_attr_destroy(&handle->attr); 159 #endif 160 delete handle; 161 } 162 } 163 164 #ifdef DL_EXT 165 thread::thread(const dodoString &module, 166 void *data, 167 void *toInit) 168 try : job(), 169 handle(NULL) { 170 handle = new __thread__; 171 172 handle->data = data; 173 174 #ifdef DL_FAST 175 handle->handle = dlopen(module.data(), RTLD_LAZY | RTLD_NODELETE); 176 #else 177 handle->handle = dlopen(module.data(), RTLD_LAZY); 178 #endif 179 if (handle->handle == NULL) 180 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 181 182 initModule init = (initModule)dlsym(handle->handle, "initPcExecutionThreadModule"); 183 if (init == NULL) 184 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 185 186 __module__ m = init(toInit); 187 188 void *f = dlsym(handle->handle, m.hook); 189 if (f == NULL) 190 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 191 192 if (m.detached) 193 pthread_attr_setdetachstate(&handle->attr, PTHREAD_CREATE_DETACHED); 194 else 195 pthread_attr_setdetachstate(&handle->attr, PTHREAD_CREATE_JOINABLE); 196 197 errno = pthread_attr_setstacksize(&handle->attr, m.stackSize); 198 if (errno != 0) 199 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_CONSTRUCTOR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 200 201 handle->detached = m.detached; 202 handle->action = m.action; 203 handle->func = f; 204 } catch (...) { 205 if (handle) { 206 if (handle->handle) 207 dlclose(handle->handle); 208 209 #ifdef PTHREAD_EXT 210 pthread_attr_destroy(&handle->attr); 211 #endif 212 213 delete handle; 214 } 215 } 216 #endif 217 218 //------------------------------------------------------------------- 219 220 thread::~thread() 221 { 222 if (!cloned) { 223 #ifdef DL_EXT 224 deinitModule deinit; 225 #endif 226 227 #ifdef PTHREAD_EXT 228 pthread_attr_destroy(&handle->attr); 229 #endif 230 231 if (!isRunning() || handle->detached) 232 return; 233 234 switch (handle->action) { 235 case ON_DESTRUCTION_KEEP_ALIVE: 236 237 #ifdef PTHREAD_EXT 238 pthread_detach(handle->thread); 158 239 #endif 159 240 160 241 break; 161 242 162 case job::ON_DESTRUCTION_STOP:163 164 #ifdef PTHREAD_EXT 165 pthread_cancel( (*i)->thread);243 case ON_DESTRUCTION_STOP: 244 245 #ifdef PTHREAD_EXT 246 pthread_cancel(handle->thread); 166 247 #endif 167 248 168 249 break; 169 250 170 case job::ON_DESTRUCTION_WAIT:251 case ON_DESTRUCTION_WAIT: 171 252 default: 172 253 173 254 #ifdef PTHREAD_EXT 174 pthread_join( (*i)->thread, NULL);255 pthread_join(handle->thread, NULL); 175 256 #endif 176 257 … … 179 260 180 261 #ifdef DL_EXT 181 if ( (*i)->handle != NULL) {182 deinit = (deinitModule)dlsym( (*i)->handle, "deinitPcThreadModule");262 if (handle->handle != NULL) { 263 deinit = (deinitModule)dlsym(handle->handle, "deinitPcExecutionThreadModule"); 183 264 if (deinit != NULL) 184 265 deinit(); 185 266 186 267 #ifndef DL_FAST 187 dlclose( (*i)->handle);268 dlclose(handle->handle); 188 269 #endif 189 270 } 190 271 #endif 191 192 delete *i; 193 } 194 } 195 196 //------------------------------------------------------------------- 197 198 unsigned long 199 manager::add(job::routine func, 200 void *data, 201 short action) 202 { 203 __thread__ *thread = new __thread__; 204 205 thread->detached = false; 206 thread->data = data; 207 thread->func = func; 208 thread->id = ++threadNum; 209 thread->stackSize = 8388608; 210 thread->action = action; 211 212 #ifdef DL_EXT 213 thread->handle = NULL; 214 #endif 215 216 threads.push_back(thread); 217 218 return thread->id; 272 } 273 274 delete handle; 275 } 276 277 //------------------------------------------------------------------- 278 279 void 280 thread::run() 281 { 282 if (isRunning()) 283 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_RUN, exception::ERRNO_LIBDODO, THREADEX_ISALREADYRUNNING, PCEXECUTIONTHREADEX_ISALREADYRUNNING_STR, __LINE__, __FILE__); 284 285 #ifdef PTHREAD_EXT 286 errno = pthread_create(&(handle->thread), &handle->attr, __thread__::routine, handle); 287 if (errno != 0) 288 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_RUN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 289 #endif 290 291 handle->executed = true; 292 } 293 294 //------------------------------------------------------------------- 295 296 int 297 thread::wait() 298 { 299 if (handle->detached) { 300 if (isRunning()) 301 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_WAIT, exception::ERRNO_LIBDODO, THREADEX_ISDETACHED, PCEXECUTIONTHREADEX_ISDETACHED_STR, __LINE__, __FILE__); 302 else 303 return handle->status; 304 } 305 306 if (handle->joined) 307 return handle->status; 308 309 if (!handle->executed) 310 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_WAIT, exception::ERRNO_LIBDODO, THREADEX_ISNOTLAUNCHED, PCEXECUTIONTHREADEX_ISNOTLAUNCHED_STR, __LINE__, __FILE__); 311 312 int status = 0; 313 314 #ifdef PTHREAD_EXT 315 errno = pthread_join(handle->thread, NULL); 316 if (errno != 0) 317 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_WAIT, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 318 319 status = handle->status; 320 #endif 321 322 handle->joined = true; 323 handle->executed = false; 324 325 return status; 326 } 327 328 //------------------------------------------------------------------- 329 330 void 331 thread::stop() 332 { 333 #ifdef PTHREAD_EXT 334 errno = pthread_cancel(handle->thread); 335 if (errno != 0) 336 throw exception::basic(exception::MODULE_PCEXECUTIONTHREAD, THREADEX_STOP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 337 #endif 338 339 handle->executed = false; 219 340 } 220 341 … … 222 343 223 344 bool 224 manager::getThread(unsigned long id) const 225 { 226 dodoList<__thread__ *>::iterator i(threads.begin()), j(threads.end()); 227 for (; i != j; ++i) { 228 if ((*i)->id == id) { 229 current = i; 230 231 return true; 232 } 233 } 234 235 return false; 236 } 237 238 //------------------------------------------------------------------- 239 240 void 241 manager::remove(unsigned long id, 242 bool force) 243 { 244 if (getThread(id)) { 245 if (_isRunning(current)) { 246 if (!force) 247 throw exception::basic(exception::MODULE_PCJOBTHREADMANAGER, MANAGEREX_REMOVE, exception::ERRNO_LIBDODO, MANAGEREX_ISALREADYRUNNING, PCJOBTHREADMANAGEREX_ISALREADYRUNNING_STR, __LINE__, __FILE__); 248 else { 249 #ifdef PTHREAD_EXT 250
