danicoin/src/System/Event.cpp

147 lines
3.5 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-09-15 10:48:45 +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 "Event.h"
#include <cassert>
2015-05-27 12:08:46 +00:00
#include <System/Dispatcher.h>
2015-07-30 15:22:07 +00:00
#include <System/InterruptedException.h>
2014-09-15 10:48:45 +00:00
2015-05-27 12:08:46 +00:00
namespace System {
namespace {
2015-05-27 12:08:46 +00:00
struct EventWaiter {
2015-07-30 15:22:07 +00:00
bool interrupted;
EventWaiter* prev;
2015-05-27 12:08:46 +00:00
EventWaiter* next;
2015-07-30 15:22:07 +00:00
NativeContext* context;
2014-09-15 10:48:45 +00:00
};
}
Event::Event() : dispatcher(nullptr) {
}
2015-05-27 12:08:46 +00:00
Event::Event(Dispatcher& dispatcher) : dispatcher(&dispatcher), state(false), first(nullptr) {
2014-09-15 10:48:45 +00:00
}
Event::Event(Event&& other) : dispatcher(other.dispatcher) {
2015-05-27 12:08:46 +00:00
if (dispatcher != nullptr) {
state = other.state;
if (!state) {
assert(other.first == nullptr);
first = nullptr;
2014-09-15 10:48:45 +00:00
}
other.dispatcher = nullptr;
2014-09-15 10:48:45 +00:00
}
}
Event::~Event() {
2015-05-27 12:08:46 +00:00
assert(dispatcher == nullptr || state || first == nullptr);
2014-09-15 10:48:45 +00:00
}
Event& Event::operator=(Event&& other) {
2015-05-27 12:08:46 +00:00
assert(dispatcher == nullptr || state || first == nullptr);
dispatcher = other.dispatcher;
2015-05-27 12:08:46 +00:00
if (dispatcher != nullptr) {
state = other.state;
if (!state) {
assert(other.first == nullptr);
first = nullptr;
2014-09-15 10:48:45 +00:00
}
other.dispatcher = nullptr;
2014-09-15 10:48:45 +00:00
}
return *this;
}
bool Event::get() const {
assert(dispatcher != nullptr);
2014-09-15 10:48:45 +00:00
return state;
}
void Event::clear() {
assert(dispatcher != nullptr);
2015-05-27 12:08:46 +00:00
if (state) {
state = false;
first = nullptr;
}
2014-09-15 10:48:45 +00:00
}
void Event::set() {
assert(dispatcher != nullptr);
2015-05-27 12:08:46 +00:00
if (!state) {
state = true;
for (EventWaiter* waiter = static_cast<EventWaiter*>(first); waiter != nullptr; waiter = waiter->next) {
2015-07-30 15:22:07 +00:00
waiter->context->interruptProcedure = nullptr;
2015-05-27 12:08:46 +00:00
dispatcher->pushContext(waiter->context);
}
2014-09-15 10:48:45 +00:00
}
}
void Event::wait() {
assert(dispatcher != nullptr);
2015-07-30 15:22:07 +00:00
if (dispatcher->interrupted()) {
throw InterruptedException();
}
if (!state) {
2015-07-30 15:22:07 +00:00
EventWaiter waiter = { false, nullptr, nullptr, dispatcher->getCurrentContext() };
waiter.context->interruptProcedure = [&] {
if (waiter.next != nullptr) {
assert(waiter.next->prev == &waiter);
waiter.next->prev = waiter.prev;
} else {
assert(last == &waiter);
last = waiter.prev;
}
if (waiter.prev != nullptr) {
assert(waiter.prev->next == &waiter);
waiter.prev->next = waiter.next;
} else {
assert(first == &waiter);
first = waiter.next;
}
assert(!waiter.interrupted);
waiter.interrupted = true;
dispatcher->pushContext(waiter.context);
};
if (first != nullptr) {
2015-05-27 12:08:46 +00:00
static_cast<EventWaiter*>(last)->next = &waiter;
2015-07-30 15:22:07 +00:00
waiter.prev = static_cast<EventWaiter*>(last);
} else {
first = &waiter;
}
2014-09-15 10:48:45 +00:00
last = &waiter;
2015-05-27 12:08:46 +00:00
dispatcher->dispatch();
assert(waiter.context == dispatcher->getCurrentContext());
2015-07-30 15:22:07 +00:00
assert( waiter.context->interruptProcedure == nullptr);
assert(dispatcher != nullptr);
2015-07-30 15:22:07 +00:00
if (waiter.interrupted) {
throw InterruptedException();
}
2014-09-15 10:48:45 +00:00
}
}
2015-05-27 12:08:46 +00:00
}