/** * @file include/ActivMiddleware/Misc/HandleManager.h * * @brief Header file for the handle manager class. */ #if (!defined ACTIV_MISC_HANDLE_MANAGER_H) #define ACTIV_MISC_HANDLE_MANAGER_H #include "ActivMiddleware/ActivMiddleware.h" #if (defined ACTIV_PLATFORM_POSIX) #include #endif //! Activ namespace. namespace Activ { /** * @brief HandleManager class. * * T should be a class that derives from HandleManager and provides a void OnEvent(size_t index) function * which will be invoked for each signalled handle during a Wait() call, eg: * * class MyHandleManager : public HandleManager * { * public: * void OnEvent(const size_t index) * { * // index is the index into the HANDLE array (as passed to the constructor or Initialize() call) * // of the signalled handle. * } * }; */ template class HandleManager : private Boost::noncopyable { public: /** * @brief Default constructor. */ HandleManager(); /** * @brief Constructor. * * @param pHandles pointer to an array of handles to manage. * @param numberOfHandles size of @a pHandles. */ HandleManager(HANDLE *pHandles, const size_t numberOfHandles); /** * @brief Initialize. * * @param pHandles pointer to an array of handles to manage. * @param numberOfHandles size of @a pHandles. */ void Initialize(HANDLE *pHandles, const size_t numberOfHandles); /** * @brief Wait for notification on handles. * * For each signalled handle, T::OnEvent(index) will be invoked. * * @param timeoutPeriod in ms. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_TIMEOUT * @retval ... */ StatusCode Wait(const TimeoutPeriod timeoutPeriod); private: T & m_derived; size_t m_numberOfHandles; #if (defined ACTIV_PLATFORM_POSIX) typedef std::vector PollFdList; PollFdList m_pollFdList; #elif (defined ACTIV_PLATFORM_WINDOWS) typedef std::vector HandleList; HandleList m_handleList; #else #error Configure #endif }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template HandleManager::HandleManager() : m_derived(static_cast(*this)) { } // --------------------------------------------------------------------------------------------------------------------------------- template HandleManager::HandleManager(HANDLE *pHandles, const size_t numberOfHandles) : m_derived(static_cast(*this)) { Initialize(pHandles, numberOfHandles); } // --------------------------------------------------------------------------------------------------------------------------------- template void HandleManager::Initialize(HANDLE *pHandles, const size_t numberOfHandles) { m_numberOfHandles = numberOfHandles; #if (defined ACTIV_PLATFORM_POSIX) m_pollFdList.clear(); for (size_t i = 0; i < m_numberOfHandles; ++i) { pollfd pollFd; pollFd.fd = pHandles[i]; pollFd.events = POLLIN; m_pollFdList.push_back(pollFd); } #elif (defined ACTIV_PLATFORM_WINDOWS) m_handleList.assign(pHandles, pHandles + numberOfHandles); #else #error Configure #endif } // --------------------------------------------------------------------------------------------------------------------------------- template StatusCode HandleManager::Wait(const TimeoutPeriod timeoutPeriod) { #if (defined ACTIV_PLATFORM_POSIX) // For an infinite timeout we loop with a small timeout in case of missed wakeup. This is to work around this: // https://bugzilla.redhat.com/show_bug.cgi?id=494404 const int actualTimeout = (ACTIV_WAIT_INFINITE == timeoutPeriod) ? 50 : static_cast(timeoutPeriod); bool isDone = false; while (!isDone) { const int res = ::poll(&m_pollFdList[0], m_numberOfHandles, actualTimeout); if (res > 0) { for (size_t i = 0, numberSignalled = 0; i < m_numberOfHandles; ++i) { if (m_pollFdList[i].revents & POLLIN) { m_derived.OnEvent(i); if (++numberSignalled == static_cast(res)) break; } } isDone = true; } else if (ACTIV_WAIT_INFINITE != timeoutPeriod) { if (res < 0) return OsErrorToStatusCode(errno); else return STATUS_CODE_TIMEOUT; } } #elif (defined ACTIV_PLATFORM_WINDOWS) const DWORD res = ::WaitForMultipleObjects(static_cast(m_numberOfHandles), &m_handleList[0], FALSE, timeoutPeriod); if ((res >= WAIT_OBJECT_0) && (res < (WAIT_OBJECT_0 + m_numberOfHandles))) m_derived.OnEvent(res - WAIT_OBJECT_0); else if ((res >= WAIT_ABANDONED_0) && (res < WAIT_ABANDONED_0 + m_numberOfHandles)) m_derived.OnEvent(res - WAIT_ABANDONED_0); else if (WAIT_TIMEOUT == res) return STATUS_CODE_TIMEOUT; else // (WAIT_FAILED == res) return OsErrorToStatusCode(::GetLastError()); #else #error Configure #endif return STATUS_CODE_SUCCESS; } } // namespace Activ #endif // (!defined ACTIV_MISC_HANDLE_MANAGER_H)