// Copyright (c) 2011-2016 The Cryptonote developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #pragma once #include #include #include namespace System { template class OperationTimeout { public: OperationTimeout(Dispatcher& dispatcher, T& object, std::chrono::nanoseconds timeout) : object(object), timerContext(dispatcher), timeoutTimer(dispatcher) { timerContext.spawn([this, timeout]() { try { timeoutTimer.sleep(timeout); timerContext.interrupt(); } catch (std::exception&) { } }); } ~OperationTimeout() { timerContext.interrupt(); timerContext.wait(); } private: T& object; ContextGroup timerContext; Timer timeoutTimer; }; }