Tcl Source Code

Artifact [5bccb003b9]
Login

Artifact 5bccb003b9f4c6c3a8a91127a400a7a69d1c77ff:

Attachment "numcpu.c" to ticket [3065485fff] added by andreas_kupries 2010-09-17 02:57:54.
/*
 * TODO:
 * = Extend tcl.m4 with check for 'sysconf()'.
 * = Find a proper define for BSD and/or OSX.
 *   Or provide our own through tcl.m4/configure.
 */

int
Tcl_GetNumberOfProcessors (int flags)
{
#ifdef _WIN
    /* Windows */
    SYSTEM_INFO sysinfo;
    GetSystemInfo( &sysinfo );
    return sysinfo.dwNumberOfProcessors;

#elif defined (HAVE_SYSCTL)
    /* OS X (Mach-derived, BSD-derived), FreeBSD, NetBSD, OpenBSD, Linux, etc. */

    int numCPU;
    nt mib[4];
    size_t len; 

    /* set the mib for hw.ncpu */
    mib[0] = CTL_HW;
    mib[1] = HW_AVAILCPU;  /* alternatively, try HW_NCPU */

    /* get the number of CPUs from the system */
    sysctl(mib, 2, &numCPU, &len, NULL, 0);

    if (numCPU < 1) {
        mib[1] = HW_NCPU;
	sysctl( mib, 2, &numCPU, &len, NULL, 0 );

     if (numCPU < 1) {
          numCPU = 1;
     }

     return numCPU;

#elif defined (HAVE_SYSCONF)
    /*
     * Linux, Solaris, AIX.
     * Note: This is a non-standard extension of the POSIX call.
     * Because of this the sysctl() is prefered.
     */
    return sysconf( _SC_NPROCESSORS_ONLN );

#elif defined (hpux)
    return mpctl(MPC_GETNUMSPUS, NULL, NULL);

#else
     /*
      * General fallback for anything where we have no idea how it
      * works.
      */
     return 1;
#endif
}
/*
 * IRIX: numCPU = sysconf( _SC_NPROC_ONLN );
 */