add getAffinity method

throw NotImplementedException on unsupported platforms
This commit is contained in:
bas524
2015-03-05 18:11:56 +03:00
parent 158aaab180
commit 888abad26c
7 changed files with 951 additions and 884 deletions

View File

@@ -23,7 +23,7 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Event.h" #include "Poco/Event.h"
#include "Poco/Mutex.h" #include "Poco/Mutex.h"
#include "Poco/Environment.h"
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(_WIN32_WCE) #if defined(_WIN32_WCE)

View File

@@ -74,7 +74,8 @@ 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 setAffinityImpl(unsigned cpu);
unsigned getAffinityImpl() const;
void startImpl(SharedPtr<Runnable> pTarget); void startImpl(SharedPtr<Runnable> pTarget);
void joinImpl(); void joinImpl();
bool joinImpl(long milliseconds); bool joinImpl(long milliseconds);

View File

@@ -80,7 +80,9 @@ 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 setAffinityImpl(unsigned cpu);
unsigned getAffinityImpl() const;
void startImpl(Runnable& target); void startImpl(Runnable& target);
void startImpl(Callable target, void* pData = 0); void startImpl(Callable target, void* pData = 0);
@@ -140,10 +142,15 @@ inline int ThreadImpl::getOSPriorityImpl() const
return _pData->osPrio; return _pData->osPrio;
} }
inline void ThreadImpl::setAffinityImpl(unsigned int cpu) inline void ThreadImpl::setAffinityImpl(unsigned cpu)
{ {
// TODO : create implementation
(void)cpu; (void)cpu;
throw Poco::NotImplementedException("Thread affinity not supported on this system");
}
inline unsigned ThreadImpl::getAffinityImpl()
{
throw Poco::NotImplementedException("Thread affinity not supported on this system");
} }
inline bool ThreadImpl::isRunningImpl() const inline bool ThreadImpl::isRunningImpl() const

View File

@@ -66,7 +66,8 @@ 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); void setAffinityImpl(unsigned cpu);
unsigned getAffinityImpl() const;
int getStackSizeImpl() const; int getStackSizeImpl() const;
void startImpl(SharedPtr<Runnable> pTarget); void startImpl(SharedPtr<Runnable> pTarget);
void joinImpl(); void joinImpl();

View File

@@ -67,7 +67,8 @@ 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 setAffinityImpl(unsigned cpu);
unsigned getAffinityImpl() const;
void startImpl(SharedPtr<Runnable> pTarget); void startImpl(SharedPtr<Runnable> pTarget);
void joinImpl(); void joinImpl();
bool joinImpl(long milliseconds); bool joinImpl(long milliseconds);
@@ -145,10 +146,15 @@ inline int ThreadImpl::getMaxOSPriorityImpl(int /* policy */)
return PRIO_HIGHEST_IMPL; return PRIO_HIGHEST_IMPL;
} }
inline void ThreadImpl::setAffinityImpl(unsigned int cpu) inline void ThreadImpl::setAffinityImpl(unsigned cpu)
{ {
// TODO : create implementation
(void)cpu; (void)cpu;
throw Poco::NotImplementedException("Thread affinity not supported on this system");
}
inline unsigned ThreadImpl::getAffinityImpl()
{
throw Poco::NotImplementedException("Thread affinity not supported on this system");
} }
inline void ThreadImpl::sleepImpl(long milliseconds) inline void ThreadImpl::sleepImpl(long milliseconds)

View File

@@ -28,6 +28,7 @@
#endif #endif
#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>
# include <unistd.h>
#endif #endif
#if POCO_OS == POCO_OS_MAC_OS_X #if POCO_OS == POCO_OS_MAC_OS_X
# include <mach/mach.h> # include <mach/mach.h>
@@ -183,7 +184,7 @@ void ThreadImpl::setStackSizeImpl(int size)
#endif #endif
} }
void ThreadImpl::setAffinityImpl(unsigned int cpu) void ThreadImpl::setAffinityImpl(unsigned cpu)
{ {
#if defined (POCO_OS_FAMILY_UNIX) && POCO_OS != POCO_OS_MAC_OS_X #if defined (POCO_OS_FAMILY_UNIX) && POCO_OS != POCO_OS_MAC_OS_X
#ifdef HAVE_PTHREAD_SETAFFINITY_NP #ifdef HAVE_PTHREAD_SETAFFINITY_NP
@@ -211,13 +212,60 @@ void ThreadImpl::setAffinityImpl(unsigned int cpu)
THREAD_AFFINITY_POLICY, THREAD_AFFINITY_POLICY,
(thread_policy_t) &policy, (thread_policy_t) &policy,
THREAD_AFFINITY_POLICY_COUNT); THREAD_AFFINITY_POLICY_COUNT);
if (ret != KERN_SUCCESS) { if (ret != KERN_SUCCESS)
{
throw SystemException("Failed to set affinity"); throw SystemException("Failed to set affinity");
} }
#endif #endif
yieldImpl(); yieldImpl();
} }
unsigned ThreadImpl::getAffinityImpl() const {
unsigned cpuSet = 0;
unsigned cpuCount = Environment::processorCount();
#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);
#ifdef HAVE_THREE_PARAM_SCHED_SETAFFINITY
if (pthread_getaffinity_np(_pData->thread, sizeof(cpuset), &cpuset) != 0)
throw SystemException("Failed to get affinity");
#else
if (pthread_getaffinity_np(_pData->thread, &cpuset) != 0)
throw SystemException("Failed to get affinity");
#endif
for (unsigned i = 0; i < cpuCount; i++) {
if (CPU_ISSET(i, &cpuset)) {
cpuSet = i;
break;
}
}
#else
throw Poco::NotImplementedException("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;
mach_msg_type_number_t count = THREAD_AFFINITY_POLICY_COUNT;
boolean_t get_default = FALSE;
ret = thread_policy_get(pthread_mach_thread_np(_pData->thread),
THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy,
&count,
&get_default);
if (ret != KERN_SUCCESS) {
throw SystemException("Failed to get affinity");
}
cpuSet = policy.affinity_tag - 1;
if (cpuSet >= cpuCount)
cpuSet = 0;
#endif
return cpuSet;
}
void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget) void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
{ {
if (_pData->pRunnableTarget) if (_pData->pRunnableTarget)

View File

@@ -103,7 +103,7 @@ void ThreadImpl::setOSPriorityImpl(int prio, int /* policy */)
setPriorityImpl(prio); setPriorityImpl(prio);
} }
void ThreadImpl::setAffinityImpl(unsigned int cpu) void ThreadImpl::setAffinityImpl(unsigned cpu)
{ {
DWORD mask = 1; DWORD mask = 1;
mask <<= cpu; mask <<= cpu;
@@ -112,6 +112,10 @@ void ThreadImpl::setAffinityImpl(unsigned int cpu)
} }
} }
unsigned ThreadImpl::getAffinityImpl() const {
throw Poco::NotImplementedException("Get thread affinity not supported on this system");
}
void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget) void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
{ {
if (isRunningImpl()) if (isRunningImpl())