1/*
2 * IXCancellationRequest.cpp
3 * Author: Benjamin Sergeant
4 * Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
5 */
6
7#include "IXCancellationRequest.h"
8
9#include <cassert>
10#include <chrono>
11
12namespace ix
13{
14 CancellationRequest makeCancellationRequestWithTimeout(
15 int secs, std::atomic<bool>& requestInitCancellation)
16 {
17 assert(secs > 0);
18
19 auto start = std::chrono::system_clock::now();
20 auto timeout = std::chrono::seconds(secs);
21
22 auto isCancellationRequested = [&requestInitCancellation, start, timeout]() -> bool {
23 // Was an explicit cancellation requested ?
24 if (requestInitCancellation) return true;
25
26 auto now = std::chrono::system_clock::now();
27 if ((now - start) > timeout) return true;
28
29 // No cancellation request
30 return false;
31 };
32
33 return isCancellationRequested;
34 }
35} // namespace ix
36