2019-01-03 21:53:44 +01:00
|
|
|
/*
|
|
|
|
* IXCancellationRequest.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXCancellationRequest.h"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
namespace ix
|
2019-01-03 21:53:44 +01:00
|
|
|
{
|
|
|
|
CancellationRequest makeCancellationRequestWithTimeout(int secs,
|
|
|
|
std::atomic<bool>& requestInitCancellation)
|
|
|
|
{
|
|
|
|
auto start = std::chrono::system_clock::now();
|
|
|
|
auto timeout = std::chrono::seconds(secs);
|
|
|
|
|
|
|
|
auto isCancellationRequested = [&requestInitCancellation, start, timeout]() -> bool
|
|
|
|
{
|
|
|
|
// Was an explicit cancellation requested ?
|
|
|
|
if (requestInitCancellation) return true;
|
2019-02-21 03:59:07 +01:00
|
|
|
|
2019-01-03 21:53:44 +01:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
if ((now - start) > timeout) return true;
|
|
|
|
|
|
|
|
// No cancellation request
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
return isCancellationRequested;
|
|
|
|
}
|
|
|
|
}
|