// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers // // This file is part of Bytecoin. // // Bytecoin is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Bytecoin is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with Bytecoin. If not, see . #include "Timer.h" #include #include #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include #include #include "Dispatcher.h" namespace System { namespace { struct TimerContext { uint64_t time; void* context; bool interrupted; }; } Timer::Timer() : dispatcher(nullptr) { } Timer::Timer(Dispatcher& dispatcher) : dispatcher(&dispatcher), stopped(false), context(nullptr) { } Timer::Timer(Timer&& other) : dispatcher(other.dispatcher) { if (dispatcher != nullptr) { assert(other.context == nullptr); stopped = other.stopped; context = nullptr; other.dispatcher = nullptr; } } Timer::~Timer() { assert(dispatcher == nullptr || context == nullptr); } Timer& Timer::operator=(Timer&& other) { assert(dispatcher == nullptr || context == nullptr); dispatcher = other.dispatcher; if (dispatcher != nullptr) { assert(other.context == nullptr); stopped = other.stopped; context = nullptr; other.dispatcher = nullptr; } return *this; } void Timer::start() { assert(dispatcher != nullptr); assert(stopped); stopped = false; } void Timer::stop() { assert(dispatcher != nullptr); assert(!stopped); if (context != nullptr) { TimerContext* timerContext = static_cast(context); if (!timerContext->interrupted) { dispatcher->interruptTimer(timerContext->time, timerContext->context); timerContext->interrupted = true; } } stopped = true; } void Timer::sleep(std::chrono::nanoseconds duration) { assert(dispatcher != nullptr); assert(context == nullptr); if (stopped) { throw InterruptedException(); } LARGE_INTEGER frequency; LARGE_INTEGER ticks; QueryPerformanceCounter(&ticks); QueryPerformanceFrequency(&frequency); uint64_t currentTime = ticks.QuadPart / (frequency.QuadPart / 1000); uint64_t time = currentTime + duration.count() / 1000000; void* fiber = GetCurrentFiber(); TimerContext timerContext{ time, fiber, false }; context = &timerContext; dispatcher->addTimer(time, fiber); dispatcher->dispatch(); assert(timerContext.context == GetCurrentFiber()); assert(dispatcher != nullptr); assert(context == &timerContext); context = nullptr; if (timerContext.interrupted) { throw InterruptedException(); } } }