2015-04-23 16:07:22 +00:00
|
|
|
// Copyright (c) 2011-2015 The Cryptonote developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
#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-07-30 15:22:07 +00:00
|
|
|
NativeContext* context;
|
2015-04-06 16:13:07 +00:00
|
|
|
bool interrupted;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer() : dispatcher(nullptr) {
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
Timer::Timer(Dispatcher& dispatcher) : dispatcher(&dispatcher), context(nullptr) {
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(Timer&& other) : dispatcher(other.dispatcher) {
|
2015-05-27 12:08:46 +00:00
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(other.context == nullptr);
|
|
|
|
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);
|
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::sleep(std::chrono::nanoseconds duration) {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context == nullptr);
|
2015-07-30 15:22:07 +00:00
|
|
|
if (dispatcher->interrupted()) {
|
2015-04-06 16:13:07 +00:00
|
|
|
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);
|
2015-07-15 12:23:00 +00:00
|
|
|
uint64_t time = currentTime + duration.count() / 1000000;
|
2015-07-30 15:22:07 +00:00
|
|
|
TimerContext timerContext{ time, dispatcher->getCurrentContext(), false };
|
2015-05-27 12:08:46 +00:00
|
|
|
context = &timerContext;
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->addTimer(time, dispatcher->getCurrentContext());
|
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&]() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context != nullptr);
|
|
|
|
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
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
dispatcher->dispatch();
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = nullptr;
|
|
|
|
assert(timerContext.context == dispatcher->getCurrentContext());
|
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
|
|
|
|
|
|
|
}
|