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 <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <sys/errno.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
#include "Dispatcher.h"
|
2015-08-05 13:09:05 +00:00
|
|
|
#include <System/ErrorMessage.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <System/InterruptedException.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
namespace System {
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
Timer::Timer() : dispatcher(nullptr) {
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
Timer::Timer(Dispatcher& dispatcher) : dispatcher(&dispatcher), context(nullptr), timer(-1) {
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(Timer&& other) : dispatcher(other.dispatcher) {
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(other.context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
timer = other.timer;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
Timer::~Timer() {
|
|
|
|
assert(dispatcher == nullptr || context == nullptr);
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
Timer& Timer::operator=(Timer&& other) {
|
|
|
|
assert(dispatcher == nullptr || context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
dispatcher = other.dispatcher;
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(other.context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
timer = other.timer;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
2015-05-27 12:08:46 +00:00
|
|
|
other.timer = -1;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:23:00 +00:00
|
|
|
void Timer::sleep(std::chrono::nanoseconds duration) {
|
2015-04-06 16:13:07 +00:00
|
|
|
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-07-30 15:22:07 +00:00
|
|
|
OperationContext timerContext;
|
2015-05-27 12:08:46 +00:00
|
|
|
timerContext.context = dispatcher->getCurrentContext();
|
|
|
|
timerContext.interrupted = false;
|
|
|
|
timer = dispatcher->getTimer();
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
struct kevent event;
|
2015-07-30 15:22:07 +00:00
|
|
|
EV_SET(&event, timer, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, NOTE_NSECONDS, duration.count(), &timerContext);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("Timer::stop, kevent failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
context = &timerContext;
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&] {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context != nullptr);
|
|
|
|
OperationContext* timerContext = static_cast<OperationContext*>(context);
|
|
|
|
if (!timerContext->interrupted) {
|
2015-04-06 16:13:07 +00:00
|
|
|
struct kevent event;
|
2015-07-30 15:22:07 +00:00
|
|
|
EV_SET(&event, timer, EVFILT_TIMER, EV_DELETE, 0, 0, NULL);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("Timer::stop, kevent failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->pushContext(timerContext->context);
|
|
|
|
timerContext->interrupted = true;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
2015-07-30 15:22: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;
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(timerContext.context == dispatcher->getCurrentContext());
|
|
|
|
assert(context == &timerContext);
|
2015-04-06 16:13:07 +00:00
|
|
|
context = nullptr;
|
2015-05-27 12:08:46 +00:00
|
|
|
timerContext.context = nullptr;
|
|
|
|
dispatcher->pushTimer(timer);
|
|
|
|
if (timerContext.interrupted) {
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|