actually cancel timed out task.
This commit is contained in:
parent
5c1ebe9639
commit
72d8a7baba
@ -4,6 +4,8 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
#undef HAVE_CONFIG_H // this disables the php allocator
|
#undef HAVE_CONFIG_H // this disables the php allocator
|
||||||
#include "../mediawiki-php-wikidiff2/src/lib/Wikidiff2.h"
|
#include "../mediawiki-php-wikidiff2/src/lib/Wikidiff2.h"
|
||||||
@ -118,11 +120,18 @@ static Wikidiff2::String wikidiff2_inline_json_diff(pywikidiff2Obj *self, char*
|
|||||||
self->config.cancelFlag = &cancelFlag;
|
self->config.cancelFlag = &cancelFlag;
|
||||||
self->timed_out = false;
|
self->timed_out = false;
|
||||||
|
|
||||||
|
std::mutex timerMutex;
|
||||||
|
std::condition_variable timerCv;
|
||||||
|
bool diffDone = false;
|
||||||
|
|
||||||
std::thread timerThread;
|
std::thread timerThread;
|
||||||
if (timeout_ms > 0) {
|
if (timeout_ms > 0) {
|
||||||
timerThread = std::thread([&cancelFlag, timeout_ms]() {
|
timerThread = std::thread([&cancelFlag, &timerMutex, &timerCv, &diffDone, timeout_ms]() {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms));
|
std::unique_lock<std::mutex> lock(timerMutex);
|
||||||
cancelFlag.store(true, std::memory_order_relaxed);
|
if (!timerCv.wait_for(lock, std::chrono::milliseconds(timeout_ms), [&diffDone]{ return diffDone; })) {
|
||||||
|
// Timeout expired before diff finished
|
||||||
|
cancelFlag.store(true, std::memory_order_relaxed);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,8 +149,13 @@ static Wikidiff2::String wikidiff2_inline_json_diff(pywikidiff2Obj *self, char*
|
|||||||
// Check if we timed out before stopping the timer thread
|
// Check if we timed out before stopping the timer thread
|
||||||
self->timed_out = cancelFlag.load(std::memory_order_relaxed);
|
self->timed_out = cancelFlag.load(std::memory_order_relaxed);
|
||||||
|
|
||||||
|
// Signal timer thread to exit and wait for it
|
||||||
if (timerThread.joinable()) {
|
if (timerThread.joinable()) {
|
||||||
cancelFlag.store(true, std::memory_order_relaxed); // Stop timer if still waiting
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(timerMutex);
|
||||||
|
diffDone = true;
|
||||||
|
}
|
||||||
|
timerCv.notify_one();
|
||||||
timerThread.join();
|
timerThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user