From 8ea8d24fcad0f0113615ef4dd331e4a127a66436 Mon Sep 17 00:00:00 2001 From: Mikael Ohlson Date: Tue, 23 Mar 2010 14:06:19 +0100 Subject: New implementation for java.lang.Runtime's availableProcessors(). This patch adds a native implementation for availableProcessors(), replacing the hardcoded "always return 1" implementation. It uses sysconf(_SC_NPROCESSORS_ONLN) to get the number of online processors. Signed-off-by: Christian Bejram --- vm/native/java_lang_Runtime.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'vm/native/java_lang_Runtime.c') diff --git a/vm/native/java_lang_Runtime.c b/vm/native/java_lang_Runtime.c index 1278f032b..36a11ef02 100644 --- a/vm/native/java_lang_Runtime.c +++ b/vm/native/java_lang_Runtime.c @@ -19,7 +19,8 @@ */ #include "Dalvik.h" #include "native/InternalNativePriv.h" - +#include +#include /* * public void gc() @@ -105,6 +106,26 @@ static void Dalvik_java_lang_Runtime_runFinalization(const u4* args, RETURN_VOID(); } +/* + * public int availableProcessors() + * + * Returns the number of online processors, at least one. + * + */ +static void Dalvik_java_lang_Runtime_availableProcessors(const u4* args, + JValue* pResult) +{ + long result = 1; +#ifdef _SC_NPROCESSORS_ONLN + result = sysconf(_SC_NPROCESSORS_ONLN); + if (result > INT_MAX) { + result = INT_MAX; + } else if (result < 1 ) { + result = 1; + } +#endif + RETURN_INT((int)result); +} /* * public void maxMemory() * @@ -149,6 +170,8 @@ const DalvikNativeMethod dvm_java_lang_Runtime[] = { Dalvik_java_lang_Runtime_freeMemory }, { "gc", "()V", Dalvik_java_lang_Runtime_gc }, + { "availableProcessors", "()I", + Dalvik_java_lang_Runtime_availableProcessors }, { "maxMemory", "()J", Dalvik_java_lang_Runtime_maxMemory }, { "nativeExit", "(IZ)V", @@ -161,4 +184,3 @@ const DalvikNativeMethod dvm_java_lang_Runtime[] = { Dalvik_java_lang_Runtime_totalMemory }, { NULL, NULL, NULL }, }; - -- cgit v1.2.3