Some more improvements

This commit is contained in:
WolverinDEV 2021-02-14 20:41:10 +01:00
parent be1a189076
commit e9445d6568
2 changed files with 11 additions and 9 deletions

View File

@ -94,6 +94,7 @@ bool ServerCommandHandler::execute_handling() {
if(!result) {
/* flush all commands */
this->inner->reset();
more_pending = false;
break;
}
} catch (std::exception& ex) {
@ -209,18 +210,14 @@ void ServerCommandExecutor::shutdown() {
void ServerCommandExecutor::enqueue_handler(const std::shared_ptr<ServerCommandHandler> &handler) {
std::lock_guard handler_lock{this->handler_mutex};
if(handler->next_handler) {
if(handler->next_handler || this->handler_tail == &handler->next_handler) {
/* handler already scheduled */
return;
}
if(handler->executing) {
/* handler currently gets executed */
return;
}
if(this->handler_tail == &handler->next_handler) {
/* handler already schedules (current head) */
if(handler->executing || handler->reschedule) {
/* Handle is currently executing */
handler->reschedule = true;
return;
}
@ -258,15 +255,19 @@ void ServerCommandExecutor::executor() {
}
executor->executing = true;
executor->reschedule = false;
executor->next_handler = nullptr;
handler_lock.unlock();
auto reschedule = executor->execute_handling();
handler_lock.lock();
reschedule |= std::exchange(executor->reschedule, false);
executor->executing = false;
if(reschedule && !executor->next_handler && this->handler_tail != &executor->next_handler) {
if(reschedule) {
assert(!executor->next_handler);
*this->handler_tail = executor;
this->handler_tail = &executor->next_handler;

View File

@ -39,6 +39,7 @@ namespace ts::server {
/* locked by ServerCommandExecutor::handler_mutex */
std::shared_ptr<ServerCommandHandler> next_handler{nullptr};
bool executing{false};
bool reschedule{false};
/**
* @returns `true` if more commands need to be handled and `false` if all commands have been handled.