Skip to content

Commit

Permalink
runtime: improve numcpu detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 19, 2025
1 parent 8edf25e commit fe7ff21
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
12 changes: 12 additions & 0 deletions std/runtime/mem.jule
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ fn sliceAsStr(b: []byte): str {
ret ""
}
ret unsafe { strBytePtr(&b[0], len(b)) }
}

// Fills the first n bytes of the pointer p with the constant byte b.
unsafe fn memset(mut p: *unsafe, b: byte, mut n: uint) {
if p == nil {
ret
}
mut bp := (*byte)(p)
for n > 0; n-- {
*bp = b
bp++
}
}
6 changes: 5 additions & 1 deletion std/runtime/sysconf_darwin.jule
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

// See: macOS 10.5 SDK
// https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.5.sdk/usr/include/unistd.h

use integ "std/jule/integrated"

cpp fn sysconf(name: integ::Int): integ::Long

const _SC_NPROCESSORS_ONLN = 58
const _SC_NPROCESSORS_CONF = 0x0039
const _SC_NPROCESSORS_ONLN = 0x003A
6 changes: 5 additions & 1 deletion std/runtime/sysconf_linux.jule
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

// See: Linux Standard Base Core Specification, Generic Part
// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic.html

use integ "std/jule/integrated"

cpp fn sysconf(name: integ::Int): integ::Long

const _SC_NPROCESSORS_ONLN = 84
const _SC_NPROCESSORS_CONF = 0x0053
const _SC_NPROCESSORS_ONLN = 0x0054
10 changes: 9 additions & 1 deletion std/runtime/thread_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ fn osyield() {
// This function must initialize critical data of the typical Jule program.
fn __init() {
// Set numcpu.
numcpu = unsafe { int(cpp.sysconf(_SC_NPROCESSORS_ONLN)) }
{
numcpu = unsafe { int(cpp.sysconf(_SC_NPROCESSORS_ONLN)) }
if numcpu == 0 {
numcpu = unsafe { int(cpp.sysconf(_SC_NPROCESSORS_CONF)) }
}
if numcpu == 0 {
numcpu = 1
}
}
// Push the main thread to threads.
// See the documentation of the pushNewThread function.
// The |threads| should be initialized here, because compiler will not do it.
Expand Down
9 changes: 8 additions & 1 deletion std/runtime/thread_windows.jule
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

use integ "std/jule/integrated"
use "std/mem"
use "std/sys"

cpp unsafe fn CreateThread(*unsafe, int, *unsafe, *unsafe, int, *unsafe): *unsafe
Expand Down Expand Up @@ -71,8 +72,14 @@ fn __init() {
// Set numcpu.
{
let info: cpp.SYSTEM_INFO
unsafe { cpp.GetSystemInfo(&info) }
unsafe {
memset(&info, 0, mem::SizeOf(cpp.SYSTEM_INFO))
cpp.GetSystemInfo(&info)
}
numcpu = int(info.dwNumberOfProcessors)
if numcpu == 0 {
numcpu = 1
}
}
// Push the main thread to threads.
// See the documentation of the pushNewThread function.
Expand Down

0 comments on commit fe7ff21

Please sign in to comment.