comparison Core/MultiThreading/ThreadedCommandProcessor.cpp @ 466:9da3596069b8

handling failed commands
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Jul 2013 12:11:45 +0200
parents d665b8fc8560
children 322c1b497036
comparison
equal deleted inserted replaced
465:7a966b440f19 466:9da3596069b8
45 { 45 {
46 std::auto_ptr<IDynamicObject> command(that->queue_.Dequeue(TIMEOUT)); 46 std::auto_ptr<IDynamicObject> command(that->queue_.Dequeue(TIMEOUT));
47 47
48 if (command.get() != NULL) 48 if (command.get() != NULL)
49 { 49 {
50 bool success = false;
51
50 try 52 try
51 { 53 {
52 dynamic_cast<ICommand&>(*command).Execute(); 54 if (that->success_)
55 {
56 // No command has failed so far
57 success = dynamic_cast<ICommand&>(*command).Execute();
58 }
59 else
60 {
61 // A command has already failed. Skip the execution of this command.
62 }
53 } 63 }
54 catch (OrthancException) 64 catch (OrthancException)
55 { 65 {
56 } 66 }
57 67
58 { 68 {
59 boost::mutex::scoped_lock lock(that->mutex_); 69 boost::mutex::scoped_lock lock(that->mutex_);
60 assert(that->remainingCommands_ > 0); 70 assert(that->remainingCommands_ > 0);
61 that->remainingCommands_--; 71 that->remainingCommands_--;
72
73 if (!success)
74 that->success_ = false;
75
62 that->processedCommand_.notify_all(); 76 that->processedCommand_.notify_all();
63 } 77 }
64 } 78 }
65 } 79 }
66 } 80 }
70 { 84 {
71 if (numThreads < 1) 85 if (numThreads < 1)
72 { 86 {
73 throw OrthancException(ErrorCode_ParameterOutOfRange); 87 throw OrthancException(ErrorCode_ParameterOutOfRange);
74 } 88 }
75 89
90 success_ = true;
76 done_ = false; 91 done_ = false;
77 threads_.resize(numThreads); 92 threads_.resize(numThreads);
78 remainingCommands_ = 0; 93 remainingCommands_ = 0;
79 94
80 for (unsigned int i = 0; i < numThreads; i++) 95 for (unsigned int i = 0; i < numThreads; i++)
113 remainingCommands_++; 128 remainingCommands_++;
114 } 129 }
115 } 130 }
116 131
117 132
118 void ThreadedCommandProcessor::Join() 133 bool ThreadedCommandProcessor::Join()
119 { 134 {
120 boost::mutex::scoped_lock lock(mutex_); 135 boost::mutex::scoped_lock lock(mutex_);
121 136
122 while (!remainingCommands_ == 0) 137 while (!remainingCommands_ == 0)
123 { 138 {
124 processedCommand_.wait(lock); 139 processedCommand_.wait(lock);
125 } 140 }
141
142 // Reset the "success" flag for subsequent commands
143 bool hasSucceeded = success_;
144 success_ = true;
145
146 return hasSucceeded;
126 } 147 }
127 } 148 }