danicoin/src/System/Event.cpp

112 lines
2.6 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>
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 {
EventWaiter* next;
2014-09-15 10:48:45 +00:00
void* context;
};
}
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) {
dispatcher->pushContext(waiter->context);
}
2014-09-15 10:48:45 +00:00
}
}
void Event::wait() {
assert(dispatcher != nullptr);
if (!state) {
2015-05-27 12:08:46 +00:00
EventWaiter waiter = {nullptr, dispatcher->getCurrentContext()};
if (first != nullptr) {
2015-05-27 12:08:46 +00:00
static_cast<EventWaiter*>(last)->next = &waiter;
} 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());
assert(dispatcher != nullptr);
2014-09-15 10:48:45 +00:00
}
}
2015-05-27 12:08:46 +00:00
}