Skip to content

Commit

Permalink
Use get_nprocs to implement posix.cpu_count()
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed Dec 28, 2024
1 parent d15d7c1 commit eb5eacb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/deemon/system-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ header_nostdinc("spawn.h");
header_nostdinc("vfork.h");
header_nostdinc("paths.h");
header_nostdinc("sys/sendfile.h");
header_nostdinc("sys/sysinfo.h", addparen(linux) + " || " + addparen(kos));

include_known_headers();

Expand Down Expand Up @@ -1207,6 +1208,8 @@ functest("_sysconf(0)");
functest("mpctl(0, 0, 0)", "defined(__hpux__)");
functest("sysctl(0, 0, 0, 0, 0, 0)");

functest("get_nprocs()", "defined(CONFIG_HAVE_SYS_SYSINFO_H)");

constant("_SC_NPROCESSORS_ONLN");
constant("_SC_NPROC_ONLN");
constant("MPC_GETNUMSPUS", "defined(__hpux__)");
Expand Down Expand Up @@ -1957,6 +1960,14 @@ feature("CONSTANT_NAN", "1", test: "extern int val[NAN != 0.0 ? 1 : -1]; return
#define CONFIG_HAVE_SYS_SENDFILE_H
#endif

#ifdef CONFIG_NO_SYS_SYSINFO_H
#undef CONFIG_HAVE_SYS_SYSINFO_H
#elif !defined(CONFIG_HAVE_SYS_SYSINFO_H) && \
(__has_include(<sys/sysinfo.h>) || (defined(__NO_has_include) && ((defined(__linux__) || \
defined(__linux) || defined(linux)) || defined(__KOS__))))
#define CONFIG_HAVE_SYS_SYSINFO_H
#endif

#ifdef CONFIG_HAVE_IO_H
#include <io.h>
#endif /* CONFIG_HAVE_IO_H */
Expand Down Expand Up @@ -8976,6 +8987,13 @@ feature("CONSTANT_NAN", "1", test: "extern int val[NAN != 0.0 ? 1 : -1]; return
#define CONFIG_HAVE_sysctl
#endif

#ifdef CONFIG_NO_get_nprocs
#undef CONFIG_HAVE_get_nprocs
#elif !defined(CONFIG_HAVE_get_nprocs) && \
(defined(get_nprocs) || defined(__get_nprocs_defined) || defined(CONFIG_HAVE_SYS_SYSINFO_H))
#define CONFIG_HAVE_get_nprocs
#endif

#ifdef CONFIG_NO__SC_NPROCESSORS_ONLN
#undef CONFIG_HAVE__SC_NPROCESSORS_ONLN
#elif !defined(CONFIG_HAVE__SC_NPROCESSORS_ONLN) && \
Expand Down
15 changes: 15 additions & 0 deletions src/dex/posix/p-cpucount.c.inl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ DECL_BEGIN
#undef posix_cpu_count_USE_sysctl__HW_NCPU
#undef posix_cpu_count_USE_mpctl__MPC_GETNUMSPUS
#undef posix_cpu_count_USE_sysconf__SC_NPROC_ONLN
#undef posix_cpu_count_USE_get_nprocs
#undef posix_cpu_count_USE_open_AND_proc_cpuinfo
#undef posix_cpu_count_USE_STUB
#if defined(CONFIG_HOST_WINDOWS)
Expand All @@ -50,13 +51,20 @@ DECL_BEGIN
#define posix_cpu_count_USE_mpctl__MPC_GETNUMSPUS
#elif defined(CONFIG_HAVE_sysconf) && defined(CONFIG_HAVE__SC_NPROC_ONLN)
#define posix_cpu_count_USE_sysconf__SC_NPROC_ONLN
#elif defined(CONFIG_HAVE_get_nprocs)
#define posix_cpu_count_USE_get_nprocs
#elif defined(CONFIG_HAVE_PROCFS)
#define posix_cpu_count_USE_open_AND_proc_cpuinfo
#else /* ... */
#define posix_cpu_count_USE_STUB
#endif /* !... */


#ifdef posix_cpu_count_USE_get_nprocs
DECL_END
#include <sys/sysinfo.h>
DECL_BEGIN
#endif /* posix_cpu_count_USE_get_nprocs */

/*[[[deemon import("rt.gen.dexutils").gw("cpu_count", "->?Dint", libname: "posix", ispure: true); ]]]*/
FORCELOCAL WUNUSED DREF DeeObject *DCALL posix_cpu_count_f_impl(void);
Expand Down Expand Up @@ -144,6 +152,13 @@ FORCELOCAL WUNUSED DREF DeeObject *DCALL posix_cpu_count_f_impl(void)
return DeeInt_NewUInt((unsigned int)result);
#endif /* posix_cpu_count_USE_sysconf__SC_NPROC_ONLN */

#ifdef posix_cpu_count_USE_get_nprocs
int result = get_nprocs();
if unlikely(result <= 0)
result = 1; /* Shouldn't happen... */
return DeeInt_NewUInt((unsigned int)result);
#endif /* posix_cpu_count_USE_get_nprocs */

#ifdef posix_cpu_count_USE_open_AND_proc_cpuinfo
unsigned int result = 0;
DREF DeeObject *file;
Expand Down

0 comments on commit eb5eacb

Please sign in to comment.