Files

178 lines
4.8 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Posix/EventManager.h
*
* @brief Header file for poll manager base.
*/
#if (!defined ACTIV_BASE_POSIX_EVENT_MANAGER_H)
#define ACTIV_BASE_POSIX_EVENT_MANAGER_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/EventManagee.h"
#include "ActivMiddleware/CxxYZ/atomic.h"
#include "ActivMiddleware/CxxYZ/memory.h"
#include "ActivMiddleware/Misc/ScopedHelper.h"
#include <vector>
//! Activ namespace.
namespace Activ
{
class IMutex;
class TraceBufferBase;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Event manager.
*
* Acts as a base for poll, epoll, /dev/poll, kevent etc based managers.
* If Settings::m_hasMutex == false, all calls to this object and any EventManagee objects attached to this object
* MUST be made from the same thread.
*/
class EventManager : private Boost::noncopyable
{
public:
/**
* @brief Settings.
*/
class Settings
{
public:
/**
* @brief Constructor.
*
* @param hasMutex
*/
ACTIV_BASE_API Settings(const bool hasMutex);
bool m_hasMutex; ///< Whether to create a mutex.
#if (defined ACTIV_PLATFORM_LINUX)
bool m_shouldUseEpoll; ///< Whether to use epoll if available, default true.
#endif
};
/**
* @brief Destructor.
*/
ACTIV_BASE_API virtual ~EventManager();
/**
* @brief Get the underlying notification handle, if available.
*
* @param handle reference to receive the handle.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_NOT_SUPPORTED the notification type for this EventManager does not support an exportable handle.
*/
virtual StatusCode GetNotificationHandle(HANDLE &handle) const = 0;
/**
* @brief Send a close notification event for a EventManagee.
*
* @param pCallback EventManagee to receive the OnEvent() EVENT_CLOSE.
*/
ACTIV_BASE_API StatusCode SendCloseNotificationEvent(EventManagee *pCallback);
/**
* @brief Wait for events and distribute.
*
* @param timeoutPeriod in ms.
* @param pTraceBuffer trace buffer to record timestamp to.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_SHUTTING_DOWN user has requested thread exit.
*/
virtual StatusCode Distribute(const TimeoutPeriod timeoutPeriod, TraceBufferBase *pTraceBuffer = ACTIV_NULLPTR) = 0;
/**
* @brief Initiate a shutdown of the manager's thread(s).
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_BASE_API StatusCode Shutdown();
/**
* @brief Create an EventManager using the most suitable underlying mechanism for this platform.
*
* @param settings
*
* @return Pointer to an EventManager.
*
* @throw Exception.
*/
ACTIV_BASE_API static CxxYZ::unique_ptr<EventManager> Create(const Settings &settings);
protected:
/**
* @brief Constructor.
*
* @param settings
*/
ACTIV_BASE_API EventManager(const Settings &settings);
/**
* @brief Send notification event to manager thread.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_BASE_API StatusCode SendNotificationEvent();
//! Received a notification event from a user thread.
ACTIV_BASE_API void ProcessNotificationEvent();
typedef std::vector<EventManagee *> CallbackList;
const CxxYZ::unique_ptr<IMutex> m_pMutex;
bool m_shutdownReceived; ///< Set when manager thread receives the shutdown.
ScopedSocket m_notificationManager; ///< Manager end of socket pair to send notifications to manager thread.
private:
/**
* @brief Add a file descriptor to the manager.
*
* @param fd the fd to add.
* @param events the set of events to wait on, initially.
* @param pCallback callback to receive event notifications through.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
virtual StatusCode AddFd(const HANDLE fd, const EventManagee::Events events, EventManagee * const pCallback) = 0;
/**
* @brief Remove a file description from the manager.
*
* @param fd the fd to remove.
*/
virtual StatusCode RemoveFd(const HANDLE fd) = 0;
/**
* @brief Modify the set of events for an fd already managed.
*
* @param fd the fd to modify.
* @param events the new set of events to wait on.
* @param pCallback callback to receive event notifications through.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
virtual StatusCode ModifyEvents(const HANDLE fd, const EventManagee::Events events, EventManagee * const pCallback) = 0;
CxxYZ::atomic_bool m_shutdownPosted; ///< Set when user thread initiates a shutdown.
ScopedSocket m_notificationUser; ///< User end of socket pair to send notification to manager thread.
CallbackList m_closeCallbackList; ///< List of callbacks to notify of close.
friend class EventManagee;
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_POSIX_EVENT_MANAGER_H)