1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "AlternateThreadPT.hxx"
26 #include "YacsTrace.hxx"
29 using namespace YACS::BASES;
31 AlternateThreadPT::AlternateThreadPT()
32 : _threadStatus(UNEXISTING)
34 YASSERT(pthread_cond_init(&_pingPongCond, NULL) == 0)
35 YASSERT(pthread_mutex_init(&_pingPongMutex, NULL) == 0)
38 AlternateThreadPT::~AlternateThreadPT()
41 terminateSlaveThread();
42 YASSERT(pthread_mutex_destroy(&_pingPongMutex) == 0)
43 YASSERT(pthread_cond_destroy(&_pingPongCond) == 0)
44 } catch (const exception & e) {
45 cerr << "Exception happened in AlternateThreadPT destructor: " << e.what() << endl;
47 cerr << "Unknown exception happened in AlternateThreadPT destructor." << endl;
51 void AlternateThreadPT::start()
53 // This method must not be called if a slave thread is running
54 YASSERT(_threadStatus == UNEXISTING)
56 YASSERT(pthread_mutex_lock(&_pingPongMutex) == 0)
57 DEBTRACE("Starting thread")
58 YASSERT(pthread_create(&_threadId, 0, runThread, this) == 0)
59 DEBTRACE("Master waiting for slave")
60 YASSERT(pthread_cond_wait(&_pingPongCond, &_pingPongMutex) == 0)
61 DEBTRACE("Master running again")
64 void AlternateThreadPT::terminateSlaveThread()
66 // This method must not be called by the slave thread
67 YASSERT(_threadStatus == UNEXISTING || !pthread_equal(pthread_self(), _threadId))
69 switch (_threadStatus) {
76 // First try to signal the slave thread to end properly
77 DEBTRACE("Master is trying to terminate slave by signaling error")
78 _threadStatus = TERMINATION_REQUESTED;
81 if (_threadStatus != READY_TO_JOIN) {
82 // Try to cancel the thread
83 cerr << "Warning: Slave thread in AlternateThread did not end properly. "
84 "Thread will be canceled." << endl;
85 YASSERT(pthread_cancel(_threadId) == 0)
86 YASSERT(pthread_cond_wait(&_pingPongCond, &_pingPongMutex) == 0)
88 if (_threadStatus != READY_TO_JOIN) {
89 // If cancel failed, we can do nothing more, throw an exception
99 // Finally join the thread
100 YASSERT(pthread_mutex_unlock(&_pingPongMutex) == 0)
101 YASSERT(pthread_join(_threadId, NULL) == 0)
102 _threadStatus = UNEXISTING;
103 DEBTRACE("AlternateThread terminated")
106 void AlternateThreadPT::signalSlaveAndWait()
108 YASSERT(!pthread_equal(pthread_self(), _threadId))
109 DEBTRACE("Master signaling slave and waiting");
111 DEBTRACE("Master running again");
114 void AlternateThreadPT::signalMasterAndWait()
116 YASSERT(pthread_equal(pthread_self(), _threadId))
117 DEBTRACE("Slave signaling master and waiting");
119 DEBTRACE("Slave running again");
122 void AlternateThreadPT::signalAndWait()
124 YASSERT(_threadStatus == NORMAL_CYCLE || _threadStatus == TERMINATION_REQUESTED)
125 YASSERT(pthread_cond_signal(&_pingPongCond) == 0)
126 YASSERT(pthread_cond_wait(&_pingPongCond, &_pingPongMutex) == 0)
129 bool AlternateThreadPT::isTerminationRequested() const
131 return (_threadStatus == TERMINATION_REQUESTED);
134 AlternateThreadPT::ThreadStatus AlternateThreadPT::getThreadStatus() const
136 return _threadStatus;
139 void * AlternateThreadPT::runThread(void * instance)
142 AlternateThreadPT * instanceCst = (AlternateThreadPT *)instance;
143 YASSERT(pthread_mutex_lock(&instanceCst->_pingPongMutex) == 0)
144 DEBTRACE("Slave thread is now running")
145 instanceCst->_threadStatus = NORMAL_CYCLE;
146 pthread_cleanup_push(threadCleanupFct, instance);
148 pthread_cleanup_pop(1);
149 } catch (const exception & e) {
150 cerr << "Unrecoverable error: an exception was caught in AlternateThread "
151 "(exceptions should normally be caught before getting here). "
152 "Exception type is: " << typeid(e).name() << ", message is: " <<
154 threadCleanupFct(instance);
156 // We can't catch (...) here because it causes problems with thread cancellation, at least
157 // with gcc 4.1.2 and older. With newer versions it should be possible to do something like
158 // catch (abi::__forced_unwind e) { throw; }. See
159 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28145 for details.
160 // The problem is that no exception should get out of the thread scope without being caught
161 // (otherwise the program aborts). So for now the user has to take care of catching all
162 // exceptions, but he cannot use catch (...).
166 void AlternateThreadPT::threadCleanupFct(void * instance)
168 // Beware of not throwing exceptions in this method
169 DEBTRACE("Cleaning up slave thread")
170 AlternateThreadPT * instanceCst = (AlternateThreadPT *)instance;
171 instanceCst->_threadStatus = READY_TO_JOIN;
172 pthread_cond_signal(&instanceCst->_pingPongCond);
173 pthread_mutex_unlock(&instanceCst->_pingPongMutex);