danicoin/src/Platform/OSX/System/Dispatcher.cpp

437 lines
13 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "Dispatcher.h"
2015-05-27 12:08:46 +00:00
#include <cassert>
#include <string>
#include <sys/errno.h>
#include <sys/event.h>
#include <sys/time.h>
2015-05-27 12:08:46 +00:00
#include <sys/types.h>
2015-07-30 15:22:07 +00:00
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
2015-05-27 12:08:46 +00:00
#include <unistd.h>
2015-07-30 15:22:07 +00:00
#include "Context.h"
2015-08-05 13:09:05 +00:00
#include "ErrorMessage.h"
2015-05-27 12:08:46 +00:00
namespace System {
2015-07-30 15:22:07 +00:00
namespace{
struct ContextMakingData {
void* uctx;
Dispatcher* dispatcher;
};
2015-07-30 15:22:07 +00:00
class MutextGuard {
public:
MutextGuard(pthread_mutex_t& _mutex) : mutex(_mutex) {
auto ret = pthread_mutex_lock(&mutex);
if (ret != 0) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("MutextGuard::MutextGuard, pthread_mutex_lock failed, " + errorMessage(ret));
2015-07-30 15:22:07 +00:00
}
}
~MutextGuard() {
pthread_mutex_unlock(&mutex);
}
private:
pthread_mutex_t& mutex;
};
const size_t STACK_SIZE = 64 * 1024;
}
2015-07-30 15:22:07 +00:00
static_assert(Dispatcher::SIZEOF_PTHREAD_MUTEX_T == sizeof(pthread_mutex_t), "invalid pthread mutex size");
Dispatcher::Dispatcher() : lastCreatedTimer(0) {
2015-05-27 12:08:46 +00:00
std::string message;
kqueue = ::kqueue();
if (kqueue == -1) {
2015-08-05 13:09:05 +00:00
message = "kqueue failed, " + lastErrorMessage();
} else {
2015-07-30 15:22:07 +00:00
mainContext.uctx = new uctx;
if (getcontext(static_cast<uctx*>(mainContext.uctx)) == -1) {
2015-08-05 13:09:05 +00:00
message = "getcontext failed, " + lastErrorMessage();
} else {
2015-05-27 12:08:46 +00:00
struct kevent event;
EV_SET(&event, 0, EVFILT_USER, EV_ADD, NOTE_FFNOP, 0, NULL);
if (kevent(kqueue, &event, 1, NULL, 0, NULL) == -1) {
2015-08-05 13:09:05 +00:00
message = "kevent failed, " + lastErrorMessage();
2015-05-27 12:08:46 +00:00
} else {
if(pthread_mutex_init(reinterpret_cast<pthread_mutex_t*>(this->mutex), NULL) == -1) {
2015-08-05 13:09:05 +00:00
message = "pthread_mutex_init failed, " + lastErrorMessage();
2015-05-27 12:08:46 +00:00
} else {
remoteSpawned = false;
2015-07-30 15:22:07 +00:00
mainContext.interrupted = false;
mainContext.group = &contextGroup;
mainContext.groupPrev = nullptr;
mainContext.groupNext = nullptr;
contextGroup.firstContext = nullptr;
contextGroup.lastContext = nullptr;
contextGroup.firstWaiter = nullptr;
contextGroup.lastWaiter = nullptr;
currentContext = &mainContext;
firstResumingContext = nullptr;
firstReusableContext = nullptr;
runningContextCount = 0;
2015-05-27 12:08:46 +00:00
return;
}
}
}
2015-05-27 12:08:46 +00:00
auto result = close(kqueue);
assert(result == 0);
}
2015-05-27 12:08:46 +00:00
throw std::runtime_error("Dispatcher::Dispatcher, " + message);
}
Dispatcher::~Dispatcher() {
2015-07-30 15:22:07 +00:00
for (NativeContext* context = contextGroup.firstContext; context != nullptr; context = context->groupNext) {
interrupt(context);
}
2015-07-30 15:22:07 +00:00
yield();
assert(contextGroup.firstContext == nullptr);
assert(contextGroup.firstWaiter == nullptr);
assert(firstResumingContext == nullptr);
assert(runningContextCount == 0);
while (firstReusableContext != nullptr) {
auto ucontext = static_cast<uctx*>(firstReusableContext->uctx);
auto stackPtr = static_cast<uint8_t *>(firstReusableContext->stackPtr);
firstReusableContext = firstReusableContext->next;
delete[] stackPtr;
delete ucontext;
}
2015-05-27 12:08:46 +00:00
auto result = close(kqueue);
assert(result != -1);
result = pthread_mutex_destroy(reinterpret_cast<pthread_mutex_t*>(this->mutex));
assert(result != -1);
}
void Dispatcher::clear() {
2015-07-30 15:22:07 +00:00
while (firstReusableContext != nullptr) {
auto ucontext = static_cast<uctx*>(firstReusableContext->uctx);
auto stackPtr = static_cast<uint8_t *>(firstReusableContext->stackPtr);
firstReusableContext = firstReusableContext->next;
delete[] stackPtr;
delete ucontext;
}
2015-05-27 12:08:46 +00:00
}
void Dispatcher::dispatch() {
2015-07-30 15:22:07 +00:00
NativeContext* context;
2015-05-27 12:08:46 +00:00
for (;;) {
2015-07-30 15:22:07 +00:00
if (firstResumingContext != nullptr) {
context = firstResumingContext;
firstResumingContext = context->next;
2015-05-27 12:08:46 +00:00
break;
}
2015-07-30 15:22:07 +00:00
2015-05-27 12:08:46 +00:00
if(remoteSpawned.load() == true) {
2015-07-30 15:22:07 +00:00
MutextGuard guard(*reinterpret_cast<pthread_mutex_t*>(this->mutex));
2015-05-27 12:08:46 +00:00
while (!remoteSpawningProcedures.empty()) {
spawn(std::move(remoteSpawningProcedures.front()));
remoteSpawningProcedures.pop();
}
2015-05-27 12:08:46 +00:00
remoteSpawned = false;
continue;
}
struct kevent event;
int count = kevent(kqueue, NULL, 0, &event, 1, NULL);
if (count == 1) {
2015-08-27 18:55:14 +00:00
if (event.flags & EV_ERROR) {
continue;
}
2015-05-27 12:08:46 +00:00
if (event.filter == EVFILT_USER && event.ident == 0) {
struct kevent event;
EV_SET(&event, 0, EVFILT_USER, EV_ADD | EV_DISABLE, NOTE_FFNOP, 0, NULL);
if (kevent(kqueue, &event, 1, NULL, 0, NULL) == -1) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::dispatch, kevent failed, " + lastErrorMessage());
2015-05-27 12:08:46 +00:00
}
continue;
}
2015-08-27 18:55:14 +00:00
if (event.filter == EVFILT_WRITE) {
event.flags = EV_DELETE | EV_DISABLE;
kevent(kqueue, &event, 1, NULL, 0, NULL); // ignore error here
}
2015-05-27 12:08:46 +00:00
context = static_cast<OperationContext*>(event.udata)->context;
break;
}
if (errno != EINTR) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::dispatch, kqueue failed, " + lastErrorMessage());
2015-05-27 12:08:46 +00:00
} else {
2015-07-30 15:22:07 +00:00
MutextGuard guard(*reinterpret_cast<pthread_mutex_t*>(this->mutex));
2015-05-27 12:08:46 +00:00
while (!remoteSpawningProcedures.empty()) {
spawn(std::move(remoteSpawningProcedures.front()));
remoteSpawningProcedures.pop();
}
}
}
2015-05-27 12:08:46 +00:00
if (context != currentContext) {
2015-07-30 15:22:07 +00:00
uctx* oldContext = static_cast<uctx*>(currentContext->uctx);
2015-05-27 12:08:46 +00:00
currentContext = context;
2015-07-30 15:22:07 +00:00
if (swapcontext(oldContext,static_cast<uctx*>(currentContext->uctx)) == -1) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::dispatch, swapcontext failed, " + lastErrorMessage());
2015-05-27 12:08:46 +00:00
}
}
}
2015-07-30 15:22:07 +00:00
NativeContext* Dispatcher::getCurrentContext() const {
return currentContext;
}
2015-07-30 15:22:07 +00:00
void Dispatcher::interrupt() {
interrupt(currentContext);
}
2015-07-30 15:22:07 +00:00
void Dispatcher::interrupt(NativeContext* context) {
assert(context!=nullptr);
if (!context->interrupted) {
if (context->interruptProcedure != nullptr) {
context->interruptProcedure();
context->interruptProcedure = nullptr;
} else {
context->interrupted = true;
}
}
}
bool Dispatcher::interrupted() {
if (currentContext->interrupted) {
currentContext->interrupted = false;
return true;
}
return false;
}
void Dispatcher::pushContext(NativeContext* context) {
assert(context!=nullptr);
context->next = nullptr;
if (firstResumingContext != nullptr) {
assert(lastResumingContext != nullptr);
lastResumingContext->next = context;
} else {
firstResumingContext = context;
}
lastResumingContext = context;
}
2015-05-27 12:08:46 +00:00
void Dispatcher::remoteSpawn(std::function<void()>&& procedure) {
2015-07-30 15:22:07 +00:00
MutextGuard guard(*reinterpret_cast<pthread_mutex_t*>(this->mutex));
2015-05-27 12:08:46 +00:00
remoteSpawningProcedures.push(std::move(procedure));
2015-07-30 15:22:07 +00:00
if (remoteSpawned == false) {
2015-05-27 12:08:46 +00:00
remoteSpawned = true;
struct kevent event;
2015-07-30 15:22:07 +00:00
EV_SET(&event, 0, EVFILT_USER, EV_ADD | EV_ENABLE, NOTE_FFCOPY | NOTE_TRIGGER, 0, NULL);
2015-05-27 12:08:46 +00:00
if (kevent(kqueue, &event, 1, NULL, 0, NULL) == -1) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::remoteSpawn, kevent failed, " + lastErrorMessage());
2015-05-27 12:08:46 +00:00
};
}
}
void Dispatcher::spawn(std::function<void()>&& procedure) {
2015-07-30 15:22:07 +00:00
NativeContext* context = &getReusableContext();
if(contextGroup.firstContext != nullptr) {
context->groupPrev = contextGroup.lastContext;
assert(contextGroup.lastContext->groupNext == nullptr);
contextGroup.lastContext->groupNext = context;
} else {
2015-07-30 15:22:07 +00:00
context->groupPrev = nullptr;
contextGroup.firstContext = context;
contextGroup.firstWaiter = nullptr;
}
2015-07-30 15:22:07 +00:00
context->interrupted = false;
context->group = &contextGroup;
context->groupNext = nullptr;
context->procedure = std::move(procedure);
contextGroup.lastContext = context;
pushContext(context);
}
2015-05-27 12:08:46 +00:00
void Dispatcher::yield() {
struct timespec zeroTimeout = { 0, 0 };
2015-08-27 18:55:14 +00:00
int updatesCounter = 0;
2015-05-27 12:08:46 +00:00
for (;;) {
struct kevent events[16];
2015-08-27 18:55:14 +00:00
struct kevent updates[16];
int count = kevent(kqueue, updates, updatesCounter, events, 16, &zeroTimeout);
2015-05-27 12:08:46 +00:00
if (count == 0) {
break;
}
2015-08-27 18:55:14 +00:00
updatesCounter = 0;
2015-05-27 12:08:46 +00:00
if (count > 0) {
for (int i = 0; i < count; ++i) {
2015-08-27 18:55:14 +00:00
if (events[i].flags & EV_ERROR) {
continue;
}
2015-05-27 12:08:46 +00:00
if (events[i].filter == EVFILT_USER && events[i].ident == 0) {
2015-08-27 18:55:14 +00:00
EV_SET(&updates[updatesCounter++], 0, EVFILT_USER, EV_ADD | EV_DISABLE, NOTE_FFNOP, 0, NULL);
2015-05-27 12:08:46 +00:00
2015-07-30 15:22:07 +00:00
MutextGuard guard(*reinterpret_cast<pthread_mutex_t*>(this->mutex));
2015-05-27 12:08:46 +00:00
while (!remoteSpawningProcedures.empty()) {
spawn(std::move(remoteSpawningProcedures.front()));
remoteSpawningProcedures.pop();
}
remoteSpawned = false;
continue;
}
2015-07-30 15:22:07 +00:00
static_cast<OperationContext*>(events[i].udata)->context->interruptProcedure = nullptr;
pushContext(static_cast<OperationContext*>(events[i].udata)->context);
2015-08-27 18:55:14 +00:00
if (events[i].filter == EVFILT_WRITE) {
EV_SET(&updates[updatesCounter++], events[i].ident, EVFILT_WRITE, EV_DELETE | EV_DISABLE, 0, 0, NULL);
}
2015-05-27 12:08:46 +00:00
}
} else {
if (errno != EINTR) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::dispatch, kevent failed, " + lastErrorMessage());
2015-05-27 12:08:46 +00:00
}
}
}
2015-07-30 15:22:07 +00:00
if (firstResumingContext != nullptr) {
pushContext(currentContext);
2015-05-27 12:08:46 +00:00
dispatch();
}
}
int Dispatcher::getKqueue() const {
return kqueue;
}
2015-07-30 15:22:07 +00:00
NativeContext& Dispatcher::getReusableContext() {
if(firstReusableContext == nullptr) {
uctx* newlyCreatedContext = new uctx;
uint8_t* stackPointer = new uint8_t[STACK_SIZE];
static_cast<uctx*>(newlyCreatedContext)->uc_stack.ss_sp = stackPointer;
static_cast<uctx*>(newlyCreatedContext)->uc_stack.ss_size = STACK_SIZE;
ContextMakingData makingData{ newlyCreatedContext, this};
makecontext(static_cast<uctx*>(newlyCreatedContext), reinterpret_cast<void(*)()>(contextProcedureStatic), reinterpret_cast<intptr_t>(&makingData));
uctx* oldContext = static_cast<uctx*>(currentContext->uctx);
if (swapcontext(oldContext, newlyCreatedContext) == -1) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::getReusableContext, swapcontext failed, " + lastErrorMessage());
2015-07-30 15:22:07 +00:00
}
assert(firstReusableContext != nullptr);
assert(firstReusableContext->uctx == newlyCreatedContext);
firstReusableContext->stackPtr = stackPointer;
}
NativeContext* context = firstReusableContext;
firstReusableContext = firstReusableContext->next;
return *context;
}
void Dispatcher::pushReusableContext(NativeContext& context) {
context.next = firstReusableContext;
firstReusableContext = &context;
--runningContextCount;
}
int Dispatcher::getTimer() {
int timer;
if (timers.empty()) {
timer = ++lastCreatedTimer;
} else {
timer = timers.top();
timers.pop();
}
return timer;
}
void Dispatcher::pushTimer(int timer) {
timers.push(timer);
}
2015-07-30 15:22:07 +00:00
void Dispatcher::contextProcedure(void* ucontext) {
assert(firstReusableContext == nullptr);
NativeContext context;
context.uctx = ucontext;
context.interrupted = false;
context.next = nullptr;
firstReusableContext = &context;
uctx* oldContext = static_cast<uctx*>(context.uctx);
if (swapcontext(oldContext, static_cast<uctx*>(currentContext->uctx)) == -1) {
2015-08-05 13:09:05 +00:00
throw std::runtime_error("Dispatcher::contextProcedure, swapcontext failed, " + lastErrorMessage());
2015-07-30 15:22:07 +00:00
}
for (;;) {
2015-07-30 15:22:07 +00:00
++runningContextCount;
try {
context.procedure();
} catch(std::exception&) {
}
2015-07-30 15:22:07 +00:00
if (context.group != nullptr) {
if (context.groupPrev != nullptr) {
assert(context.groupPrev->groupNext == &context);
context.groupPrev->groupNext = context.groupNext;
if (context.groupNext != nullptr) {
assert(context.groupNext->groupPrev == &context);
context.groupNext->groupPrev = context.groupPrev;
} else {
assert(context.group->lastContext == &context);
context.group->lastContext = context.groupPrev;
}
} else {
assert(context.group->firstContext == &context);
context.group->firstContext = context.groupNext;
if (context.groupNext != nullptr) {
assert(context.groupNext->groupPrev == &context);
context.groupNext->groupPrev = nullptr;
} else {
assert(context.group->lastContext == &context);
if (context.group->firstWaiter != nullptr) {
if (firstResumingContext != nullptr) {
assert(lastResumingContext->next == nullptr);
lastResumingContext->next = context.group->firstWaiter;
} else {
firstResumingContext = context.group->firstWaiter;
}
2015-07-30 15:22:07 +00:00
lastResumingContext = context.group->lastWaiter;
context.group->firstWaiter = nullptr;
}
}
}
2015-07-30 15:22:07 +00:00
pushReusableContext(context);
}
2015-05-27 12:08:46 +00:00
dispatch();
}
}
2015-05-27 12:08:46 +00:00
void Dispatcher::contextProcedureStatic(intptr_t context) {
2015-07-30 15:22:07 +00:00
ContextMakingData* makingContextData = reinterpret_cast<ContextMakingData*>(context);
makingContextData->dispatcher->contextProcedure(makingContextData->uctx);
2015-05-27 12:08:46 +00:00
}
}