Skip to content
/ janet Public
forked from janet-lang/janet

Commit

Permalink
Add os/cpu-count functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpakin committed May 28, 2022
1 parent e5a989c commit 48289ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Join the chat](https://badges.gitter.im/janet-language/community.svg)](https://gitter.im/janet-language/community)
 
[![builds.sr.ht status](https://builds.sr.ht/~bakpakin/janet/commits/freebsd.yml.svg)](https://builds.sr.ht/~bakpakin/janet/commits/freebsd.yml?)
[![builds.sr.ht status](https://builds.sr.ht/~bakpakin/janet/commits/openbsd.yml.svg)](https://builds.sr.ht/~bakpakin/janet/commits/openbsd.yml?)
[![builds.sr.ht status](https://builds.sr.ht/~bakpakin/janet/commits/master/freebsd.yml.svg)](https://builds.sr.ht/~bakpakin/janet/commits/freebsd.yml?)
[![builds.sr.ht status](https://builds.sr.ht/~bakpakin/janet/commits/master/openbsd.yml.svg)](https://builds.sr.ht/~bakpakin/janet/commits/openbsd.yml?)
[![Actions Status](https://github.com/janet-lang/janet/actions/workflows/test.yml/badge.svg)](https://github.com/janet-lang/janet/actions/workflows/test.yml)

<img src="https://raw.githubusercontent.com/janet-lang/janet/master/assets/janet-w200.png" alt="Janet logo" width=200 align="left">
Expand Down
42 changes: 42 additions & 0 deletions src/core/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <sys/stat.h>
#include <signal.h>

#ifdef JANET_BSD
#include <sys/sysctl.h>
#endif

#ifdef JANET_WINDOWS
#include <windows.h>
#include <direct.h>
Expand Down Expand Up @@ -201,6 +205,43 @@ JANET_CORE_FN(os_exit,
return janet_wrap_nil();
}

JANET_CORE_FN(os_cpu_count,
"(os/cpu-count &opt dflt)",
"Get an approximate number of CPUs available on for this process to use. If "
"unable to get an approximation, will return a default value dflt.") {
janet_arity(argc, 0, 1);
Janet dflt = argc > 0 ? argv[0] : janet_wrap_nil();
#ifdef JANET_WINDOWS
(void) dflt;
SYSTEM_INFO info;
GetSystemInfo(&info);
return janet_wrap_integer(info.dwNumberOfProcessors);
#elif defined(JANET_LINUX)
(void) dflt;
return janet_wrap_integer(sysconf(_SC_NPROCESSORS_ONLN));
#elif defined(JANET_BSD) && defined(HW_NCPUONLINE)
(void) dflt;
const int name[2] = {CTL_HW, HW_NCPUONLINE};
int result = 0;
size_t len = sizeof(int);
if (-1 == sysctl(name, 2, &result, &len, sizeof(result), NULL, 0)) {
return dflt;
}
return janet_wrap_integer(result);
#elif defined(JANET_BSD) && defined(HW_NCPU)
(void) dflt;
const int name[2] = {CTL_HW, HW_NCPU};
int result = 0;
size_t len = sizeof(int);
if (-1 == sysctl(name, 2, &result, &len, sizeof(result), NULL, 0)) {
return dflt;
}
return janet_wrap_integer(result);
#else
return dflt;
#endif
}

#ifndef JANET_REDUCED_OS

#ifndef JANET_NO_PROCESSES
Expand Down Expand Up @@ -2195,6 +2236,7 @@ void janet_lib_os(JanetTable *env) {
JANET_CORE_REG("os/chmod", os_chmod),
JANET_CORE_REG("os/touch", os_touch),
JANET_CORE_REG("os/cd", os_cd),
JANET_CORE_REG("os/cpu-count", os_cpu_count),
#ifndef JANET_NO_UMASK
JANET_CORE_REG("os/umask", os_umask),
#endif
Expand Down
Empty file modified tools/format.sh
100644 → 100755
Empty file.

0 comments on commit 48289ac

Please sign in to comment.