feat(Core/SignalHandler): Replace ACE signal handling with std (#4877)

This commit is contained in:
Kargatum
2021-03-24 21:50:56 +07:00
committed by GitHub
parent e0d36be56e
commit c4c06a7734
3 changed files with 60 additions and 66 deletions

View File

@@ -4,26 +4,41 @@
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef __SIGNAL_HANDLER_H__
#define __SIGNAL_HANDLER_H__
#ifndef _SIGNAL_HANDLER_H_
#define _SIGNAL_HANDLER_H_
#include <ace/Event_Handler.h>
#include <csignal>
#include <unordered_set>
#include <mutex>
namespace acore
{
/// Handle termination signals
class SignalHandler : public ACE_Event_Handler
class SignalHandler
{
public:
int handle_signal(int SigNum, siginfo_t* = nullptr, ucontext_t* = nullptr) override
{
HandleSignal(SigNum);
return 0;
}
virtual void HandleSignal(int /*SigNum*/) { };
};
private:
std::unordered_set<int> _handled;
mutable std::mutex _mutex;
public:
bool handle_signal(int sig, void (*func)(int))
{
std::lock_guard lock(_mutex);
if (_handled.find(sig) != _handled.end())
return false;
_handled.insert(sig);
signal(sig, func);
return true;
}
~SignalHandler()
{
for (auto const& sig : _handled)
signal(sig, nullptr);
}
};
}
#endif /* __SIGNAL_HANDLER_H__ */
#endif // _SIGNAL_HANDLER_H_