Changeset 5417

Show
Ignore:
Timestamp:
08/10/06 17:50:56
Author:
robert
Message:

Added OpenThreads?::GetNumberOfProcessors().

Added better support for Linux processor affinity. Note, there happens to be to
API's out in the wild for sched_setaffinity, and two param one, and a three param one. To
arbitrate between the two ) have added a check against the define COMPILE_USING_TWO_PARAM_sched_setaffinity
in in the source. This can be selected on the command line via:

make COMPILE_USING_TWO_PARAM_sched_setaffinity=yes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenThreads/trunk/include/OpenThreads/Thread

    r4275 r5417  
    3030 
    3131namespace OpenThreads { 
     32 
     33/** 
     34 *  Get the number of processors. 
     35 * 
     36 *  Note, systems where no support exists for querrying the number of processors, 1 is returned.  
     37 * 
     38 */ 
     39extern OPENTHREAD_EXPORT_DIRECTIVE int GetNumberOfProcessors(); 
     40 
    3241 
    3342/** 
     
    130139     */ 
    131140    static ThreadPriority GetMasterPriority() {return s_masterThreadPriority;}; 
     141 
    132142 
    133143    /** 
     
    360370     */ 
    361371    void * _prvData; 
    362  
     372     
    363373    /** 
    364374     *  Master thread's priority, set by Thread::Init. 
  • OpenThreads/trunk/pthread_src/GNUmakefile

    r4901 r5417  
    4141LIB = $(LIB_PREFIX)$(TARGET_BASENAME) 
    4242 
    43  
     43ifeq ($(COMPILE_USING_TWO_PARAM_sched_setaffinity),yes) 
     44DEF += -DCOMPILE_USING_TWO_PARAM_sched_setaffinity 
     45endif 
    4446include $(TOPDIR)/Make/makerules 
  • OpenThreads/trunk/pthread_src/PThread.c++

    r4275 r5417  
    3535#endif 
    3636 
     37#if defined (__linux__) 
     38    #include <sched.h> 
     39#endif 
     40 
    3741#include <OpenThreads/Thread> 
    3842#include "PThreadPrivateData.h" 
     
    110114            static_cast<PThreadPrivateData *>(thread->_prvData); 
    111115 
    112 #ifdef __sgi 
    113     pthread_setrunon_np( pd->cpunum ); 
    114 #endif 
    115  
     116        if (pd->cpunum>=0) 
     117        { 
     118            #ifdef __sgi 
     119             
     120                pthread_setrunon_np( pd->cpunum ); 
     121                 
     122            #elif defined (__linux__) && defined(CPU_SET) 
     123 
     124                cpu_set_t cpumask; 
     125                CPU_ZERO( &cpumask ); 
     126                CPU_SET( pd->cpunum, &cpumask ); 
     127 
     128                #if defined(COMPILE_USING_TWO_PARAM_sched_setaffinity) 
     129                    sched_setaffinity( 0, &cpumask ); 
     130                #else 
     131                    sched_setaffinity( 0, sizeof(cpumask), &cpumask ); 
     132                #endif 
     133                 
     134            #endif 
     135        } 
     136         
    116137 
    117138        ThreadCleanupStruct tcs; 
     
    355376    pd->threadPriority = Thread::THREAD_PRIORITY_DEFAULT; 
    356377    pd->threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT; 
     378    pd->cpunum = -1; 
    357379 
    358380    _prvData = static_cast<void *>(pd); 
     
    482504    PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); 
    483505    pd->cpunum = cpunum; 
     506    if (pd->cpunum<0) return -1; 
     507     
    484508#ifdef __sgi 
     509 
    485510    int status; 
    486511    pthread_attr_t thread_attr; 
     
    493518    status = pthread_attr_setscope( &thread_attr, PTHREAD_SCOPE_BOUND_NP ); 
    494519    return status; 
     520 
     521#elif defined (__linux__) && defined(CPU_SET) 
     522 
     523   
     524    if (pd->isRunning && Thread::CurrentThread()==this) 
     525    { 
     526        cpu_set_t cpumask; 
     527        CPU_ZERO( &cpumask ); 
     528        CPU_SET( pd->cpunum, &cpumask ); 
     529 
     530        #if defined(COMPILE_USING_TWO_PARAM_sched_setaffinity) 
     531            return sched_setaffinity( 0, &cpumask ); 
     532        #else 
     533            return sched_setaffinity( 0, sizeof(cpumask), &cpumask ); 
     534        #endif 
     535    } 
     536 
     537    return -1; 
    495538#else 
    496539    return -1; 
     
    828871// Use: protected 
    829872// 
    830 int Thread::YieldCurrentThread() { 
     873int Thread::YieldCurrentThread() 
     874
    831875 
    832876    return sched_yield(); 
     
    842886    return ::usleep(microsec); 
    843887} 
     888 
     889 
     890 
     891//----------------------------------------------------------------------------- 
     892// 
     893// Description:  Get the number of processors 
     894// 
     895int OpenThreads::GetNumberOfProcessors() 
     896{ 
     897#if defined(__linux__) 
     898    return sysconf(_SC_NPROCESSORS_CONF); 
     899#else 
     900    return 1; 
     901#endif     
     902} 
  • OpenThreads/trunk/pthread_src/PThreadPrivateData.h

    r3332 r5417  
    6666    volatile int uniqueId; 
    6767 
    68     volatile unsigned int cpunum; 
     68    volatile int cpunum; 
    6969 
    7070    static int nextId; 
  • OpenThreads/trunk/sproc_src/SprocThread.c++

    r4275 r5417  
    350350 
    351351    _prvData = static_cast<void *>(pd); 
    352  
    353352} 
    354353 
     
    775774 
    776775} 
     776 
     777int Thread::setProcessorAffinity( unsigned int cpunum ) 
     778{ 
     779    return -1; 
     780} 
     781 
     782//----------------------------------------------------------------------------- 
     783// 
     784// Description:  Get the number of processors 
     785// 
     786int OpenThreads::GetNumberOfProcessors() 
     787{ 
     788    return 1; 
     789} 
  • OpenThreads/trunk/win32_src/Win32Thread.cpp

    r5413 r5417  
    238238 
    239239    pd->stackSize = 0; 
    240  
    241240    pd->isRunning = false; 
    242  
    243241    pd->cancelMode = 0; 
    244  
    245242    pd->uniqueId = 0; 
    246  
    247243    pd->threadPriority = Thread::THREAD_PRIORITY_DEFAULT; 
    248  
    249244    pd->threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT; 
    250  
    251245    pd->detached = false; 
    252  
    253246    pd->cancelEvent.set(CreateEvent(NULL,TRUE,FALSE,NULL)); 
    254247 
    255248    _prvData = static_cast<void *>(pd); 
    256  
    257249} 
    258250 
     
    641633#endif 
    642634} 
     635 
     636 
     637//----------------------------------------------------------------------------- 
     638// 
     639// Description:  Get the number of processors 
     640// 
     641int OpenThreads::GetNumberOfProcessors() 
     642{ 
     643    return 1; 
     644}