Files
externals/linux/include/ActivMiddleware/ActivBase/Thread.h
T
2023-12-15 13:15:08 -05:00

134 lines
3.4 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Thread.h
*
* @brief Header file for the thread class.
*/
#if (!defined ACTIV_BASE_THREAD_H)
#define ACTIV_BASE_THREAD_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/ThreadUtils.h"
#include "ActivMiddleware/CxxYZ/memory.h"
#if (defined ACTIV_LITE_FRAMEWORK)
#error Fixup code using Thread.h
#endif
//! Activ namespace.
namespace Activ
{
#if (defined ACTIV_PLATFORM_WINDOWS)
// to stop the friend declarations getting confused about linkage
extern "C" unsigned WINAPI ActivThreadGlobalThreadProc(void *pData);
#elif (defined ACTIV_PLATFORM_POSIX)
extern "C" void *ActivThreadGlobalThreadProc(void *pData);
extern "C" void ActivThreadGlobalCleanupProc(void *pData);
class IEvent;
#else
#error Configure for this platform
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Thread base class.
*/
class Thread : public ThreadUtils, private Boost::noncopyable
{
public:
//! Default constructor.
ACTIV_BASE_API Thread();
//! Destructor.
ACTIV_BASE_API virtual ~Thread();
/**
* @brief Start the thread.
*
* @param pData thread parameter.
* @param stackSize optional stack size.
*
* @throw Exception.
*/
ACTIV_BASE_API void Start(void *pData = 0, const size_t stackSize = 0);
/**
* @brief End the current thread.
*
* @param statusCode the value to return from the thread.
*/
ACTIV_BASE_API void End(StatusCode statusCode = STATUS_CODE_SUCCESS);
/**
* @brief Wait for the thread to exit.
*
* @param timeoutPeriod the timeout period to wait, in ms.
*
* @return The return value from the thread Process() function.
*
* @throw Exception on timeout.
*/
ACTIV_BASE_API StatusCode WaitForCompletion(const TimeoutPeriod timeoutPeriod = ACTIV_WAIT_INFINITE) const;
/**
* @brief Get the thread id of the thread managed by this object.
*
* @return Thread id.
*/
ACTIV_BASE_API Id GetId() const;
/**
* @brief Get the priority of the thread managed by this object.
*
* @param priority thread priority.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_FAILURE
*/
ACTIV_BASE_API StatusCode GetPriority(uint32_t &priority) const;
/**
* @brief Set the priority of the thread managed by this object.
*
* @param priority new thread priority.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_FAILURE
*/
ACTIV_BASE_API StatusCode SetPriority(uint32_t priority);
protected:
/**
* @brief Actual thread function (override this for your own processing).
*
* @param pData thread data pointer as passed to Start().
*/
virtual StatusCode Process(void *pData) = 0;
private:
#if (defined ACTIV_PLATFORM_WINDOWS)
friend unsigned WINAPI ActivThreadGlobalThreadProc(void *pData);
#elif (defined ACTIV_PLATFORM_POSIX)
friend void *ActivThreadGlobalThreadProc(void *pData);
friend void ActivThreadGlobalCleanupProc(void *pData);
#else
#error Configure for this platform
#endif
void * m_pData; ///< Application defined data pointer.
mutable Id m_id; ///< Id of the created thread.
#if (defined ACTIV_PLATFORM_WINDOWS)
HANDLE m_handle; ///< Handle of the created thread.
#elif (defined ACTIV_PLATFORM_POSIX)
CxxYZ::shared_ptr<IEvent> m_pEvent; ///< Cleanup event.
#endif
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_THREAD_H)