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

View File

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