Author: post
Date: 2010-06-26 10:47:04 +0200 (Sat, 26 Jun 2010)
New Revision: 3445
Modified:
trunk/librawstudio/rs-utils.c
Log:
Updated core count detection, use POSIX function if available (Thanks Edouard),
and don't use a critical section after first use.
Modified: trunk/librawstudio/rs-utils.c
===================================================================
--- trunk/librawstudio/rs-utils.c 2010-06-25 15:16:14 UTC (rev 3444)
+++ trunk/librawstudio/rs-utils.c 2010-06-26 08:47:04 UTC (rev 3445)
@@ -152,9 +152,19 @@
/* We assume processors will not be added/removed during our lifetime */
static gint num = 0;
+ if (num)
+ return num;
+
g_static_mutex_lock (&lock);
if (num == 0)
{
+ /* Use a temporary for thread safety */
+ gint temp_num = 0;
+#if defined(_SC_NPROCESSORS_ONLN)
+ /* Use the POSIX way of getting the number of processors */
+ temp_num = sysconf(_SC_NPROCESSORS_ONLN);
+#elif defined (__linux__) && (defined (__i386__) || defined (__x86_64__))
+ /* Parse the /proc/cpuinfo exposed by Linux i386/amd64 kernels
*/
GIOChannel *io;
gchar *line;
@@ -166,14 +176,20 @@
if (line)
{
if (g_str_has_prefix(line, "processor"))
- num++;
+ temp_num++;
g_free(line);
}
g_io_channel_shutdown(io, FALSE, NULL);
g_io_channel_unref(io);
}
- else
- num = 1;
+#elif defined(_WIN32)
+ /* Use pthread on windows */
+ temp_num = pthread_num_processors_np();
+#endif
+ /* Be sure we have at least 1 processor and as sanity check,
clamp to no more than 127 */
+ temp_num = (temp_num <= 0) ? 1 : MIN(temp_num, 127);
+ g_debug("Detected %d CPU cores.", temp_num);
+ num = temp_num;
}
g_static_mutex_unlock (&lock);
_______________________________________________
Rawstudio-commit mailing list
[email protected]
http://rawstudio.org/cgi-bin/mailman/listinfo/rawstudio-commit