From 1e081f353b9d1d9a982ee38f298f8f558a3d032f Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 1 Feb 2011 18:04:28 +0000
Subject: [PATCH] Detect number of CPUs and create particles threads
 accordingly (1+nb_cpus/2)

git-svn-id: http://svn.net-core.org/repos/t-engine4@2594 51575b47-30f0-44d4-a5cc-537603b46e54
---
 src/getself.c   | 45 +++++++++++++++++++++++++++++++++++++++++++++
 src/getself.h   |  1 +
 src/main.c      |  7 ++++++-
 src/particles.c |  8 ++++++--
 4 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/src/getself.c b/src/getself.c
index 91b096454e..e4e30ad667 100644
--- a/src/getself.c
+++ b/src/getself.c
@@ -23,6 +23,7 @@
 #if defined(SELFEXE_LINUX)
 #include <limits.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 const char *get_self_executable(int argc, char **argv)
 {
@@ -32,6 +33,11 @@ const char *get_self_executable(int argc, char **argv)
 	return res;
 }
 
+int get_number_cpus()
+{
+	return sysconf(_SC_NPROCESSORS_ONLN);
+}
+
 #elif defined(SELFEXE_WINDOWS)
 #include <stdlib.h>
 #include <windows.h>
@@ -43,9 +49,18 @@ const char *get_self_executable(int argc, char **argv)
 	return szEXEPath;
 }
 
+int get_number_cpus()
+{
+	SYSTEM_INFO sysinfo;
+	GetSystemInfo(&sysinfo);
+
+	return sysinfo.dwNumberOfProcessors;
+}
+
 #elif defined(SELFEXE_MACOSX)
 #include <mach-o/dyld.h>
 #include <string.h>
+#include <unistd.h>
 
 const char *get_self_executable(int argc, char **argv)
 {
@@ -62,10 +77,40 @@ const char *get_self_executable(int argc, char **argv)
 	return buf;
 }
 
+int get_number_cpus()
+{
+	int mib[4];
+	size_t len = sizeof(numCPU);
+
+	/* set the mib for hw.ncpu */
+	mib[0] = CTL_HW;
+	mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
+
+	/* get the number of CPUs from the system */
+	sysctl(mib, 2, &numCPU, &len, NULL, 0);
+
+	if( numCPU < 1 )
+	{
+		mib[1] = HW_NCPU;
+		sysctl( mib, 2, &numCPU, &len, NULL, 0 );
+
+		if( numCPU < 1 )
+		{
+			numCPU = 1;
+		}
+	}
+	return numCPU;
+}
+
 #else
 const char *get_self_executable(int argc, char **argv)
 {
 	return NULL;
 }
 
+int get_number_cpus()
+{
+	return 1;
+}
+
 #endif
diff --git a/src/getself.h b/src/getself.h
index b255284e9d..121ec258ad 100644
--- a/src/getself.h
+++ b/src/getself.h
@@ -22,5 +22,6 @@
 #define _GETSELF_H_
 
 extern const char *get_self_executable(int argc, char **argv);
+extern int get_number_cpus();
 
 #endif
diff --git a/src/main.c b/src/main.c
index 7a0bc87153..beff4bd093 100644
--- a/src/main.c
+++ b/src/main.c
@@ -47,6 +47,7 @@
 #define HEIGHT 600
 
 lua_State *L = NULL;
+int nb_cpus;
 bool no_debug = FALSE;
 int current_mousehandler = LUA_NOREF;
 int current_keyhandler = LUA_NOREF;
@@ -723,6 +724,10 @@ void boot_lua(int state, bool rebooting, int argc, char *argv[])
 
 int main(int argc, char *argv[])
 {
+	// Get cpu cores
+	nb_cpus = get_number_cpus();
+	printf("[CPU] Detected %d CPUs\n", nb_cpus);
+
 	// RNG init
 	init_gen_rand(time(NULL));
 
@@ -743,6 +748,7 @@ int main(int argc, char *argv[])
 	}
 
 	boot_lua(1, FALSE, argc, argv);
+	create_particles_thread();
 
 	// initialize engine and set up resolution and depth
 	Uint32 flags=SDL_INIT_VIDEO | SDL_INIT_TIMER;
@@ -795,7 +801,6 @@ int main(int argc, char *argv[])
 	boot_lua(2, FALSE, argc, argv);
 
 	//	start_xmpp_thread();
-	create_particles_thread();
 
 	pass_command_args(argc, argv);
 
diff --git a/src/particles.c b/src/particles.c
index daf1e058e2..6c9e17b003 100644
--- a/src/particles.c
+++ b/src/particles.c
@@ -37,8 +37,9 @@
 #define PARTICLE_ETERNAL 999999
 #define PARTICLES_PER_ARRAY 1000
 
-#define MAX_THREADS 7
-static particle_thread threads[MAX_THREADS];
+int MAX_THREADS = 1;
+extern int nb_cpus;
+static particle_thread *threads;
 static int textures_ref = LUA_NOREF;
 static int nb_threads = 0;
 static int cur_thread = 0;
@@ -805,6 +806,9 @@ void create_particles_thread()
 {
 	int i;
 
+	MAX_THREADS = 1 + nb_cpus / 2;
+	threads = calloc(MAX_THREADS, sizeof(particle_thread));
+
 	cur_thread = 0;
 	for (i = 0; i < MAX_THREADS; i++)
 	{
-- 
GitLab