summaryrefslogtreecommitdiff
path: root/mysys/my_getncpus.c
diff options
context:
space:
mode:
Diffstat (limited to 'mysys/my_getncpus.c')
-rw-r--r--mysys/my_getncpus.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/mysys/my_getncpus.c b/mysys/my_getncpus.c
index 38585161c22..0ee03631da8 100644
--- a/mysys/my_getncpus.c
+++ b/mysys/my_getncpus.c
@@ -21,10 +21,36 @@
#include <unistd.h>
#endif
+#if defined(__FreeBSD__) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
+#include <pthread_np.h>
+#include <sys/cpuset.h>
+#endif
+
static int ncpus=0;
-int my_getncpus()
+int my_getncpus(void)
{
+#if (defined(__linux__) || defined(__FreeBSD__)) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
+ cpu_set_t set;
+
+ if (!ncpus)
+ {
+ if (pthread_getaffinity_np(pthread_self(), sizeof(set), &set) == 0)
+ {
+ ncpus= CPU_COUNT(&set);
+ }
+ else
+ {
+#ifdef _SC_NPROCESSORS_ONLN
+ ncpus= sysconf(_SC_NPROCESSORS_ONLN);
+#else
+ ncpus= 2;
+#endif
+ }
+ }
+
+#else /* __linux__ || FreeBSD && HAVE_PTHREAD_GETAFFINITY_NP */
+
if (!ncpus)
{
#ifdef _SC_NPROCESSORS_ONLN
@@ -46,5 +72,8 @@ int my_getncpus()
ncpus= 2;
#endif
}
+
+#endif /* __linux__ || FreeBSD && HAVE_PTHREAD_GETAFFINITY_NP */
+
return ncpus;
}