Move get_logical_cpus() from lavc/pthread to lavu/cpu.

It will be useful in lavfi, and could conceivably be useful to the user
applications as well.
This commit is contained in:
Anton Khirnov
2013-05-23 08:59:07 +02:00
parent 90f9a5830b
commit 2a6eaeaa85
5 changed files with 65 additions and 55 deletions

View File

@ -31,29 +31,12 @@
#include "config.h"
#if HAVE_SCHED_GETAFFINITY
#define _GNU_SOURCE
#include <sched.h>
#endif
#if HAVE_GETPROCESSAFFINITYMASK
#include <windows.h>
#endif
#if HAVE_SYSCTL
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#if HAVE_SYSCONF
#include <unistd.h>
#endif
#include "avcodec.h"
#include "internal.h"
#include "thread.h"
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/cpu.h"
#if HAVE_PTHREADS
#include <pthread.h>
@ -153,40 +136,6 @@ typedef struct FrameThreadContext {
* limit the number of threads to 16 for automatic detection */
#define MAX_AUTO_THREADS 16
static int get_logical_cpus(AVCodecContext *avctx)
{
int ret, nb_cpus = 1;
#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
ret = sched_getaffinity(0, sizeof(cpuset), &cpuset);
if (!ret) {
nb_cpus = CPU_COUNT(&cpuset);
}
#elif HAVE_GETPROCESSAFFINITYMASK
DWORD_PTR proc_aff, sys_aff;
ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff);
if (ret)
nb_cpus = av_popcount64(proc_aff);
#elif HAVE_SYSCTL && defined(HW_NCPU)
int mib[2] = { CTL_HW, HW_NCPU };
size_t len = sizeof(nb_cpus);
ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0);
if (ret == -1)
nb_cpus = 0;
#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
nb_cpus = sysconf(_SC_NPROC_ONLN);
#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
#endif
av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
return nb_cpus;
}
static void* attribute_align_arg worker(void *v)
{
AVCodecContext *avctx = v;
@ -292,7 +241,8 @@ static int thread_init(AVCodecContext *avctx)
int thread_count = avctx->thread_count;
if (!thread_count) {
int nb_cpus = get_logical_cpus(avctx);
int nb_cpus = av_cpu_count();
av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
// use number of cores + 1 as thread count if there is more than one
if (nb_cpus > 1)
thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
@ -790,7 +740,8 @@ static int frame_thread_init(AVCodecContext *avctx)
int i, err = 0;
if (!thread_count) {
int nb_cpus = get_logical_cpus(avctx);
int nb_cpus = av_cpu_count();
av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
// use number of cores + 1 as thread count if there is more than one
if (nb_cpus > 1)
thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);