]> git.street.me.uk Git - andy/viking.git/commitdiff
Add function to get number of processors.
authorRob Norris <rw_norris@hotmail.com>
Wed, 3 Dec 2014 00:32:28 +0000 (00:32 +0000)
committerRob Norris <rw_norris@hotmail.com>
Tue, 27 Jan 2015 23:42:54 +0000 (23:42 +0000)
Would be simple if only GLib 2.36+, but need to support older versions.
Particularly for Windows builds which ATM uses GLib 2.28

src/util.c
src/util.h

index 85df4d73cc782cf79d7b0cde16d45a5d6dba74ce..34f3abf1eeb32f684a75be2cd0586263d083a20c 100644 (file)
 #include "util.h"
 #include "globals.h"
 
+#ifdef WINDOWS
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+guint util_get_number_of_cpus ()
+{
+#if GLIB_CHECK_VERSION (2, 36, 0)
+  return g_get_num_processors();
+#else
+  long nprocs = 1;
+#ifdef WINDOWS
+  SYSTEM_INFO info;
+  GetSystemInfo(&info);
+  nprocs = info.dwNumberOfProcessors;
+#else
+#ifdef _SC_NPROCESSORS_ONLN
+  nprocs = sysconf(_SC_NPROCESSORS_ONLN);
+  if (nprocs < 1)
+    nprocs = 1;
+#endif
+#endif
+  return nprocs;
+#endif
+}
+
 gchar *uri_escape(gchar *str)
 {
   gchar *esc_str = g_malloc(3*strlen(str));
@@ -107,7 +134,6 @@ gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar
   // Remove newline from val and also any other whitespace
   *key = g_strstrip ( *key );
   *val = g_strstrip ( *val );
-
   return TRUE;
 }
 
index 245139af233bdfc96176050e1fd7f275ca3b394e..7525a11ee9ade63e286a103c2a39973085c24ab2 100644 (file)
@@ -29,6 +29,8 @@
 
 G_BEGIN_DECLS
 
+guint util_get_number_of_cpus (void);
+
 gchar *uri_escape(gchar *str);
 
 GList * str_array_to_glist(gchar* data[]);