Changeset 1415:fb86a04acfb0


Ignore:
Timestamp:
11/21/09 09:55:49 (2 years ago)
Author:
niam
Branch:
default
Message:

pc::execution::manager: non-template implementation, possible to manage different kind of jobs in one instance

Location:
sources
Files:
11 edited
1 moved

Legend:

Unmodified
Added
Removed
  • sources/examples/io_network.pc_thread/test.cc

    r1406 r1415  
    9696        bool exit_condition(false); 
    9797 
    98         execution::manager<execution::thread> manager; 
     98        execution::manager manager; 
    9999 
    100100        ::data.set((void *)&exit_condition); 
     
    108108 
    109109                exchange *ex = new exchange(accepted); 
    110                 execution::thread t(::process, (void *)ex, execution::ON_DESTRUCTION_KEEP_ALIVE); 
    111                 t.run(); 
    112                 manager.add(t); 
     110                manager.run(manager.add(execution::thread(::process, (void *)ex, execution::ON_DESTRUCTION_KEEP_ALIVE))); 
    113111 
    114112                try { 
  • sources/examples/io_pipe.io_file_fifo.pc_thread/test.cc

    r1406 r1415  
    8484{ 
    8585    try { 
    86         manager<thread> threads; 
     86        manager threads; 
    8787 
    8888        cout << "\n~~using one pipe for the thread~~\n"; 
  • sources/examples/pc_job/test.cc

    r1410 r1415  
    3232        cout << endl << ">>" << (char *)data << ": " << tools::time::millinow() << endl; 
    3333 
    34         tools::os::sleep(2); 
     34        tools::os::sleep(tools::string::stringToUL((char *)data)); 
    3535 
    3636        cout << endl << "<<" << (char *)data << ": " << tools::time::millinow() << endl; 
     
    5858        scheduler.schedule(&oneshot1, 3000); 
    5959 
    60         execution::job *jobs[amount]; 
     60        execution::manager manager; 
    6161 
    6262        dodoString ids[amount]; 
     63        int pos[amount]; 
    6364        for (int i = 0; i < amount; ++i) { 
    6465            ids[i] = tools::string::lToString(i); 
    65             if (i % 2 == 0) 
    66                 jobs[i] = new execution::thread(::job1, (void *)ids[i].c_str(), execution::ON_DESTRUCTION_STOP); 
    67             else 
    68                 jobs[i] = new execution::process(::job1, (void *)ids[i].c_str(), execution::ON_DESTRUCTION_STOP); 
     66            if (i % 2 == 0) { 
     67                pos[i] = manager.add(execution::thread(::job1, (void *)ids[i].c_str(), execution::ON_DESTRUCTION_STOP)); 
     68                cout << "Job #"<< pos[i] << " is a thread" << endl; 
     69            } else { 
     70                pos[i] = manager.add(execution::process(::job1, (void *)ids[i].c_str(), execution::ON_DESTRUCTION_STOP)); 
     71                cout << "Job #"<< pos[i] << " is a process" << endl; 
     72            } 
    6973        } 
    7074 
     
    7377 
    7478        for (int i = 0; i < amount; ++i) 
    75             jobs[i]->run(); 
     79            manager.run(pos[i]); 
     80 
     81        for (int i = 0; i < amount; ++i) 
     82            cout << "Job #"<< pos[i] << " is " << (manager.isRunning(pos[i])?"running":"not running") << endl; 
    7683 
    7784        cout << tools::time::millinow() << endl; 
     
    7986        cout << tools::time::millinow() << endl; 
    8087        scheduler.remove(pID); 
     88 
     89        for (int i = 0; i < amount; ++i) 
     90            cout << "Job #"<< pos[i] << " is " << (manager.isRunning(pos[i])?"running":"not running") << endl; 
     91 
    8192        pID = scheduler.schedule(&periodic, 100, true); 
    8293        cout << tools::time::millinow() << endl; 
     
    8899 
    89100        for (int i = 0; i < amount; ++i) 
    90             cout << "status: " << jobs[i]->wait() << endl; 
     101            cout << "Job #"<< pos[i] << " is " << (manager.isRunning(pos[i])?"running":"not running") << endl; 
    91102 
    92103        for (int i = 0; i < amount; ++i) 
    93             delete jobs[i]; 
     104            cout << "status: " << manager.wait(pos[i]) << endl; 
     105 
     106        for (int i = 0; i < amount; ++i) 
     107            cout << "Job #"<< pos[i] << " is " << (manager.isRunning(pos[i])?"running":"not running") << endl; 
    94108    } catch (dodo::exception::basic &ex)   { 
    95109        cout << (dodoString)ex << "\t" << ex.line << "\t" << ex.file << endl; 
  • sources/examples/pc_process/test.cc

    r1406 r1415  
    102102        data0.set((char *)"!test!\n"); 
    103103 
    104         execution::manager<execution::process> manager; 
     104        execution::manager manager; 
    105105 
    106106        const int amount = 10; 
  • sources/examples/pc_thread/test.cc

    r1406 r1415  
    4949        ::data.set((void *)data); 
    5050 
    51         execution::manager<execution::thread> manager; 
     51        execution::manager manager; 
    5252 
    5353        const int amount = 10; 
     
    7070 
    7171        for (int i = 0; i < amount; ++i) { 
    72             dodo::exception::basic *ex = manager.job(pos[i])->exception(); 
     72            dodo::exception::basic *ex = dynamic_cast<execution::thread *>(manager.job(pos[i]))->exception(); 
    7373            if (ex) { 
    7474                cout << "Thread " << i << ":\t" << (dodoString)*ex << "\t" << ex->line << "\t" << ex->file << endl; 
  • sources/include/libdodo/pcExecutionJob.h

    r1406 r1415  
    3838    namespace pc { 
    3939        namespace execution { 
     40            class manager; 
     41 
    4042            /** 
    4143             * @typedef routine 
     
    5961             */ 
    6062            class job { 
     63                friend class manager; 
     64 
    6165              public: 
     66 
     67                enum typeEnum { 
     68                    TYPE_PROCESS, 
     69                    TYPE_THREAD 
     70                }; 
    6271 
    6372                /** 
    6473                 * constructor 
     74                 * @param type defines type of job[@see job::typeEnum] 
    6575                 */ 
    66                 job(); 
     76                job(short type); 
    6777 
    6878                /** 
     
    100110 
    101111                mutable bool cloned; ///< true if object was cloned 
     112 
     113                short type; ///< type of job[@see job::typeEnum] 
    102114            }; 
    103115        }; 
  • sources/include/libdodo/pcExecutionManager.h

    r1410 r1415  
    4242 
    4343        namespace execution { 
     44            class job; 
     45 
    4446            /** 
    4547             * @class manager 
    4648             * @brief provides interface for jobs management 
    4749             */ 
    48             template <typename T> 
    4950            class manager { 
    5051              public: 
     
    6566                 * @param job defines job for managing 
    6667                 */ 
    67                 unsigned long add(const T &job); 
     68                unsigned long add(const job &job); 
    6869 
    6970                /** 
     
    124125                 * @param id defines job identificator 
    125126                 */ 
    126                 T *job(unsigned long id); 
     127                execution::job *job(unsigned long id); 
    127128 
    128129              protected: 
    129130 
    130                 dodoMap<unsigned long, T> handles;  ///< managed jobs 
     131                dodoMap<unsigned long, execution::job *> handles;  ///< managed jobs 
    131132 
    132133                unsigned long counter;              ///< job id counter 
     
    138139}; 
    139140 
    140 #include <libdodo/pcExecutionManager.inline> 
    141  
    142141#endif 
  • sources/include/libdodo/pcExecutionManagerEx.h

    r1406 r1415  
    4343            enum managerExR { 
    4444                MANAGEREX_NOTFOUND, 
     45                MANAGEREX_UNKNOWNJOB, 
    4546            }; 
    4647 
     
    4950             */ 
    5051#define PCEXECUTIONMANAGEREX_NOTFOUND_STR    "Job was not found" 
     52#define PCEXECUTIONMANAGEREX_UNKNOWNJOB_STR  "Unknown type of job" 
    5153            /** 
    5254             * ID of function where exception was thrown 
     
    5860                MANAGEREX_ISRUNNING, 
    5961                MANAGEREX_JOB, 
     62                MANAGEREX_ADD, 
    6063            }; 
    6164        }; 
  • sources/src/pcExecutionJob.cc

    r1406 r1415  
    3434using namespace dodo::pc::execution; 
    3535 
    36 job::job() : cloned(false) 
     36job::job(short type) : cloned(false), 
     37                       type(type) 
    3738{ 
    3839} 
  • sources/src/pcExecutionManager.cc

    r1406 r1415  
    11/*************************************************************************** 
    2  *            pcExecutionManager.inline 
     2 *            pcExecutionManager.cc 
    33 * 
    44 *  Sun Oct  30 2007 
     
    3131 
    3232#include <libdodo/pcExecutionManager.h> 
     33#include <libdodo/pcExecutionJob.h> 
     34#include <libdodo/pcExecutionThread.h> 
     35#include <libdodo/pcExecutionProcess.h> 
    3336#include <libdodo/pcExecutionManagerEx.h> 
    3437#include <libdodo/types.h> 
     
    3639#include <libdodo/pcSyncStack.h> 
    3740 
    38 template <typename T> 
    39 dodo::pc::execution::manager<T>::manager() : counter(0), 
    40                                              keeper(new pc::sync::thread) 
    41 { 
    42 } 
    43  
    44 //------------------------------------------------------------------- 
    45  
    46 template <typename T> 
    47 dodo::pc::execution::manager<T>::~manager() 
    48 { 
     41using namespace dodo::pc::execution; 
     42 
     43manager::manager() : counter(0), 
     44                     keeper(new pc::sync::thread) 
     45{ 
     46} 
     47 
     48//------------------------------------------------------------------- 
     49 
     50manager::~manager() 
     51{ 
     52    dodoMap<unsigned long, execution::job *>::const_iterator i = handles.begin(), j = handles.end(); 
     53 
     54    for (; i != j; ++i) 
     55        delete i->second; 
     56 
    4957    delete keeper; 
    5058} 
     
    5260//------------------------------------------------------------------- 
    5361 
    54 template <typename T> 
    5562unsigned long 
    56 dodo::pc::execution::manager<T>::add(const T &job) 
    57 { 
    58     pc::sync::stack tg(keeper); 
    59  
    60     handles.insert(make_pair(counter, job)); 
     63manager::add(const execution::job &job) 
     64{ 
     65    pc::sync::stack tg(keeper); 
     66 
     67    execution::job *j; 
     68 
     69    execution::job *orig = const_cast<execution::job *>(&job); 
     70 
     71    switch (job.type) { 
     72        case execution::job::TYPE_PROCESS: 
     73            j = new process(*dynamic_cast<process *>(orig)); 
     74 
     75            break; 
     76 
     77        case execution::job::TYPE_THREAD: 
     78            j = new thread(*dynamic_cast<thread *>(orig)); 
     79 
     80            break; 
     81 
     82        default: 
     83            throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_RUN, exception::ERRNO_LIBDODO, MANAGEREX_UNKNOWNJOB, PCEXECUTIONMANAGEREX_UNKNOWNJOB_STR, __LINE__, __FILE__); 
     84    } 
     85 
     86    handles.insert(std::make_pair(counter, j)); 
    6187 
    6288    return counter++; 
     
    6591//------------------------------------------------------------------- 
    6692 
    67 template <typename T> 
    68 void 
    69 dodo::pc::execution::manager<T>::remove(unsigned long id, 
    70                                        bool          terminate) 
    71 { 
    72     pc::sync::stack tg(keeper); 
    73  
    74     typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 
     93void 
     94manager::remove(unsigned long id, 
     95                bool          terminate) 
     96{ 
     97    pc::sync::stack tg(keeper); 
     98 
     99    dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); 
    75100 
    76101    if (job == handles.end()) 
    77102        return; 
    78103 
    79     if (terminate && job->second.isRunning()) 
    80         job->second.stop(); 
     104    if (terminate && job->second->isRunning()) 
     105        job->second->stop(); 
     106 
     107    delete job->second; 
    81108 
    82109    handles.erase(job); 
     
    85112//------------------------------------------------------------------- 
    86113 
    87 template <typename T> 
    88 void 
    89 dodo::pc::execution::manager<T>::run(unsigned long id) 
    90 { 
    91     pc::sync::stack tg(keeper); 
    92  
    93     typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 
     114void 
     115manager::run(unsigned long id) 
     116{ 
     117    pc::sync::stack tg(keeper); 
     118 
     119    dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); 
    94120 
    95121    if (job == handles.end()) 
    96122        throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_RUN, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 
    97123 
    98     job->second.run(); 
    99 } 
    100  
    101 //------------------------------------------------------------------- 
    102  
    103 template <typename T> 
    104 void 
    105 dodo::pc::execution::manager<T>::stop(unsigned long id) 
    106 { 
    107     pc::sync::stack tg(keeper); 
    108  
    109     typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 
     124    job->second->run(); 
     125} 
     126 
     127//------------------------------------------------------------------- 
     128 
     129void 
     130manager::stop(unsigned long id) 
     131{ 
     132    pc::sync::stack tg(keeper); 
     133 
     134    dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); 
    110135 
    111136    if (job == handles.end()) 
    112137        throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_STOP, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 
    113138 
    114     job->second.stop(); 
    115 } 
    116  
    117 //------------------------------------------------------------------- 
    118  
    119 template <typename T> 
    120 void 
    121 dodo::pc::execution::manager<T>::stop() 
    122 { 
    123     pc::sync::stack tg(keeper); 
    124  
    125     typename dodoMap<unsigned long, T>::iterator i = handles.begin(), j = handles.end(); 
    126  
    127     for (; i != j; ++i) 
    128         i->second.stop(); 
    129 } 
    130  
    131 //------------------------------------------------------------------- 
    132  
    133 template <typename T> 
     139    job->second->stop(); 
     140} 
     141 
     142//------------------------------------------------------------------- 
     143 
     144void 
     145manager::stop() 
     146{ 
     147    pc::sync::stack tg(keeper); 
     148 
     149    dodoMap<unsigned long, execution::job *>::iterator i = handles.begin(), j = handles.end(); 
     150 
     151    for (; i != j; ++i) 
     152        i->second->stop(); 
     153} 
     154 
     155//------------------------------------------------------------------- 
     156 
    134157int 
    135 dodo::pc::execution::manager<T>::wait(unsigned long id) 
    136 { 
    137     pc::sync::stack tg(keeper); 
    138  
    139     typename dodoMap<unsigned long, T>::iterator job = handles.find(id); 
     158manager::wait(unsigned long id) 
     159{ 
     160    pc::sync::stack tg(keeper); 
     161 
     162    dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); 
    140163 
    141164    if (job == handles.end()) 
    142165        throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_WAIT, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 
    143166 
    144     return job->second.wait(); 
    145 } 
    146  
    147 //------------------------------------------------------------------- 
    148  
    149 template <typename T> 
    150 void 
    151 dodo::pc::execution::manager<T>::wait() 
    152 { 
    153     pc::sync::stack tg(keeper); 
    154  
    155     typename dodoMap<unsigned long, T>::iterator i = handles.begin(), j = handles.end(); 
    156  
    157     for (; i != j; ++i) 
    158         i->second.wait(); 
    159 } 
    160  
    161 //------------------------------------------------------------------- 
    162  
    163 template <typename T> 
     167    return job->second->wait(); 
     168} 
     169 
     170//------------------------------------------------------------------- 
     171 
     172void 
     173manager::wait() 
     174{ 
     175    pc::sync::stack tg(keeper); 
     176 
     177    dodoMap<unsigned long, execution::job *>::iterator i = handles.begin(), j = handles.end(); 
     178 
     179    for (; i != j; ++i) 
     180        i->second->wait(); 
     181} 
     182 
     183//------------------------------------------------------------------- 
     184 
    164185bool 
    165 dodo::pc::execution::manager<T>::isRunning(unsigned long id) const 
    166 { 
    167     pc::sync::stack tg(keeper); 
    168  
    169     typename dodoMap<unsigned long, T>::const_iterator job = handles.find(id); 
     186manager::isRunning(unsigned long id) const 
     187{ 
     188    pc::sync::stack tg(keeper); 
     189 
     190    dodoMap<unsigned long, execution::job *>::const_iterator job = handles.find(id); 
    170191 
    171192    if (job == handles.end()) 
    172193        throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_ISRUNNING, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 
    173194 
    174     return job->second.isRunning(); 
    175 } 
    176  
    177 //------------------------------------------------------------------- 
    178  
    179 template <typename T> 
     195    return job->second->isRunning(); 
     196} 
     197 
     198//------------------------------------------------------------------- 
     199 
    180200unsigned long 
    181 dodo::pc::execution::manager<T>::running() const 
     201manager::running() const 
    182202{ 
    183203    pc::sync::stack tg(keeper); 
     
    185205    unsigned long jobs; 
    186206 
    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()) 
     207    dodoMap<unsigned long, execution::job *>::const_iterator i = handles.begin(), j = handles.end(); 
     208 
     209    for (; i != j; ++i) 
     210        if (i->second->isRunning()) 
    191211            ++jobs; 
    192212 
     
    196216//------------------------------------------------------------------- 
    197217 
    198 template <typename T> 
    199218dodoList<unsigned long> 
    200 dodo::pc::execution::manager<T>::jobs() 
     219manager::jobs() 
    201220{ 
    202221    pc::sync::stack tg(keeper); 
     
    204223    dodoList<unsigned long> jobs; 
    205224 
    206     typename dodoMap<unsigned long, T>::const_iterator i = handles.begin(), j = handles.end(); 
     225    dodoMap<unsigned long, execution::job *>::const_iterator i = handles.begin(), j = handles.end(); 
    207226 
    208227    for (; i != j; ++i) 
     
    214233//------------------------------------------------------------------- 
    215234 
    216 template <typename T> 
    217 T * 
    218 dodo::pc::execution::manager<T>::job(unsigned long id) 
    219 { 
    220     pc::sync::stack tg(keeper); 
    221  
    222     typename dodoMap<unsigned long, T>::const_iterator job = handles.find(id); 
     235dodo::pc::execution::job * 
     236manager::job(unsigned long id) 
     237{ 
     238    pc::sync::stack tg(keeper); 
     239 
     240    dodoMap<unsigned long, execution::job *>::const_iterator job = handles.find(id); 
    223241 
    224242    if (job == handles.end()) 
    225243        throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_JOB, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); 
    226244 
    227     return (T *)&job->second; 
    228 } 
    229  
    230 //------------------------------------------------------------------- 
     245    return job->second; 
     246} 
     247 
     248//------------------------------------------------------------------- 
  • sources/src/pcExecutionProcess.cc

    r1406 r1415  
    9898process::process(routine func, 
    9999                 void    *data, 
    100                  short   action) : job(), 
     100                 short   action) : job(TYPE_PROCESS), 
    101101                                   handle(new __process__) 
    102102{ 
     
    116116                 void             *data, 
    117117                 void             *toInit) 
    118 try : job(), 
     118try : job(TYPE_PROCESS), 
    119119      handle(NULL) 
    120120    { 
  • sources/src/pcExecutionThread.cc

    r1410 r1415  
    148148               bool          detached, 
    149149               unsigned long stackSize) 
    150 try : job(), 
     150try : job(TYPE_THREAD), 
    151151      handle(NULL) 
    152152    { 
     
    193193               void             *data, 
    194194               void             *toInit) 
    195 try : job(), 
     195try : job(TYPE_THREAD), 
    196196      handle(NULL) 
    197197    { 
Note: See TracChangeset for help on using the changeset viewer.