source: src/Actions/ActionQueue.cpp@ 7e6c0d

Last change on this file since 7e6c0d was 7e6c0d, checked in by Frederik Heber <heber@…>, 11 years ago

Merge branch 'ThreadFixes' into Candidate_v1.4.10

Conflicts:

src/Actions/ActionQueue.cpp
src/Actions/ActionQueue.hpp

  • many conflicts due to last commit in ThreadFixes. Combined stuff from ThreadFixes and preventing failing actions from clearing queue entirely.
  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[628577]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2013 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder 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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * ActionQueue.cpp
25 *
26 * Created on: Aug 16, 2013
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
[1d3563]37#include "Actions/ActionQueue.hpp"
[628577]38
[690741]39#include "CodePatterns/Assert.hpp"
40#include "CodePatterns/IteratorAdaptors.hpp"
[46b181]41#include "CodePatterns/Log.hpp"
[628577]42#include "CodePatterns/Singleton_impl.hpp"
43
[415ddd]44#include <boost/date_time/posix_time/posix_time.hpp>
45#include <boost/version.hpp>
[601ef8]46#include <iterator>
[46b181]47#include <string>
48#include <sstream>
[690741]49#include <vector>
50
[0d4168]51#include "Actions/ActionExceptions.hpp"
[6367dd]52#include "Actions/ActionHistory.hpp"
[ed3944]53#include "Actions/ActionRegistry.hpp"
[0d4168]54#include "World.hpp"
[ed3944]55
[628577]56using namespace MoleCuilder;
57
[29b52b]58const Action* ActionQueue::_lastchangedaction = NULL;
59
[ed3944]60ActionQueue::ActionQueue() :
[29b52b]61 Observable("ActionQueue"),
[6367dd]62 AR(new ActionRegistry()),
[af5384]63 history(new ActionHistory),
[74459a]64#ifndef HAVE_ACTION_THREAD
[a61dbb]65 lastActionOk(true)
[74459a]66#else
[a61dbb]67 lastActionOk(true),
[7e6c0d]68 CurrentAction(0),
[23b6cf]69 run_thread_isIdle(true),
[9a4949]70 run_thread_running(false),
[23b6cf]71 run_thread(boost::bind(&ActionQueue::run, this))
[74459a]72#endif
[29b52b]73{
74 // channels of observable
75 Channels *OurChannel = new Channels;
76 NotificationChannels.insert( std::make_pair(static_cast<Observable *>(this), OurChannel) );
77 // add instance for each notification type
78 for (size_t type = 0; type < NotificationType_MAX; ++type)
79 OurChannel->addChannel(type);
80}
[628577]81
82ActionQueue::~ActionQueue()
[ed3944]83{
[74459a]84#ifdef HAVE_ACTION_THREAD
[415ddd]85 stop();
[601ef8]86
87 clearTempQueue();
[74459a]88#endif
[415ddd]89
[7f1a1a]90 clearQueue();
[af5384]91
[6367dd]92 delete history;
[ed3944]93 delete AR;
94}
[628577]95
[f54cda]96void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state)
[05c989]97{
[7f1a1a]98 const Action * const registryaction = AR->getActionByName(name);
99 queueAction(registryaction, state);
[f54cda]100}
101
[7f1a1a]102void ActionQueue::queueAction(const Action * const _action, enum Action::QueryOptions state)
[f54cda]103{
[af5384]104 Action *newaction = _action->clone(state);
105 newaction->prepare(state);
[74459a]106#ifdef HAVE_ACTION_THREAD
[fff8fc]107 mtx_actionqueue.lock();
[74459a]108#endif
[7fc447]109 actionqueue.push_back( newaction );
[74459a]110#ifndef HAVE_ACTION_THREAD
111 try {
112 newaction->call();
[a61dbb]113 lastActionOk = true;
[74459a]114 } catch(ActionFailureException &e) {
115 std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
116 World::getInstance().setExitFlag(5);
[7e6c0d]117// clearQueue(actionqueue.size()-1);
[a61dbb]118 lastActionOk = false;
[7e6c0d]119// std::cerr << "Remaining Actions cleared from queue." << std::endl;
[11d433]120 } catch (std::exception &e) {
121 pushStatus("FAIL: General exception caught, aborting.");
122 World::getInstance().setExitFlag(134);
[7e6c0d]123// clearQueue(actionqueue.size()-1);
[11d433]124 lastActionOk = false;
[7e6c0d]125// std::cerr << "Remaining Actions cleared from queue." << std::endl;
[74459a]126 }
[cfb9c5]127 if (lastActionOk) {
128 OBSERVE;
129 NOTIFY(ActionQueued);
130 _lastchangedaction = newaction;
131 }
[74459a]132#else
[fff8fc]133 mtx_actionqueue.unlock();
[7e6c0d]134 const bool new_run_thread_isIdle = isActionQueueDone();
135 setrun_thread_isIdle(new_run_thread_isIdle);
[74459a]136#endif
[05c989]137}
138
[7e6c0d]139#ifdef HAVE_ACTION_THREAD
[fff8fc]140bool ActionQueue::isActionQueueDone() const
141{
142 boost::lock_guard<boost::mutex> lock(mtx_actionqueue);
143 return (CurrentAction == actionqueue.size());
144}
145
146bool ActionQueue::isTempQueueDone() const
147{
148 boost::lock_guard<boost::mutex> lock(mtx_tempqueue);
149 return tempqueue.empty();
150}
[7e6c0d]151#endif
[fff8fc]152
[975b83]153void ActionQueue::insertAction(Action *_action, enum Action::QueryOptions state)
154{
[74459a]155#ifndef HAVE_ACTION_THREAD
156 queueAction(_action, state);
157#else
[415ddd]158 Action *newaction = _action->clone(state);
159 newaction->prepare(state);
160 {
[fff8fc]161 boost::lock_guard<boost::mutex> lock(mtx_tempqueue);
162 tempqueue.push_back( newaction );
163 }
164 {
165 bool new_run_thread_isIdle = getrun_thread_isIdle();
166 new_run_thread_isIdle &= isTempQueueDone();
167 setrun_thread_isIdle(new_run_thread_isIdle);
[415ddd]168 }
[74459a]169#endif
[415ddd]170}
171
[74459a]172#ifdef HAVE_ACTION_THREAD
[415ddd]173void ActionQueue::run()
174{
[9a4949]175 {
176 boost::lock_guard<boost::mutex> lock(mtx_run_thread_isIdle);
177 run_thread_running = true;
178 }
[415ddd]179 bool Interrupted = false;
180 do {
181 // sleep for some time and wait for queue to fill up again
182 try {
183#if BOOST_VERSION < 105000
[23b6cf]184 boost::this_thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(100));
[415ddd]185#else
[d93b4b3]186 boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
[415ddd]187#endif
188 } catch(boost::thread_interrupted &e) {
189 LOG(2, "INFO: ActionQueue has received stop signal.");
190 Interrupted = true;
191 }
192// LOG(1, "DEBUG: Start of ActionQueue's run() loop.");
193 // call all currently present Actions
194 insertTempQueue();
[fff8fc]195 while ((!Interrupted) && (!isActionQueueDone())) {
[415ddd]196 // boost::this_thread::disable_interruption di;
[23b6cf]197 // access actionqueue, hence using mutex
[fff8fc]198 mtx_actionqueue.lock();
[415ddd]199 LOG(0, "Calling Action " << actionqueue[CurrentAction]->getName() << " ... ");
200 try {
201 actionqueue[CurrentAction]->call();
[0b6b77]202 pushStatus("SUCCESS: Action "+actionqueue[CurrentAction]->getName()+" successful.");
[a61dbb]203 lastActionOk = true;
[415ddd]204 } catch(ActionFailureException &e) {
[0b6b77]205 pushStatus("FAIL: Action "+*boost::get_error_info<ActionNameString>(e)+" has failed.");
[415ddd]206 World::getInstance().setExitFlag(5);
[601ef8]207 clearQueue(CurrentAction);
208 clearTempQueue();
[a61dbb]209 lastActionOk = false;
[601ef8]210 std::cerr << "Remaining Actions cleared from queue." << std::endl;
[11d433]211 } catch (std::exception &e) {
212 pushStatus("FAIL: General exception caught, aborting.");
213 World::getInstance().setExitFlag(134);
[601ef8]214 clearQueue(CurrentAction);
215 clearTempQueue();
216 std::cerr << "Remaining Actions cleared from queue." << std::endl;
[415ddd]217 }
[23b6cf]218 // remember action we juse executed
219 const Action *lastaction = actionqueue[CurrentAction];
[415ddd]220 // step on to next action and check for end
221 CurrentAction++;
222 // insert new actions (before [CurrentAction]) if they have been spawned
223 // we must have an extra vector for this, as we cannot change actionqueue
224 // while an action instance is "in-use"
[fff8fc]225 mtx_actionqueue.unlock();
226
[415ddd]227 insertTempQueue();
[fff8fc]228
[23b6cf]229 // set last action
230 if (lastActionOk) {
231 OBSERVE;
232 NOTIFY(ActionQueued);
233 _lastchangedaction = lastaction;
234 }
[415ddd]235 }
236 {
[fff8fc]237 bool new_run_thread_isIdle = isActionQueueDone();
238 new_run_thread_isIdle &= isTempQueueDone();
239 setrun_thread_isIdle(new_run_thread_isIdle);
[415ddd]240 }
241 cond_idle.notify_one();
242// LOG(1, "DEBUG: End of ActionQueue's run() loop.");
243 } while (!Interrupted);
[9a4949]244 {
245 boost::lock_guard<boost::mutex> lock(mtx_run_thread_isIdle);
246 run_thread_running = false;
247 }
[415ddd]248}
249
250void ActionQueue::insertTempQueue()
251{
[fff8fc]252 boost::lock_guard<boost::mutex> lock(mtx_tempqueue);
[415ddd]253 if (!tempqueue.empty()) {
[fff8fc]254 boost::lock_guard<boost::mutex> lock(mtx_actionqueue);
[415ddd]255 ActionQueue_t::iterator InsertionIter = actionqueue.begin();
256 std::advance(InsertionIter, CurrentAction);
257 actionqueue.insert( InsertionIter, tempqueue.begin(), tempqueue.end() );
258 tempqueue.clear();
259 }
260}
261
[fff8fc]262void ActionQueue::setrun_thread_isIdle(
263 const bool _run_thread_isIdle)
[415ddd]264{
[23b6cf]265 boost::unique_lock<boost::mutex> lock(mtx_run_thread_isIdle);
[fff8fc]266 run_thread_isIdle = _run_thread_isIdle;
267}
268
269bool ActionQueue::getrun_thread_isIdle() const
270{
271 boost::unique_lock<boost::mutex> lock(mtx_run_thread_isIdle);
272 return run_thread_isIdle;
273}
274
[415ddd]275void ActionQueue::wait()
276{
[fff8fc]277 if (run_thread_running) {
278 boost::unique_lock<boost::mutex> lock(mtx_run_thread_isIdle);
[9a4949]279 while(!run_thread_isIdle)
280 {
281 cond_idle.wait(lock);
282 }
[415ddd]283 }
284}
285
286void ActionQueue::stop()
287{
288 // notify actionqueue thread that we wish to terminate
289 run_thread.interrupt();
290 // wait till it ends
291 run_thread.join();
[975b83]292}
[74459a]293#endif
[975b83]294
[a6ceab]295Action* ActionQueue::getActionByName(const std::string &name)
[1d3563]296{
[ed3944]297 return AR->getActionByName(name);
[1d3563]298}
299
[a6ceab]300bool ActionQueue::isActionKnownByName(const std::string &name) const
[1d3563]301{
[ed3944]302 return AR->isActionPresentByName(name);
[1d3563]303}
304
[126867]305void ActionQueue::registerAction(Action *_action)
306{
307 AR->registerInstance(_action);
308}
309
[46b181]310void ActionQueue::outputAsCLI(std::ostream &output) const
311{
[7fc447]312 for (ActionQueue_t::const_iterator iter = actionqueue.begin();
313 iter != actionqueue.end();
[46b181]314 ++iter) {
[bad589]315 // skip store-session in printed list
[12d946]316 if ( ((*iter)->getName() != std::string("store-session"))
317 && ((*iter)->getName() != std::string("load-session"))) {
[7fc447]318 if (iter != actionqueue.begin())
[bad589]319 output << " ";
320 (*iter)->outputAsCLI(output);
321 }
[46b181]322 }
323 output << std::endl;
324}
325
[477012]326void ActionQueue::outputAsPython(std::ostream &output) const
327{
328 const std::string prefix("pyMoleCuilder");
329 output << "import " << prefix << std::endl;
[9e4655]330 output << "# ========================== Stored Session BEGIN ==========================" << std::endl;
[7fc447]331 for (ActionQueue_t::const_iterator iter = actionqueue.begin();
332 iter != actionqueue.end();
[477012]333 ++iter) {
334 // skip store-session in printed list
[12d946]335 if ( ((*iter)->getName() != std::string("store-session"))
336 && ((*iter)->getName() != std::string("load-session")))
[477012]337 (*iter)->outputAsPython(output, prefix);
338 }
[9e4655]339 output << "# =========================== Stored Session END ===========================" << std::endl;
[477012]340}
341
[a6ceab]342const ActionTrait& ActionQueue::getActionsTrait(const std::string &name) const
[690741]343{
344 // this const_cast is just required as long as we have a non-const getActionByName
345 const Action * const action = const_cast<ActionQueue *>(this)->getActionByName(name);
346 return action->Traits;
347}
348
[6367dd]349void ActionQueue::addElement(Action* _Action,ActionState::ptr _state)
350{
351 history->addElement(_Action, _state);
352}
353
354void ActionQueue::clear()
355{
356 history->clear();
357}
358
[601ef8]359void ActionQueue::clearQueue(const size_t _fromAction)
[7f1a1a]360{
361 // free all actions contained in actionqueue
[fff8fc]362 {
[601ef8]363#ifdef HAVE_ACTION_THREAD
[fff8fc]364 boost::lock_guard<boost::mutex> lock(mtx_actionqueue);
[601ef8]365#endif
[7e6c0d]366 LOG(1, "Removing all Actions from position " << _fromAction << " onward.");
367 // free all actions still to be called contained in actionqueue
368 ActionQueue_t::iterator inititer = actionqueue.begin();
369 std::advance(inititer, _fromAction);
370 for (ActionQueue_t::iterator iter = inititer; iter != actionqueue.end(); ++iter)
[fff8fc]371 delete *iter;
[7e6c0d]372 actionqueue.erase(inititer, actionqueue.end());
373 LOG(1, "There are " << actionqueue.size() << " remaining Actions.");
[601ef8]374#ifdef HAVE_ACTION_THREAD
[7e6c0d]375 CurrentAction = actionqueue.size();
[601ef8]376#endif
[7f1a1a]377 }
[601ef8]378}
379
380#ifdef HAVE_ACTION_THREAD
381void ActionQueue::clearTempQueue()
382{
[7f1a1a]383 // free all actions contained in tempqueue
[06b5df]384 {
[fff8fc]385 boost::lock_guard<boost::mutex> lock(mtx_tempqueue);
386 for (ActionQueue_t::iterator iter = tempqueue.begin();
387 !tempqueue.empty(); iter = tempqueue.begin()) {
388 delete *iter;
389 tempqueue.erase(iter);
390 }
[06b5df]391 }
[7f1a1a]392}
[601ef8]393#endif
[6367dd]394
[690741]395const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
396{
397 ActionTokens_t returnlist;
398
399 returnlist.insert(
400 returnlist.end(),
[ed3944]401 MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getBeginIter()),
402 MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getEndIter()));
[690741]403
404 return returnlist;
405}
406
[6367dd]407void ActionQueue::undoLast()
408{
409 history->undoLast();
410}
411
[c01fec]412bool ActionQueue::canUndo() const
413{
414 return history->hasUndo();
415}
416
[6367dd]417void ActionQueue::redoLast()
418{
419 history->redoLast();
420}
421
[c01fec]422bool ActionQueue::canRedo() const
423{
424 return history->hasRedo();
425}
426
[6367dd]427
[628577]428CONSTRUCT_SINGLETON(ActionQueue)
Note: See TracBrowser for help on using the repository browser.