2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2015-04-06 16:13:07 +00:00
|
|
|
//
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "Timer.h"
|
|
|
|
#include <cassert>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <string>
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
2015-04-06 16:13:07 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-05-27 12:08:46 +00:00
|
|
|
#endif
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <windows.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <System/InterruptedException.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "Dispatcher.h"
|
|
|
|
|
|
|
|
namespace System {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
struct TimerContext {
|
|
|
|
uint64_t time;
|
2015-04-06 16:13:07 +00:00
|
|
|
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) {
|
2015-05-27 12:08:46 +00:00
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(other.context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
stopped = other.stopped;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer() {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(dispatcher == nullptr || context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer& Timer::operator=(Timer&& other) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(dispatcher == nullptr || context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
dispatcher = other.dispatcher;
|
2015-05-27 12:08:46 +00:00
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(other.context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
stopped = other.stopped;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
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) {
|
2015-05-27 12:08:46 +00:00
|
|
|
TimerContext* timerContext = static_cast<TimerContext*>(context);
|
|
|
|
if (!timerContext->interrupted) {
|
|
|
|
dispatcher->interruptTimer(timerContext->time, timerContext->context);
|
|
|
|
timerContext->interrupted = true;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
void Timer::sleep(std::chrono::milliseconds duration) {
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context == nullptr);
|
|
|
|
if (stopped) {
|
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
LARGE_INTEGER frequency;
|
|
|
|
LARGE_INTEGER ticks;
|
|
|
|
QueryPerformanceCounter(&ticks);
|
|
|
|
QueryPerformanceFrequency(&frequency);
|
|
|
|
uint64_t currentTime = ticks.QuadPart / (frequency.QuadPart / 1000);
|
|
|
|
uint64_t time = currentTime + duration.count();
|
|
|
|
void* fiber = GetCurrentFiber();
|
|
|
|
TimerContext timerContext{ time, fiber, false };
|
|
|
|
context = &timerContext;
|
|
|
|
dispatcher->addTimer(time, fiber);
|
|
|
|
dispatcher->dispatch();
|
|
|
|
assert(timerContext.context == GetCurrentFiber());
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(context == &timerContext);
|
2015-04-06 16:13:07 +00:00
|
|
|
context = nullptr;
|
2015-05-27 12:08:46 +00:00
|
|
|
if (timerContext.interrupted) {
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
}
|