From d9261bdb05cd0863a2c3747c812871dbb851646e Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 14 Aug 2019 11:45:34 +0200 Subject: 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. --- matlab/mex/astra_mex_algorithm_c.cpp | 75 +++--------------------------------- 1 file changed, 5 insertions(+), 70 deletions(-) (limited to 'matlab/mex') 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); -- cgit v1.2.3