| 1 | /*************************************************************************** |
|---|
| 2 | * pcExecutionManager.cc |
|---|
| 3 | * |
|---|
| 4 | * Sun Oct 30 2007 |
|---|
| 5 | * Copyright 2007 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #include <libdodo/pcExecutionManager.h> |
|---|
| 33 | #include <libdodo/pcExecutionJob.h> |
|---|
| 34 | #include <libdodo/pcExecutionThread.h> |
|---|
| 35 | #include <libdodo/pcExecutionProcess.h> |
|---|
| 36 | #include <libdodo/pcExecutionManagerEx.h> |
|---|
| 37 | #include <libdodo/types.h> |
|---|
| 38 | #include <libdodo/pcSyncThread.h> |
|---|
| 39 | #include <libdodo/pcSyncStack.h> |
|---|
| 40 | |
|---|
| 41 | using namespace dodo::pc::execution; |
|---|
| 42 | |
|---|
| 43 | manager::manager() : counter(0), |
|---|
| 44 | keeper(new pc::sync::thread) |
|---|
| 45 | { |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | //------------------------------------------------------------------- |
|---|
| 49 | |
|---|
| 50 | manager::~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 | |
|---|
| 57 | delete keeper; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | //------------------------------------------------------------------- |
|---|
| 61 | |
|---|
| 62 | unsigned long |
|---|
| 63 | manager::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 | dodo_throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_ADD, exception::ERRNO_LIBDODO, MANAGEREX_UNKNOWNJOB, PCEXECUTIONMANAGEREX_UNKNOWNJOB_STR, __LINE__, __FILE__); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | handles.insert(std::make_pair(counter, j)); |
|---|
| 87 | |
|---|
| 88 | return counter++; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | //------------------------------------------------------------------- |
|---|
| 92 | |
|---|
| 93 | void |
|---|
| 94 | manager::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); |
|---|
| 100 | |
|---|
| 101 | if (job == handles.end()) |
|---|
| 102 | return; |
|---|
| 103 | |
|---|
| 104 | if (terminate && job->second->isRunning()) |
|---|
| 105 | job->second->stop(); |
|---|
| 106 | |
|---|
| 107 | delete job->second; |
|---|
| 108 | |
|---|
| 109 | handles.erase(job); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | //------------------------------------------------------------------- |
|---|
| 113 | |
|---|
| 114 | void |
|---|
| 115 | manager::run(unsigned long id) |
|---|
| 116 | { |
|---|
| 117 | pc::sync::stack tg(keeper); |
|---|
| 118 | |
|---|
| 119 | dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); |
|---|
| 120 | |
|---|
| 121 | if (job == handles.end()) |
|---|
| 122 | dodo_throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_RUN, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); |
|---|
| 123 | |
|---|
| 124 | job->second->run(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | //------------------------------------------------------------------- |
|---|
| 128 | |
|---|
| 129 | void |
|---|
| 130 | manager::stop(unsigned long id) |
|---|
| 131 | { |
|---|
| 132 | pc::sync::stack tg(keeper); |
|---|
| 133 | |
|---|
| 134 | dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); |
|---|
| 135 | |
|---|
| 136 | if (job == handles.end()) |
|---|
| 137 | dodo_throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_STOP, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); |
|---|
| 138 | |
|---|
| 139 | job->second->stop(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | //------------------------------------------------------------------- |
|---|
| 143 | |
|---|
| 144 | void |
|---|
| 145 | manager::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 | |
|---|
| 157 | int |
|---|
| 158 | manager::wait(unsigned long id) |
|---|
| 159 | { |
|---|
| 160 | pc::sync::stack tg(keeper); |
|---|
| 161 | |
|---|
| 162 | dodoMap<unsigned long, execution::job *>::iterator job = handles.find(id); |
|---|
| 163 | |
|---|
| 164 | if (job == handles.end()) |
|---|
| 165 | dodo_throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_WAIT, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); |
|---|
| 166 | |
|---|
| 167 | return job->second->wait(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | //------------------------------------------------------------------- |
|---|
| 171 | |
|---|
| 172 | void |
|---|
| 173 | manager::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 | |
|---|
| 185 | bool |
|---|
| 186 | manager::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); |
|---|
| 191 | |
|---|
| 192 | if (job == handles.end()) |
|---|
| 193 | dodo_throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_ISRUNNING, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); |
|---|
| 194 | |
|---|
| 195 | return job->second->isRunning(); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | //------------------------------------------------------------------- |
|---|
| 199 | |
|---|
| 200 | unsigned long |
|---|
| 201 | manager::running() const |
|---|
| 202 | { |
|---|
| 203 | pc::sync::stack tg(keeper); |
|---|
| 204 | |
|---|
| 205 | unsigned long jobs; |
|---|
| 206 | |
|---|
| 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()) |
|---|
| 211 | ++jobs; |
|---|
| 212 | |
|---|
| 213 | return jobs; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | //------------------------------------------------------------------- |
|---|
| 217 | |
|---|
| 218 | dodo::slList<unsigned long> |
|---|
| 219 | manager::jobs() |
|---|
| 220 | { |
|---|
| 221 | pc::sync::stack tg(keeper); |
|---|
| 222 | |
|---|
| 223 | dodo::slList<unsigned long> jobs; |
|---|
| 224 | |
|---|
| 225 | dodoMap<unsigned long, execution::job *>::const_iterator i = handles.begin(), j = handles.end(); |
|---|
| 226 | |
|---|
| 227 | for (; i != j; ++i) |
|---|
| 228 | jobs.push(i->first); |
|---|
| 229 | |
|---|
| 230 | return jobs; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | //------------------------------------------------------------------- |
|---|
| 234 | |
|---|
| 235 | dodo::pc::execution::job * |
|---|
| 236 | manager::job(unsigned long id) |
|---|
| 237 | { |
|---|
| 238 | pc::sync::stack tg(keeper); |
|---|
| 239 | |
|---|
| 240 | dodoMap<unsigned long, execution::job *>::const_iterator job = handles.find(id); |
|---|
| 241 | |
|---|
| 242 | if (job == handles.end()) |
|---|
| 243 | dodo_throw exception::basic(exception::MODULE_PCEXECUTIONMANAGER, MANAGEREX_JOB, exception::ERRNO_LIBDODO, MANAGEREX_NOTFOUND, PCEXECUTIONMANAGEREX_NOTFOUND_STR, __LINE__, __FILE__); |
|---|
| 244 | |
|---|
| 245 | return job->second; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | //------------------------------------------------------------------- |
|---|