[K/N] Implement Platform.availableProcessors

^KT-48179
This commit is contained in:
Pavel Kunyavskiy
2021-12-15 10:29:07 +03:00
committed by Space
parent 964cf937c3
commit 2ba0a7b40f
6 changed files with 136 additions and 6 deletions
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import kotlin.native.*
import kotlin.test.*
import cstdlib.*
import kotlinx.cinterop.*
@OptIn(kotlin.ExperimentalStdlibApi::class)
fun main() {
val x = Platform.getAvailableProcessors()
println(x)
assertTrue(x > 0)
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "12345", 1)
assertEquals(Platform.getAvailableProcessors(), 12345)
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", Long.MAX_VALUE.toString(), 1)
assertFailsWith<IllegalStateException> { Platform.getAvailableProcessors() }
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "-1", 1)
assertFailsWith<IllegalStateException> { Platform.getAvailableProcessors() }
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "0", 1)
assertFailsWith<IllegalStateException> { Platform.getAvailableProcessors() }
// windows doesn't support empty env variables
if (Platform.osFamily != OsFamily.WINDOWS) {
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "", 1)
assertFailsWith<IllegalStateException> { Platform.getAvailableProcessors() }
}
setenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS", "123aaaa", 1)
assertFailsWith<IllegalStateException> { Platform.getAvailableProcessors() }
unsetenv("KOTLIN_NATIVE_AVAILABLE_PROCESSORS")
assertEquals(Platform.getAvailableProcessors(), x)
}
@@ -0,0 +1,21 @@
headers = stdlib.h
compilerOpts.mingw = -D ADD_WINDOWS_ENV_FUNCTIONS
---
#ifdef ADD_WINDOWS_ENV_FUNCTIONS
static inline int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if (!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
static inline int unsetenv(const char *name)
{
return _putenv_s(name, "");
}
#endif