mirror of
https://github.com/Relintai/osxcross.git
synced 2025-02-03 22:45:56 +01:00
Replace cpucount.c with cpucount.cpp.
Uses a new function in C++11. Avoids platform-specific code.
This commit is contained in:
parent
233b1d8860
commit
d3961b8ed4
103
tools/cpucount.c
103
tools/cpucount.c
@ -1,103 +0,0 @@
|
||||
/***********************************************************************
|
||||
* OSXCross *
|
||||
* Copyright (C) 2013-2016 by Thomas Poechtrager *
|
||||
* t.poechtrager@gmail.com *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software *
|
||||
* Foundation, Inc., *
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
***********************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
#define WIN32
|
||||
#endif /* __CYGWIN__ */
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#endif /* WIN32 */
|
||||
|
||||
#ifdef __linux__
|
||||
#define __USE_GNU
|
||||
#include <sched.h>
|
||||
#undef __USE_GNU
|
||||
#endif /* __linux__ */
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
|
||||
defined(__OpenBSD__) || defined(__DragonFly__) || \
|
||||
defined(__APPLE__)
|
||||
#include <unistd.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#ifndef HW_AVAILCPU
|
||||
#define HW_AVAILCPU 25
|
||||
#endif /* HW_AVAILCPU */
|
||||
#endif /* BSD */
|
||||
|
||||
int getcpucount() {
|
||||
#ifdef WIN32
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
|
||||
return sysinfo.dwNumberOfProcessors;
|
||||
#else
|
||||
#ifdef __linux__
|
||||
cpu_set_t cs;
|
||||
int i, cpucount = 0;
|
||||
|
||||
CPU_ZERO(&cs);
|
||||
|
||||
if (sched_getaffinity(0, sizeof(cs), &cs))
|
||||
return 1;
|
||||
|
||||
for (i = 0; i < CPU_SETSIZE; i++)
|
||||
if (CPU_ISSET(i, &cs))
|
||||
cpucount++;
|
||||
|
||||
return cpucount;
|
||||
#else
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
|
||||
defined(__OpenBSD__) || defined(__DragonFly__) || \
|
||||
defined(__APPLE__)
|
||||
int cpucount = 0;
|
||||
int mib[4];
|
||||
size_t len = sizeof(cpucount);
|
||||
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_AVAILCPU;
|
||||
|
||||
sysctl(mib, 2, &cpucount, &len, NULL, 0);
|
||||
|
||||
if (cpucount < 1) {
|
||||
mib[1] = HW_NCPU;
|
||||
sysctl(mib, 2, &cpucount, &len, NULL, 0);
|
||||
}
|
||||
|
||||
return cpucount ? cpucount : 1;
|
||||
#else
|
||||
#warning unknown platform
|
||||
return 1;
|
||||
#endif /* BSD */
|
||||
#endif /* __linux__ */
|
||||
#endif /* WIN32 */
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("%d\n", getcpucount());
|
||||
return 0;
|
||||
}
|
11
tools/cpucount.cpp
Normal file
11
tools/cpucount.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
/** Print number of (enabled) CPU cores.
|
||||
*
|
||||
* Requires C++11 or better.
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
std::cout << std::thread::hardware_concurrency() << std::endl;
|
||||
}
|
@ -1,18 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
set -u
|
||||
|
||||
which cc &>/dev/null || { echo "1" && exit 0; }
|
||||
# Print number of enabled CPUs. Use this as a simple, platform-independent
|
||||
# replacement for nproc or ncpus.
|
||||
#
|
||||
# The shell script wraps a simple C++ tool which will be compiled on demand.
|
||||
|
||||
prog="cpucount"
|
||||
|
||||
pushd "${0%/*}" >/dev/null 2>&1
|
||||
# This script's location. The proper way to do this in bash is using
|
||||
# ${BASH_SOURCE[0]}; ignore the possibility of softlinks.
|
||||
script_dir=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
|
||||
|
||||
prog="$script_dir/cpucount"
|
||||
case "$(uname -s)" in
|
||||
*NT* | CYGWIN*)
|
||||
prog="${prog}.exe" ;;
|
||||
esac
|
||||
|
||||
[ ! -f $prog ] && cc cpucount.c -o cpucount &>/dev/null
|
||||
if [ ! -f $prog ]
|
||||
then
|
||||
# Don't have cpucount. Build it.
|
||||
|
||||
./$prog
|
||||
if ! which c++ >/dev/null
|
||||
then
|
||||
# Can't compile cpucount. Just give the safe answer.
|
||||
echo 1
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Attempt to compile cpucount.cpp.
|
||||
if ! c++ $prog.cpp -o $prog &>/dev/null
|
||||
then
|
||||
# Okay, that didn't work... Try it with gcc/clang's option to force
|
||||
# C++11. Versions of gcc older than 6.x still default to C++98.
|
||||
c++ $prog.cpp -std=c++11 -o $prog >/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
# Now, at last: run cpucount.
|
||||
$prog
|
||||
|
Loading…
Reference in New Issue
Block a user