From 933d6755d4cd316745feb90601451e4f07ee2340 Mon Sep 17 00:00:00 2001 From: Laurent Aimar Date: Mon, 3 May 2010 21:54:28 +0200 Subject: [PATCH] Added vlc_GetCPUCount() to retreive the number of available CPU. It is implemented for win32 and when sched_getaffinity() is available. --- configure.ac | 2 +- include/vlc_cpu.h | 1 + src/libvlccore.sym | 1 + src/misc/cpu.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 8510e84619..8571ab6d2b 100644 --- a/configure.ac +++ b/configure.ac @@ -556,7 +556,7 @@ AC_CHECK_FUNCS(fdatasync,, ]) dnl Check for non-standard system calls -AC_CHECK_FUNCS([accept4 dup3 eventfd fstatfs vmsplice]) +AC_CHECK_FUNCS([accept4 dup3 eventfd fstatfs vmsplice sched_getaffinity]) AH_BOTTOM([#include ]) diff --git a/include/vlc_cpu.h b/include/vlc_cpu.h index feb6e5e130..bbc7b5b468 100644 --- a/include/vlc_cpu.h +++ b/include/vlc_cpu.h @@ -63,6 +63,7 @@ # endif VLC_EXPORT( unsigned, vlc_CPU, ( void ) ); +VLC_EXPORT( unsigned, vlc_GetCPUCount, ( void ) ); /** Are floating point operations fast? * If this bit is not set, you should try to use fixed-point instead. diff --git a/src/libvlccore.sym b/src/libvlccore.sym index a8479ebb70..f2343e9f99 100644 --- a/src/libvlccore.sym +++ b/src/libvlccore.sym @@ -493,6 +493,7 @@ vlc_sem_destroy vlc_sem_post vlc_sem_wait vlc_control_cancel +vlc_GetCPUCount vlc_CPU vlc_error vlc_event_attach diff --git a/src/misc/cpu.c b/src/misc/cpu.c index 3b0d278999..fb705a249f 100644 --- a/src/misc/cpu.c +++ b/src/misc/cpu.c @@ -318,6 +318,38 @@ const struct #endif }; +/** + * Return the number of available logical CPU. + */ +unsigned vlc_GetCPUCount(void) +{ +#ifdef WIN32 + DWORD process_mask; + DWORD system_mask; + if (!GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask)) + return 1; + + unsigned count = 0; + while (system_mask) { + count++; + system_mask >>= 1; + } + return count; +#elif HAVE_SCHED_GETAFFINITY + cpu_set_t cpu; + CPU_ZERO(&cpu); + if (sched_getaffinity(0, sizeof(cpu), &cpu) < 0) + return 1; + unsigned count = 0; + for (unsigned i = 0; i < CPU_SETSIZE; i++) + count += CPU_ISSET(i, &cpu) != 0; + return count; +#else +# warning "vlc_GetCPUCount is not implemented for your platform" + return 1; +#endif +} + /** * Check if a directory name contains usable plugins w.r.t. the hardware * capabilities. Loading a plugin when the hardware has insufficient -- 2.39.2