169 lines
3.8 KiB
C++
169 lines
3.8 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/CtrlHandler.h
|
|
*
|
|
* @brief Header file for the CtrlHandler class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_CTRL_HANDLER_H)
|
|
#define ACTIV_BASE_CTRL_HANDLER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
|
|
#if (ACTIV_PLATFORM_LINUX_HAS_MIN_GLIBC(2, 9))
|
|
#define ACTIV_BASE_CTRL_HANDLER_USE_SIGNAL_FD
|
|
#endif
|
|
|
|
#include "ActivMiddleware/CxxYZ/atomic.h"
|
|
|
|
#if (defined ACTIV_BASE_CTRL_HANDLER_USE_SIGNAL_FD)
|
|
#include "ActivMiddleware/ActivBase/EventManagee.h"
|
|
#include "ActivMiddleware/Misc/ScopedHelper.h"
|
|
#endif
|
|
|
|
#if (defined ACTIV_PLATFORM_POSIX)
|
|
#include "ActivMiddleware/CxxYZ/thread.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(scoped_ptr.hpp)
|
|
|
|
#include <signal.h>
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Ctrl handler class.
|
|
*
|
|
* Note only one instance of CtrlHandler is allowed on the Windows platform (per process).
|
|
*/
|
|
class CtrlHandler : private Boost::noncopyable
|
|
{
|
|
public:
|
|
/**
|
|
* @brief List of ctrl event types.
|
|
*/
|
|
enum EventType
|
|
{
|
|
CTRL_C,
|
|
CTRL_BREAK,
|
|
CTRL_EXIT_PROCESS,
|
|
CTRL_LOGOFF_USER,
|
|
CTRL_SHUTDOWN_MACHINE,
|
|
CTRL_UNKNOWN
|
|
};
|
|
|
|
#if (defined ACTIV_BASE_CTRL_HANDLER_USE_SIGNAL_FD)
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* This constructor (if available with recent enough glibc) does not incur the cost of an additional thread, by
|
|
* using signalfd and receiving signal notifications via the provided EventManager. As such, the OnEvent() callback
|
|
* will be invoked in the context of the EventManager's thread.
|
|
*
|
|
* @param eventManager
|
|
*
|
|
* @throw Exception if the handler could not be installed.
|
|
*/
|
|
ACTIV_BASE_API CtrlHandler(EventManager &eventManager);
|
|
|
|
#endif
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* On Posix, this constructor creates a thread to sigwait() for signals. As such, the OnEvent() callback is invoked
|
|
* from the context of this thread.
|
|
*
|
|
* @throw Exception if the handler could not be installed.
|
|
*/
|
|
ACTIV_BASE_API CtrlHandler();
|
|
|
|
//! Destructor.
|
|
ACTIV_BASE_API virtual ~CtrlHandler();
|
|
|
|
protected:
|
|
/**
|
|
* @brief Callback when a ctrl event occurs.
|
|
*
|
|
* No guarantees are provided about which thread will invoke this callback - unless
|
|
* ACTIV_BASE_CTRL_HANDLER_USE_SIGNAL_FD is defined, in which case it will be a thread running the EventManager provided
|
|
* to the constructor.
|
|
*
|
|
* @param eventType the type of ctrl event that has occured.
|
|
*
|
|
* @retval true if the event has been handled.
|
|
* @retval false if the event has not been handled.
|
|
*/
|
|
ACTIV_BASE_API virtual bool OnEvent(const EventType eventType);
|
|
|
|
private:
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
|
|
static CxxYZ::atomic<CtrlHandler *> m_pCtrlHandler; ///< Can only have one ctrl handler per process.
|
|
|
|
static BOOL WINAPI HandlerFunction(DWORD ctrlType);
|
|
|
|
#elif (defined ACTIV_PLATFORM_POSIX)
|
|
|
|
class SignalMask
|
|
{
|
|
public:
|
|
SignalMask();
|
|
~SignalMask();
|
|
|
|
sigset_t m_originalSigSet;
|
|
sigset_t m_blockSigSet;
|
|
};
|
|
|
|
const SignalMask m_signalMask;
|
|
|
|
#endif
|
|
|
|
#if (defined ACTIV_BASE_CTRL_HANDLER_USE_SIGNAL_FD)
|
|
|
|
class SignalFd
|
|
{
|
|
public:
|
|
SignalFd(const sigset_t &sigset);
|
|
~SignalFd();
|
|
|
|
const int m_fd;
|
|
};
|
|
|
|
//! EventManagee to receive signalfd events.
|
|
class EventManagee : public Activ::EventManagee
|
|
{
|
|
public:
|
|
EventManagee(CtrlHandler &ctrlHandler, EventManager &eventManager);
|
|
~EventManagee();
|
|
|
|
protected:
|
|
virtual void OnEvents(const Events events) ACTIV_OVERRIDE;
|
|
|
|
private:
|
|
CtrlHandler &m_ctrlHandler;
|
|
};
|
|
|
|
Boost::scoped_ptr<const SignalFd> m_pSignalFd;
|
|
Boost::scoped_ptr<EventManagee> m_pEventManagee;
|
|
|
|
#endif
|
|
|
|
#if (defined ACTIV_PLATFORM_POSIX)
|
|
|
|
void ThreadFunction();
|
|
|
|
CxxYZ::atomic_bool m_isShuttingDown;
|
|
CxxYZ::thread m_thread; ///< Thread waiting for signals.
|
|
|
|
#elif (!defined ACTIV_PLATFORM_WINDOWS)
|
|
#error Configure for this platform
|
|
#endif
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_CTRL_HANDLER_H)
|