diff options
author | Willem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl> | 2019-08-14 11:45:34 +0200 |
---|---|---|
committer | Willem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl> | 2019-08-14 11:45:34 +0200 |
commit | d9261bdb05cd0863a2c3747c812871dbb851646e (patch) | |
tree | d1a81110e3fe213747a58f236e021a085417f663 /matlab/mex | |
parent | 9a58b7451179ed512f975bc4c90fb71f172250b9 (diff) | |
download | astra-d9261bdb05cd0863a2c3747c812871dbb851646e.tar.gz astra-d9261bdb05cd0863a2c3747c812871dbb851646e.tar.bz2 astra-d9261bdb05cd0863a2c3747c812871dbb851646e.tar.xz astra-d9261bdb05cd0863a2c3747c812871dbb851646e.zip |
Replace signal-based abort handling by query-based handling
The abort handling is currently only used to process Ctrl-C from Matlab.
Since Matlab R2019a, it appears that calling utIsInterruptPending() from
a thread other than the main thread will crash. The previous approach of
checking utIsInterruptPending() in a thread, and then signalling the running
algorithm was therefore broken.
Diffstat (limited to 'matlab/mex')
-rw-r--r-- | matlab/mex/astra_mex_algorithm_c.cpp | 75 |
1 files changed, 5 insertions, 70 deletions
diff --git a/matlab/mex/astra_mex_algorithm_c.cpp b/matlab/mex/astra_mex_algorithm_c.cpp index 7804eeb..69ebd45 100644 --- a/matlab/mex/astra_mex_algorithm_c.cpp +++ b/matlab/mex/astra_mex_algorithm_c.cpp @@ -105,52 +105,9 @@ void astra_mex_algorithm_create(int nlhs, mxArray* plhs[], int nrhs, const mxArr } #ifdef USE_MATLAB_UNDOCUMENTED - -#ifndef USE_PTHREADS_CTRLC - -// boost version -void waitForInterrupt_boost(CAlgorithm* _pAlg) -{ - boost::posix_time::milliseconds rel(2000); - - while (!utIsInterruptPending()) { - - // This is an interruption point. If the main thread calls - // interrupt(), this thread will terminate here. - boost::this_thread::sleep(rel); - } - - //mexPrintf("Aborting. Please wait.\n"); - - // One last quick check to see if the algorithm already finished - boost::this_thread::interruption_point(); - - _pAlg->signalAbort(); -} - -#else - -// pthreads version -void *waitForInterrupt_pthreads(void *threadid) -{ - CAlgorithm* _pAlg = (CAlgorithm*)threadid; - - while (!utIsInterruptPending()) { - usleep(50000); - pthread_testcancel(); - } - - //mexPrintf("Aborting. Please wait.\n"); - - // One last quick check to see if the algorithm already finished - pthread_testcancel(); - - _pAlg->signalAbort(); - - return 0; +bool checkMatlabInterrupt() { + return utIsInterruptPending(); } - -#endif #endif //----------------------------------------------------------------------------------------- @@ -185,34 +142,12 @@ void astra_mex_algorithm_run(int nlhs, mxArray* plhs[], int nrhs, const mxArray* } // step3: perform actions -#ifndef USE_MATLAB_UNDOCUMENTED - - pAlg->run(iIterations); - -#elif defined(USE_PTHREADS_CTRLC) - - // Start a new thread to watch if the user pressed Ctrl-C - pthread_t thread; - pthread_create(&thread, 0, waitForInterrupt_pthreads, (void*)pAlg); - - pAlg->run(iIterations); - - // kill the watcher thread in case it's still running - pthread_cancel(thread); - pthread_join(thread, 0); -#else - - // Start a new thread to watch if the user pressed Ctrl-C - boost::thread interruptThread(waitForInterrupt_boost, pAlg); +#ifdef USE_MATLAB_UNDOCUMENTED + setShouldAbortHook(&checkMatlabInterrupt); +#endif pAlg->run(iIterations); - - // kill the watcher thread in case it's still running - interruptThread.interrupt(); - interruptThread.join(); - -#endif } //----------------------------------------------------------------------------------------- /** astra_mex_algorithm('get_res_norm', id); |