Files

289 lines
8.0 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Windows/IoCompletionManager.h
*
* @brief Header file for Windows I/O completion manager classes.
*/
#if (!defined ACTIV_BASE_WINDOWS_IO_COMPLETION_MANAGER_H)
#define ACTIV_BASE_WINDOWS_IO_COMPLETION_MANAGER_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/IniFileParser.h"
#include "ActivMiddleware/ActivBase/ProcessorAffinity.h"
#include "ActivMiddleware/ActivBase/FieldTypes/UInt.h"
#include "ActivMiddleware/CxxYZ/memory.h"
#include <vector>
//! Activ namespace.
namespace Activ
{
class IMutex;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief I/O completion manager class.
*/
class IoCompletionManager : private Boost::noncopyable
{
public:
typedef long EventId; ///< Type for identification of completion port based IEvent objects.
/**
* @brief Completion keys used by the ACTIV middleware.
*/
enum CompletionKey
{
COMPLETION_KEY_START = 0x7a3c6f19, ///< Arbitrary value to "avoid" clashes.
COMPLETION_KEY_DIE,
COMPLETION_KEY_EVENT,
COMPLETION_KEY_WSA_FAILURE
};
/**
* @brief Overlapped class.
*
* Extends the Windows OVERLAPPED structure and adds a virtual callback for i/o completions.
*/
class Overlapped : public OVERLAPPED
{
public:
/**
* @brief Constructor.
*
* @param ioCompletionManager reference to manager.
* @param pMutex mutex to acquire before invoking OnIoCompletion() callback, or 0 if not required.
*/
Overlapped(IoCompletionManager &ioCompletionManager, IMutex * const pMutex);
//! Destructor.
virtual ~Overlapped();
/**
* @brief Post an i/o completion to the completion port. See Windows PostQueuedCompletionStatus().
*
* Causes OnIoCompletion() to be invoked from a manager thread.
*
* @param numberOfBytes
* @param completionKey
*
* @return boolean.
*/
bool PostIoCompletion(const DWORD numberOfBytes, const CompletionKey completionKey);
/**
* @brief Check if i/o is pending for this object.
*
* @return boolean.
*/
bool IsPending() const;
/**
* @brief Set the pending status for this object.
*
* SetPending(true) after an i/o operation has successfully been initiated using this object.
*
* @param isPending
*/
void SetPending(const bool isPending);
/**
* @brief Check if i/o was successful.
*
* Should be called when processing an OnIoCompletion() callback.
*
* @return boolean.
*/
bool WasSuccessfulCompletion() const;
/**
* @brief Get number of bytes in completion.
*
* Should be called when processing an OnIoCompletion() callback.
*
* @return Number of bytes transferred in i/o operation.
*/
DWORD GetNumberOfBytes() const;
/**
* @brief Set number of bytes in completion.
*
* @param numberOfBytes
*/
void SetNumberOfBytes(const DWORD numberOfBytes);
protected:
/**
* @brief Overlapped i/o has completed.
*
* Note if a mutex was provided to the constructor of the object, it will be acquired before
* invoking this callback.
*/
virtual void OnIoCompletion() = 0;
private:
IoCompletionManager & m_ioCompletionManager; ///< Hook back to manager.
IMutex * const m_pMutex; ///< Mutex, or 0 if none.
bool m_isPending;
bool m_wasSuccessfulCompletion;
DWORD m_numberOfBytes;
friend class IoCompletionManager; ///< Allow manager access.
};
/**
* @brief Settings.
*/
class Settings
{
public:
/**
* @brief Default constructor.
*/
ACTIV_BASE_API Settings();
/**
* @brief Read settings from an ini file.
*
* @param sectionIterator iterator to an ini file section.
*
* @throw Exception
*/
ACTIV_BASE_API void Read(const IniFileParser::ConstSectionIterator &sectionIterator);
UInt m_numberOfIoThreads; ///< Default undefined; let object decide.
ProcessorAffinity m_threadProcessorAffinity; ///< Default: empty; do nothing. Else a list of processor ids to use cyclicly to set launched threads' processor affinity.
HANDLE m_completionPort; ///< Default 0; create a completion port. Otherwise use the provided handle.
};
/**
* @brief Constructor.
*
* @param settings
*
* @throw Exception
*/
ACTIV_BASE_API IoCompletionManager(const Settings &settings);
/**
* @brief Destructor.
*/
ACTIV_BASE_API ~IoCompletionManager();
/**
* @brief Attach a handle to the completion port.
*
* @param handle the handle to attach.
*
* @throw OsException.
*/
ACTIV_BASE_API void AttachHandle(const HANDLE handle) const;
/**
* @brief Get the underlying completion port handle.
*
* @return Handle.
*/
ACTIV_BASE_API HANDLE GetHandle() const;
/**
* @brief Run the completion port manager.
*
* @param timeout how long to wait for a completion, or ACTIV_WAIT_INFINITE.
*
* @return true if the completion manager has been shut down.
*/
ACTIV_BASE_API const bool Run(const uint32_t timeout);
protected:
/**
* @brief Callback for an IEvent notification.
*
* @param eventId
*
* @return STATUS_CODE_EXIT to shutdown the IoCompletionManager; any other value is ignored.
*/
ACTIV_BASE_API virtual const StatusCode OnEvent(const EventId eventId);
private:
class IoThread;
//! Io worker thread entry point.
void IoThreadFunction();
typedef CxxYZ::shared_ptr<IoThread> IoThreadPtr;
typedef std::vector<IoThreadPtr> IoThreadList;
DWORD m_concurrency; ///< Number of processors in the system.
IoThreadList m_ioThreadList; ///< List of io threads.
HANDLE m_completionPort; ///< Io completion port.
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline IoCompletionManager::Overlapped::Overlapped(IoCompletionManager &ioCompletionManager, IMutex * const pMutex) :
m_ioCompletionManager(ioCompletionManager),
m_pMutex(pMutex),
m_isPending(false)
{
OVERLAPPED *pOverlapped = static_cast<OVERLAPPED *>(this);
::memset(pOverlapped, 0, sizeof(OVERLAPPED));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline IoCompletionManager::Overlapped::~Overlapped()
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool IoCompletionManager::Overlapped::PostIoCompletion(const DWORD numberOfBytes, const CompletionKey completionKey)
{
return (FALSE != ::PostQueuedCompletionStatus(m_ioCompletionManager.m_completionPort, numberOfBytes, completionKey, this));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool IoCompletionManager::Overlapped::IsPending() const
{
return m_isPending;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void IoCompletionManager::Overlapped::SetPending(const bool isPending)
{
m_isPending = isPending;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool IoCompletionManager::Overlapped::WasSuccessfulCompletion() const
{
return m_wasSuccessfulCompletion;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DWORD IoCompletionManager::Overlapped::GetNumberOfBytes() const
{
return m_numberOfBytes;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void IoCompletionManager::Overlapped::SetNumberOfBytes(const DWORD numberOfBytes)
{
m_numberOfBytes = numberOfBytes;
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_WINDOWS_IO_COMPLETION_MANAGER_H)