mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 02:18:04 +01:00
Add setaffinity method to thread implementation and interface class
This commit is contained in:
@@ -107,6 +107,44 @@ endif (${CMAKE_CXX_COMPILER_ID} MATCHES "SunPro")
|
|||||||
# TODO: Why is this here?
|
# TODO: Why is this here?
|
||||||
add_definitions( -DPCRE_STATIC)
|
add_definitions( -DPCRE_STATIC)
|
||||||
|
|
||||||
|
# For SetAffinity
|
||||||
|
if(UNIX)
|
||||||
|
INCLUDE (CheckFunctionExists)
|
||||||
|
INCLUDE (CheckCXXSourceCompiles)
|
||||||
|
CHECK_FUNCTION_EXISTS(pthread_setaffinity_np HAVE_PTHREAD_SETAFFINITY_NP)
|
||||||
|
if(HAVE_PTHREAD_SETAFFINITY_NP)
|
||||||
|
message(STATUS "Platform has PTHREAD_SETAFFINITY_NP")
|
||||||
|
add_definitions(-DHAVE_PTHREAD_SETAFFINITY_NP)
|
||||||
|
else(HAVE_PTHREAD_SETAFFINITY_NP)
|
||||||
|
CHECK_CXX_SOURCE_COMPILES("
|
||||||
|
#include <sched.h>
|
||||||
|
int main() {
|
||||||
|
cpu_set_t cpumask;
|
||||||
|
sched_setaffinity( 0, sizeof(cpumask), &cpumask );
|
||||||
|
return 0;
|
||||||
|
}" HAVE_THREE_PARAM_SCHED_SETAFFINITY)
|
||||||
|
|
||||||
|
if(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
|
||||||
|
message(STATUS "Platform has THREE PARAM at PTHREAD_SETAFFINITY_NP")
|
||||||
|
add_definitions(-DHAVE_THREE_PARAM_SCHED_SETAFFINITY)
|
||||||
|
else(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
|
||||||
|
CHECK_CXX_SOURCE_COMPILES("
|
||||||
|
#include <sched.h>
|
||||||
|
int main() {
|
||||||
|
cpu_set_t cpumask;
|
||||||
|
sched_setaffinity( 0, &cpumask );
|
||||||
|
return 0;
|
||||||
|
}" HAVE_TWO_PARAM_SCHED_SETAFFINITY)
|
||||||
|
|
||||||
|
if(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
|
||||||
|
message(STATUS "Platform has TWO PARAM at PTHREAD_SETAFFINITY_NP")
|
||||||
|
add_definitions(-DHAVE_TWO_PARAM_SCHED_SETAFFINITY)
|
||||||
|
endif(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
|
||||||
|
|
||||||
|
endif(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
|
||||||
|
endif(HAVE_PTHREAD_SETAFFINITY_NP)
|
||||||
|
endif(UNIX)
|
||||||
|
|
||||||
add_library( "${LIBNAME}" ${LIB_MODE} ${SRCS})
|
add_library( "${LIBNAME}" ${LIB_MODE} ${SRCS})
|
||||||
add_library( "${POCO_LIBNAME}" ALIAS "${LIBNAME}")
|
add_library( "${POCO_LIBNAME}" ALIAS "${LIBNAME}")
|
||||||
set_target_properties( "${LIBNAME}"
|
set_target_properties( "${LIBNAME}"
|
||||||
|
|||||||
@@ -135,6 +135,11 @@ public:
|
|||||||
/// Typically, the real stack size is rounded up to the nearest
|
/// Typically, the real stack size is rounded up to the nearest
|
||||||
/// page size multiple.
|
/// page size multiple.
|
||||||
|
|
||||||
|
void setAffinity(unsigned int cpu);
|
||||||
|
/// Limit specified thread to run only on the processors "cpu"
|
||||||
|
/// cpu - processor (core) number
|
||||||
|
/// Method would Throw SystemException if affinity did not setted
|
||||||
|
|
||||||
int getStackSize() const;
|
int getStackSize() const;
|
||||||
/// Returns the thread's stack size in bytes.
|
/// Returns the thread's stack size in bytes.
|
||||||
/// If the default stack size is used, 0 is returned.
|
/// If the default stack size is used, 0 is returned.
|
||||||
@@ -350,6 +355,9 @@ inline void Thread::setStackSize(int size)
|
|||||||
setStackSizeImpl(size);
|
setStackSizeImpl(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void Thread::setAffinity(unsigned int cpu) {
|
||||||
|
setAffinityImpl(cpu);
|
||||||
|
}
|
||||||
|
|
||||||
inline int Thread::getStackSize() const
|
inline int Thread::getStackSize() const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ public:
|
|||||||
static int getMaxOSPriorityImpl(int policy);
|
static int getMaxOSPriorityImpl(int policy);
|
||||||
void setStackSizeImpl(int size);
|
void setStackSizeImpl(int size);
|
||||||
int getStackSizeImpl() const;
|
int getStackSizeImpl() const;
|
||||||
|
void setAffinityImpl(unsigned int cpu);
|
||||||
void startImpl(SharedPtr<Runnable> pTarget);
|
void startImpl(SharedPtr<Runnable> pTarget);
|
||||||
void joinImpl();
|
void joinImpl();
|
||||||
bool joinImpl(long milliseconds);
|
bool joinImpl(long milliseconds);
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ public:
|
|||||||
static int getMaxOSPriorityImpl(int policy);
|
static int getMaxOSPriorityImpl(int policy);
|
||||||
void setStackSizeImpl(int size);
|
void setStackSizeImpl(int size);
|
||||||
int getStackSizeImpl() const;
|
int getStackSizeImpl() const;
|
||||||
|
void setAffinityImpl(unsigned int cpu);
|
||||||
void startImpl(Runnable& target);
|
void startImpl(Runnable& target);
|
||||||
void startImpl(Callable target, void* pData = 0);
|
void startImpl(Callable target, void* pData = 0);
|
||||||
|
|
||||||
@@ -139,6 +140,11 @@ inline int ThreadImpl::getOSPriorityImpl() const
|
|||||||
return _pData->osPrio;
|
return _pData->osPrio;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void ThreadImpl::setAffinityImpl(unsigned int cpu)
|
||||||
|
{
|
||||||
|
// TODO : create implementation
|
||||||
|
(void)cpu;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool ThreadImpl::isRunningImpl() const
|
inline bool ThreadImpl::isRunningImpl() const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ public:
|
|||||||
static int getMinOSPriorityImpl(int policy);
|
static int getMinOSPriorityImpl(int policy);
|
||||||
static int getMaxOSPriorityImpl(int policy);
|
static int getMaxOSPriorityImpl(int policy);
|
||||||
void setStackSizeImpl(int size);
|
void setStackSizeImpl(int size);
|
||||||
|
void setAffinityImpl(unsigned int cpu);
|
||||||
int getStackSizeImpl() const;
|
int getStackSizeImpl() const;
|
||||||
void startImpl(SharedPtr<Runnable> pTarget);
|
void startImpl(SharedPtr<Runnable> pTarget);
|
||||||
void joinImpl();
|
void joinImpl();
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ public:
|
|||||||
static int getMaxOSPriorityImpl(int policy);
|
static int getMaxOSPriorityImpl(int policy);
|
||||||
void setStackSizeImpl(int size);
|
void setStackSizeImpl(int size);
|
||||||
int getStackSizeImpl() const;
|
int getStackSizeImpl() const;
|
||||||
|
void setAffinityImpl(unsigned int cpu);
|
||||||
void startImpl(SharedPtr<Runnable> pTarget);
|
void startImpl(SharedPtr<Runnable> pTarget);
|
||||||
void joinImpl();
|
void joinImpl();
|
||||||
bool joinImpl(long milliseconds);
|
bool joinImpl(long milliseconds);
|
||||||
@@ -144,6 +145,11 @@ inline int ThreadImpl::getMaxOSPriorityImpl(int /* policy */)
|
|||||||
return PRIO_HIGHEST_IMPL;
|
return PRIO_HIGHEST_IMPL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void ThreadImpl::setAffinityImpl(unsigned int cpu)
|
||||||
|
{
|
||||||
|
// TODO : create implementation
|
||||||
|
(void)cpu;
|
||||||
|
}
|
||||||
|
|
||||||
inline void ThreadImpl::sleepImpl(long milliseconds)
|
inline void ThreadImpl::sleepImpl(long milliseconds)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,7 +29,11 @@
|
|||||||
#if POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX
|
#if POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX
|
||||||
# include <time.h>
|
# include <time.h>
|
||||||
#endif
|
#endif
|
||||||
|
#if POCO_OS == POCO_OS_MAC_OS_X
|
||||||
|
# include <mach/mach.h>
|
||||||
|
# include <mach/task.h>
|
||||||
|
# include <mach/thread_policy.h>
|
||||||
|
#endif
|
||||||
//
|
//
|
||||||
// Block SIGPIPE in main thread.
|
// Block SIGPIPE in main thread.
|
||||||
//
|
//
|
||||||
@@ -179,6 +183,40 @@ void ThreadImpl::setStackSizeImpl(int size)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThreadImpl::setAffinityImpl(unsigned int cpu)
|
||||||
|
{
|
||||||
|
#if defined (POCO_OS_FAMILY_UNIX) && POCO_OS != POCO_OS_MAC_OS_X
|
||||||
|
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
|
||||||
|
cpu_set_t cpuset;
|
||||||
|
CPU_ZERO(&cpuset);
|
||||||
|
CPU_SET(cpu, &cpuset);
|
||||||
|
#ifdef HAVE_THREE_PARAM_SCHED_SETAFFINITY
|
||||||
|
if (pthread_setaffinity_np(_pData->thread, sizeof(cpuset), &cpuset) != 0)
|
||||||
|
throw SystemException("Failed to set affinity");
|
||||||
|
#else
|
||||||
|
if (pthread_setaffinity_np(_pData->thread, &cpuset) != 0)
|
||||||
|
throw SystemException("Failed to set affinity");
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
poco_bugcheck_msg("Thread affinity not supported on this system");
|
||||||
|
#endif
|
||||||
|
#endif // defined unix & !defined mac os x
|
||||||
|
|
||||||
|
#if POCO_OS == POCO_OS_MAC_OS_X
|
||||||
|
kern_return_t ret;
|
||||||
|
thread_affinity_policy policy;
|
||||||
|
policy.affinity_tag = cpu;
|
||||||
|
|
||||||
|
ret = thread_policy_set(pthread_mach_thread_np(_pData->thread),
|
||||||
|
THREAD_AFFINITY_POLICY,
|
||||||
|
(thread_policy_t) &policy,
|
||||||
|
THREAD_AFFINITY_POLICY_COUNT);
|
||||||
|
if (ret != KERN_SUCCESS) {
|
||||||
|
throw SystemException("Failed to set affinity");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
yieldImpl();
|
||||||
|
}
|
||||||
|
|
||||||
void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
|
void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -103,6 +103,14 @@ void ThreadImpl::setOSPriorityImpl(int prio, int /* policy */)
|
|||||||
setPriorityImpl(prio);
|
setPriorityImpl(prio);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThreadImpl::setAffinityImpl(unsigned int cpu)
|
||||||
|
{
|
||||||
|
DWORD mask = 1;
|
||||||
|
mask <<= cpu;
|
||||||
|
if (SetThreadAffinityMask(_thread, mask) == 0) {
|
||||||
|
throw SystemException("Failed to set affinity");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
|
void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user