Changeset 1374:501ccac7f3c0


Ignore:
Timestamp:
10/17/09 00:56:43 (2 years ago)
Author:
niam
Branch:
default
Children:
1375:7463cec1af99, 1380:3d4feefdfb45
Message:

{issue #91[resolved]} reworked pc thread/process interfaces for easy handling one single job

Files:
2 added
9 edited
8 moved

Legend:

Unmodified
Added
Removed
  • .hgignore

    r1110 r1374  
    66.settings 
    77*.so* 
     8*.a 
    89*.cgi 
    910*.fcgi 
     
    1415*.png 
    1516*.swp 
     17*.d 
    1618test 
    1719module 
  • trunk/include/libdodo/exceptionBasic.h

    r1373 r1374  
    9797                        MODULE_IONETWORKHTTP, 
    9898                        MODULE_IOEVENTMANAGER, 
    99                         MODULE_PCJOBPROCESSMANAGER, 
    100                         MODULE_PCJOBTHREADMANAGER, 
     99                        MODULE_PCEXECUTIONMANAGER, 
     100                        MODULE_PCEXECUTIONPROCESS, 
     101                        MODULE_PCEXECUTIONTHREAD, 
    101102                        MODULE_PCSYNCTHREADDATASINGLE, 
    102103                        MODULE_PCSYNCPROCESSDATASINGLE, 
  • trunk/include/libdodo/pc.h

    r1373 r1374  
    3131#define _PC_H_ 1 
    3232 
    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> 
    3839#include <libdodo/pcSyncDataCollection.h> 
    3940#include <libdodo/pcSyncDataSingle.h> 
  • trunk/include/libdodo/pcExecutionManager.h

    r1373 r1374  
    11/*************************************************************************** 
    2  *            pcJobManager.h 
     2 *            pcExecutionManager.h 
    33 * 
    44 *  Mon Mar 05 2007 
     
    2828 */ 
    2929 
    30 #ifndef _PCJOBMANAGER_H_ 
    31 #define _PCJOBMANAGER_H_ 1 
     30#ifndef _PCEXECUTIONMANAGER_H_ 
     31#define _PCEXECUTIONMANAGER_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
    3434 
    3535#include <libdodo/types.h> 
     36#include <libdodo/pcSyncSection.h> 
    3637 
    3738namespace dodo { 
    3839        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 { 
    5641                        /** 
    5742                         * @class manager 
    5843                         * @brief provides interface for jobs management 
    5944                         */ 
     45                        template <typename T> 
    6046                        class manager { 
    6147                          public: 
     48 
     49                                /** 
     50                                 * constructor 
     51                                 */ 
     52                                manager(); 
     53 
    6254                                /** 
    6355                                 * destructor 
    6456                                 */ 
    65                                 virtual ~manager() = 0; 
     57                                virtual ~manager(); 
    6658 
    6759                                /** 
    68                                  * add function as a job 
     60                                 * add a job 
    6961                                 * @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 
    7363                                 */ 
    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); 
    8965 
    9066                                /** 
    9167                                 * remove registered job 
    9268                                 * @param id defines job identificator 
    93                                  * @param terminate defines termination condition; if true and job is running stop execution of the job 
     69                                 * @param terminate defines termination condition 
    9470                                 */ 
    9571                                virtual void remove(unsigned long id, 
    96                                                                  bool          terminate = false) = 0; 
     72                                                                 bool          terminate = false); 
    9773 
    9874                                /** 
     
    10177                                 * @param force defines run condition; if true and job is running run job anyway 
    10278                                 */ 
    103                                 virtual void run(unsigned long id, 
    104                                                                  bool          force = false) = 0; 
     79                                virtual void run(unsigned long id); 
    10580 
    10681                                /** 
     
    10883                                 * @param id defines job identificator 
    10984                                 */ 
    110                                 virtual void stop(unsigned long id) = 0; 
     85                                virtual void stop(unsigned long id); 
    11186 
    11287                                /** 
    11388                                 * stop all registered jobs 
    11489                                 */ 
    115                                 virtual void stop() = 0; 
     90                                virtual void stop(); 
    11691 
    11792                                /** 
     
    12095                                 * @param id defines job identificator 
    12196                                 */ 
    122                                 virtual int wait(unsigned long id) = 0; 
     97                                virtual int wait(unsigned long id); 
    12398 
    12499                                /** 
    125100                                 * wait for all registered jobs termination 
    126101                                 */ 
    127                                 virtual void wait() = 0; 
     102                                virtual void wait(); 
    128103 
    129104                                /** 
     
    131106                                 * @param id defines job identificator 
    132107                                 */ 
    133                                 virtual bool isRunning(unsigned long id) const = 0; 
     108                                virtual bool isRunning(unsigned long id) const; 
    134109 
    135110                                /** 
    136111                                 * @return amount of running jobs 
    137112                                 */ 
    138                                 virtual unsigned long running() const = 0; 
     113                                virtual unsigned long running() const; 
    139114 
    140115                                /** 
    141116                                 * @return list of jobs in object 
    142117                                 */ 
    143                                 virtual dodoList<unsigned long> jobs() = 0; 
     118                                virtual dodoList<unsigned long> jobs(); 
    144119 
    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 
    157127                        }; 
    158128                }; 
    159129        }; 
    160130}; 
     131 
     132#include <libdodo/pcExecutionManager.inline> 
     133 
    161134#endif 
  • trunk/include/libdodo/pcExecutionManager.inline

    r1373 r1374  
    11/*************************************************************************** 
    2  *            pcJobManager.cc 
     2 *            pcExecutionManager.inline 
    33 * 
    44 *  Sun Oct  30 2007 
     
    3030#include <libdodo/directives.h> 
    3131 
    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 
     38template <typename T> 
     39dodo::pc::execution::manager<T>::manager() : counter(0), 
     40                                                                                         keeper(new pc::sync::thread::section) 
     41{ 
     42} 
     43 
     44//------------------------------------------------------------------- 
     45 
     46template <typename T> 
     47dodo::pc::execution::manager<T>::~manager() 
     48{ 
     49        delete keeper; 
     50} 
     51 
     52//------------------------------------------------------------------- 
     53 
     54template <typename T> 
     55unsigned long 
     56dodo::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 
     67template <typename T> 
     68void 
     69dodo::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 
     87template <typename T> 
     88void 
     89dodo::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 
     103template <typename T> 
     104void 
     105dodo::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 
     119template <typename T> 
     120void 
     121dodo::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 
     133template <typename T> 
     134int 
     135dodo::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 
     149template <typename T> 
     150void 
     151dodo::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 
     163template <typename T> 
     164bool 
     165dodo::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 
     179template <typename T> 
     180unsigned long 
     181dodo::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 
     198template <typename T> 
     199dodoList<unsigned long> 
     200dodo::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  
    11/*************************************************************************** 
    2  *            pcJobProcessManager.h 
     2 *            pcExecutionProcess.h 
    33 * 
    4  *  Tue Feb 27 2007 
    5  *  Copyright  2007  Ni@m 
     4 *  Wed Oct 08 2009 
     5 *  Copyright  2009  Ni@m 
    66 *  niam.niam@gmail.com 
    77 ****************************************************************************/ 
     
    2828 */ 
    2929 
    30 #ifndef _PCJOBPROCESSMANAGER_H_ 
    31 #define _PCJOBPROCESSMANAGER_H_ 1 
     30#ifndef _PCEXECUTIONPROCESS_H_ 
     31#define _PCEXECUTIONPROCESS_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
    3434 
    3535#include <libdodo/types.h> 
    36 #include <libdodo/pcJobManager.h> 
     36#include <libdodo/pcExecutionJob.h> 
    3737 
    3838namespace dodo { 
    3939        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; 
    43103 
    44104#ifdef DL_EXT 
     
    51111                                        char          discription[256]; ///< discription of module 
    52112                                        char          hook[64];         ///< name of function in module that will be a hook 
    53                                         short         action;           ///< action on object destruction, @see job::onDestructionEnum 
     113                                        short         action;           ///< action on object destruction, @see onDestructionEnum 
    54114                                }; 
    55115 
     
    58118                                 * @brief defines type of init function for library 
    59119                                 * @param data defines user data 
    60                                  * @note name in the library must be initPcJobProcessManagerModule 
     120                                 * @note name in the library must be initPcExecutionProcessModule 
    61121                                 */ 
    62122                                typedef __module__ (*initModule)(void *data); 
     
    65125                                 * @typedef deinitModule 
    66126                                 * @brief defines type of deinit function for library 
    67                                  * @note name in the library must be deinitPcJobProcessManagerModule 
     127                                 * @note name in the library must be deinitPcExecutionProcessModule 
    68128                                 */ 
    69129                                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); 
    70138#endif 
    71139 
    72                                 /** 
    73                                  * @class manager 
    74                                  * @brief provides processes management functionality 
    75                                  */ 
    76                                 class manager : public job::manager { 
    77                                   private: 
     140                          private: 
    78141 
    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 
    221143                        }; 
    222144                }; 
  • trunk/include/libdodo/pcExecutionProcessEx.h

    r1373 r1374  
    11/*************************************************************************** 
    2  *            pcJobProcessManagerEx.h 
     2 *            pcExecutionProcessEx.h 
    33 * 
    4  *  Tue Feb 27 2007 
    5  *  Copyright  2007  Ni@m 
     4 *  Wed Oct 08 2009 
     5 *  Copyright  2009  Ni@m 
    66 *  niam.niam@gmail.com 
    77 ****************************************************************************/ 
     
    2828 */ 
    2929 
    30 #ifndef _PCJOBPROCESSMANAGEREX_H_ 
    31 #define _PCJOBPROCESSMANAGEREX_H_ 1 
     30#ifndef _PCEXECUTIONPROCESSEX_H_ 
     31#define _PCEXECUTIONPROCESSEX_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
     
    3737namespace dodo { 
    3838        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                        }; 
    4947 
    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" 
    5653 
    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, 
    6862#ifdef DL_EXT 
    69                                         MANAGEREX_MODULE, 
    70                                         MANAGEREX_ADD, 
     63                                PROCESSEX_MODULE, 
     64                                PROCESSEX_CONSTRUCTOR, 
    7165#endif 
    72                                 }; 
    7366                        }; 
    7467                }; 
  • trunk/include/libdodo/pcExecutionThread.h

    r1373 r1374  
    11/*************************************************************************** 
    2  *            pcJobThreadManager.h 
     2 *            pcExecutionThread.h 
    33 * 
    4  *  Tue Nov 29 2005 
    5  *  Copyright  2005  Ni@m 
     4 *  Wed Oct 07 2009 
     5 *  Copyright  2009  Ni@m 
    66 *  niam.niam@gmail.com 
    77 ****************************************************************************/ 
     
    2828 */ 
    2929 
    30 #ifndef _PCJOBTHREADMANAGER_H_ 
    31 #define _PCJOBTHREADMANAGER_H_ 1 
     30#ifndef _PCEXECUTIONTHREAD_H_ 
     31#define _PCEXECUTIONTHREAD_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
    3434 
    35 #include <libdodo/pcJobManager.h> 
     35#include <libdodo/pcExecutionJob.h> 
    3636#include <libdodo/types.h> 
    3737 
    3838namespace dodo { 
    3939        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; 
    43106 
    44107#ifdef DL_EXT 
     
    52115                                        char          hook[64];         ///< name of function in module that will be a hook 
    53116                                        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::onDestructionEnum 
     117                                        int           stackSize;        ///< size of stack for thread in bytes 
     118                                        short         action;           ///< action on object destruction, @see onDestructionEnum 
    56119                                }; 
    57120 
     
    60123                                 * @brief defines type of init function for library 
    61124                                 * @param data defines user data 
    62                                  * @note name in the library must be initPcJobThreadManagerModule 
     125                                 * @note name in the library must be initPcExecutionThreadModule 
    63126                                 */ 
    64127                                typedef __module__ (*initModule)(void *data); 
     
    67130                                 * @typedef deinitModule 
    68131                                 * @brief defines type of deinit function for library 
    69                                  * @note name in the library must be deinitPcJobThreadManagerModule 
     132                                 * @note name in the library must be deinitPcExecutionThreadModule 
    70133                                 */ 
    71134                                typedef void (*deinitModule)(); 
    72 #endif 
    73135 
    74136                                /** 
    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 
    77140                                 */ 
    78                                 class manager : public job::manager { 
    79                                   private: 
     141                                static __module__ module(const dodoString &module, 
     142                                                                                 void             *toInit = NULL); 
     143#endif 
     144                          private: 
    80145 
    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 
    269147                        }; 
    270148                }; 
  • trunk/include/libdodo/pcExecutionThreadEx.h

    r1373 r1374  
    11/*************************************************************************** 
    2  *            pcJobThreadManagerEx.h 
     2 *            pcExecutionThreadEx.h 
    33 * 
    4  *  Wed Oct 5 2005 
    5  *  Copyright  2005  Ni@m 
     4 *  Wed Oct 07 2009 
     5 *  Copyright  2009  Ni@m 
    66 *  niam.niam@gmail.com 
    77 ****************************************************************************/ 
     
    2828 */ 
    2929 
    30 #ifndef _PCJOBTHREADMANAGEREX_H_ 
    31 #define _PCJOBTHREADMANAGEREX_H_ 1 
     30#ifndef _PCEXECUTIONTHREADEX_H_ 
     31#define _PCEXECUTIONTHREADEX_H_ 1 
    3232 
    3333#include <libdodo/directives.h> 
     
    3737namespace dodo { 
    3838        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                        }; 
    5048 
    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" 
    5855 
    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, 
    7265#ifdef DL_EXT 
    73                                         MANAGEREX_MODULE, 
    74                                         MANAGEREX_ADD, 
     66                                THREADEX_MODULE, 
    7567#endif 
    76                                 }; 
    7768                        }; 
    7869                }; 
  • trunk/src/exceptionBasic.cc

    r1373 r1374  
    102102        false, 
    103103        false, 
    104         false, 
    105104        false 
    106105}; 
     
    158157        NULL, 
    159158        NULL, 
    160         NULL, 
    161159        NULL 
    162160}; 
     
    214212        NULL, 
    215213        NULL, 
    216         NULL, 
    217214        NULL 
    218215}; 
     
    270267        false, 
    271268        false, 
    272         false, 
    273269        false 
    274270}; 
     
    277273 
    278274void *basic::handles[] = { 
    279         NULL, 
    280275        NULL, 
    281276        NULL, 
  • trunk/src/pcExecutionProcess.cc

    r1373 r1374  
    11/*************************************************************************** 
    2  *            pcJobProcessManager.cc 
     2 *            pcExecutionProcess.cc 
    33 * 
    4  *  Tue Feb 27 2007 
    5  *  Copyright  2007  Ni@m 
     4 *  Wed Oct 08 2009 
     5 *  Copyright  2009  Ni@m 
    66 *  niam.niam@gmail.com 
    77 ****************************************************************************/ 
     
    4141#include <string.h> 
    4242 
    43 #include <libdodo/pcJobManager.h> 
    44  
    4543namespace dodo { 
    4644        namespace pc { 
    47                 namespace job { 
    48                         namespace process { 
     45                namespace execution { 
     46                        /** 
     47                         * @struct __process__ 
     48                         * @brief defines process information 
     49                         */ 
     50                        struct __process__ { 
    4951                                /** 
    50                                  * @struct __process__ 
    51                                  * @brief defines process information 
     52                                 * constuctor 
    5253                                 */ 
    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 
    7170                        }; 
    7271                }; 
     
    7473}; 
    7574 
    76 #include <libdodo/pcJobProcessManager.h> 
     75#include <libdodo/pcExecutionProcess.h> 
    7776#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 
     80using namespace dodo::pc::execution; 
     81 
     82process::process(const process &p) : job(p), 
     83                                                                         handle(new __process__) 
     84{ 
     85        *handle = *p.handle; 
     86} 
     87 
     88//------------------------------------------------------------------- 
     89 
     90process::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 
     107process::process(const dodoString &module, 
     108                                void             *data, 
     109                                void             *toInit) 
     110try     : 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 
     148process::~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                        } 
    116174                } 
    117175 
    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"); 
    140179                        if (deinit != NULL) 
    141180                                deinit(); 
    142181 
    143182#ifndef DL_FAST 
    144                         dlclose((*i)->handle); 
     183                        dlclose(handle->handle); 
    145184#endif 
    146185                } 
    147186#endif 
    148  
    149                 delete *i; 
    150187        } 
    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 
     194bool 
     195process::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 
     213void 
     214process::run() 
     215{ 
     216        if (isRunning()) 
     217                throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_RUN, exception::ERRNO_LIBDODO, PROCESSEX_ISALREADYRUNNING, PCEXECUTIONPROCESSEX_ISALREADYRUNNING_STR, __LINE__, __FILE__); 
    195218 
    196219        pid_t pid = fork(); 
    197220 
    198         if (pid == 0) 
    199                 _exit(func(data)); 
    200         else { 
     221        if (pid == 0) { 
     222                _exit(((routine)handle->func)(handle->data)); 
     223        } else { 
    201224                if (pid == -1) 
    202                         throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, 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__); 
    203226                else 
    204                         process->pid = pid; 
     227                        handle->pid = pid; 
    205228        } 
    206229 
    207         process->isRunning = true; 
    208  
    209         return process->position; 
     230        handle->executed = true; 
     231        handle->joined = false; 
    210232} 
    211233 
     
    213235 
    214236void 
    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         } 
     237process::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; 
    344243} 
    345244 
     
    347246 
    348247int 
    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 { 
     248process::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 
    378256        int status; 
    379257 
    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 
     273process::__module__ 
     274process::module(const dodoString &module, 
    430275                                                  void             *toInit) 
    431276{ 
     
    436281#endif 
    437282        if (handle == NULL) 
    438                 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, MANAGEREX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 
    439  
    440         initModule init = (initModule)dlsym(handle, "initPcJobProcessManagerModule"); 
     283                throw exception::basic(exception::MODULE_PCEXECUTIONPROCESS, PROCESSEX_MODULE, exception::ERRNO_DYNLOAD, 0, dlerror(), __LINE__, __FILE__); 
     284 
     285        initModule init = (initModule)dlsym(handle, "initPcExecutionProcessModule"); 
    441286        if (init == NULL) 
    442                 throw exception::basic(exception::MODULE_PCJOBPROCESSMANAGER, 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__); 
    443288 
    444289        __module__ mod = init(toInit); 
     290 
     291        deinitModule deinit = (deinitModule)dlsym(handle, "deinitPcExecutionProcessModule"); 
     292        if (deinit != NULL) 
     293                deinit(); 
    445294 
    446295#ifndef DL_FAST 
    447296        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__); 
    450298#endif 
    451299 
    452300        return mod; 
    453301} 
    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  
    11/*************************************************************************** 
    2  *            pcJobThreadManager.cc 
     2 *            pcJobThreadThread.cc 
    33 * 
    4  *  Wed Nov 30 2005 
    5  *  Copyright  2005  Ni@m 
     4 *  Wed Oct 08 2009 
     5 *  Copyright  2009  Ni@m 
    66 *  niam.niam@gmail.com 
    77 ****************************************************************************/ 
     
    4040#include <string.h> 
    4141 
    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  
    4842namespace dodo { 
    4943        namespace pc { 
    50                 namespace job { 
    51                         namespace thread { 
     44                namespace execution { 
     45                        /** 
     46                         * @struct __thread__ 
     47                         * @brief defines process information 
     48                         */ 
     49                        struct __thread__ { 
    5250                                /** 
    53                                  * @struct __thread__ 
    54                                  * @brief defines process information 
     51                                 * contructor 
    5552                                 */ 
    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 
    8684                        }; 
    8785                }; 
     
    8987}; 
    9088 
    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 
     95using namespace dodo::pc::execution; 
    10496 
    10597#ifdef PTHREAD_EXT 
     
    109101        __thread__ *ti = (__thread__ *)data; 
    110102 
    111         ti->status = ti->func(ti->data); 
     103        ti->status = ((execution::routine)ti->func)(ti->data); 
    112104 
    113105        return NULL; 
     
    117109//------------------------------------------------------------------- 
    118110 
    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); 
     111thread::thread(const thread &t) : job(t), 
     112                                                                  handle(new __thread__) 
     113{ 
     114        *handle = *t.handle; 
     115} 
     116 
     117//------------------------------------------------------------------- 
     118 
     119thread::thread(routine func, 
     120                           void         *data, 
     121                           short        action, 
     122                           bool detached, 
     123                           unsigned long stackSize) 
     124try     : 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 
     165thread::thread(const dodoString &module, 
     166                                void             *data, 
     167                                void             *toInit) 
     168try : 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 
     220thread::~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); 
    158239#endif 
    159240 
    160241                                break; 
    161242 
    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); 
    166247#endif 
    167248 
    168249                                break; 
    169250 
    170                         case job::ON_DESTRUCTION_WAIT: 
     251                        case ON_DESTRUCTION_WAIT: 
    171252                        default: 
    172253 
    173254#ifdef PTHREAD_EXT 
    174                                 pthread_join((*i)->thread, NULL); 
     255                                pthread_join(handle->thread, NULL); 
    175256#endif 
    176257 
     
    179260 
    180261#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"); 
    183264                        if (deinit != NULL) 
    184265                                deinit(); 
    185266 
    186267#ifndef DL_FAST 
    187                         dlclose((*i)->handle); 
     268                        dlclose(handle->handle); 
    188269#endif 
    189270                } 
    190271#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 
     279void 
     280thread::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 
     296int 
     297thread::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 
     330void 
     331thread::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; 
    219340} 
    220341 
     
    222343 
    223344bool 
    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