From dff0b89a6c60633bcfef1f8f7e0c1df2eab6a61e Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 19 Mar 2013 12:05:50 +0900 Subject: [PATCH] Detect the number of CPUs available. Now qfvis will default to multi-threaded on multi-core machines. --- tools/qfvis/source/options.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tools/qfvis/source/options.c b/tools/qfvis/source/options.c index 6aab88c5d..048f604b0 100644 --- a/tools/qfvis/source/options.c +++ b/tools/qfvis/source/options.c @@ -37,10 +37,12 @@ #include #include #include +#include #include "QF/dstring.h" #include "options.h" +#include "vis.h" const char *this_program; @@ -86,6 +88,20 @@ usage (int status) exit (status); } +static int +default_threads (void) +{ + int threads = 1; +#ifdef USE_PTHREADS +# ifdef _SC_NPROCESSORS_ONLN + threads = sysconf(_SC_NPROCESSORS_ONLN); + if (threads < 1) + threads = 1; +# endif +#endif + return threads; +} + int DecodeArgs (int argc, char **argv) { @@ -93,7 +109,7 @@ DecodeArgs (int argc, char **argv) options.verbosity = 0; options.bspfile = NULL; - options.threads = 1; + options.threads = default_threads (); options.level = 4; while ((c = getopt_long (argc, argv, short_options, long_options, 0)) @@ -128,6 +144,12 @@ DecodeArgs (int argc, char **argv) usage (1); } } +#ifndef USE_PTHREADS + if (options.threads != 1) { + printf ("NOTE: ignoring thread count (unsupported)\n"); + options.threads = 1; + } +#endif if ((!options.bspfile) && argv[optind] && *(argv[optind])) options.bspfile = dstring_strdup (argv[optind++]);